github.com/friesencr/pop/v6@v6.1.6/migration_content.go (about)

     1  package pop
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"text/template"
     8  
     9  	"github.com/gobuffalo/fizz"
    10  )
    11  
    12  // MigrationContent returns the content of a migration.
    13  func MigrationContent(mf Migration, c *Connection, r io.Reader, usingTemplate bool) (string, error) {
    14  	b, err := io.ReadAll(r)
    15  	if err != nil {
    16  		return "", nil
    17  	}
    18  
    19  	content := ""
    20  	if usingTemplate {
    21  		t := template.Must(template.New("migration").Parse(string(b)))
    22  		var bb bytes.Buffer
    23  		err = t.Execute(&bb, c.Dialect.Details())
    24  		if err != nil {
    25  			return "", fmt.Errorf("could not execute migration template %s: %w", mf.Path, err)
    26  		}
    27  		content = bb.String()
    28  	} else {
    29  		content = string(b)
    30  	}
    31  
    32  	if mf.Type == "fizz" {
    33  		content, err = fizz.AString(content, c.Dialect.FizzTranslator())
    34  		if err != nil {
    35  			return "", fmt.Errorf("could not fizz the migration %s: %w", mf.Path, err)
    36  		}
    37  	}
    38  
    39  	return content, nil
    40  }