github.com/tsmith1024/pop@v4.12.2+incompatible/migration_content.go (about)

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