...

Source file src/github.com/ory/x/popx/migration_content.go

Documentation: github.com/ory/x/popx

     1  package popx
     2  
     3  import (
     4  	"bytes"
     5  	"text/template"
     6  
     7  	"github.com/ory/x/pkgerx"
     8  
     9  	"github.com/gobuffalo/fizz"
    10  	"github.com/gobuffalo/pop/v5"
    11  	"github.com/pkg/errors"
    12  )
    13  
    14  func ParameterizedMigrationContent(params map[string]interface{}) func(mf Migration, c *pop.Connection, r []byte, usingTemplate bool) (string, error) {
    15  	return func(mf Migration, c *pop.Connection, b []byte, usingTemplate bool) (string, error) {
    16  		content := ""
    17  		if usingTemplate {
    18  			t := template.New("migration")
    19  			t.Funcs(pkgerx.SQLTemplateFuncs)
    20  			t, err := t.Parse(string(b))
    21  			if err != nil {
    22  				return "", errors.Wrapf(err, "could not parse template %s", mf.Path)
    23  			}
    24  			var bb bytes.Buffer
    25  			err = t.Execute(&bb, struct {
    26  				IsSQLite       bool
    27  				IsCockroach    bool
    28  				IsMySQL        bool
    29  				IsMariaDB      bool
    30  				IsPostgreSQL   bool
    31  				DialectDetails *pop.ConnectionDetails
    32  				Parameters     map[string]interface{}
    33  			}{
    34  				IsSQLite:       c.Dialect.Name() == "sqlite3",
    35  				IsCockroach:    c.Dialect.Name() == "cockroach",
    36  				IsMySQL:        c.Dialect.Name() == "mysql",
    37  				IsMariaDB:      c.Dialect.Name() == "mariadb",
    38  				IsPostgreSQL:   c.Dialect.Name() == "postgres",
    39  				DialectDetails: c.Dialect.Details(),
    40  				Parameters:     params,
    41  			})
    42  			if err != nil {
    43  				return "", errors.Wrapf(err, "could not execute migration template %s", mf.Path)
    44  			}
    45  			content = bb.String()
    46  		} else {
    47  			content = string(b)
    48  		}
    49  
    50  		if mf.Type == "fizz" {
    51  			var err error
    52  			content, err = fizz.AString(content, c.Dialect.FizzTranslator())
    53  			if err != nil {
    54  				return "", errors.Wrapf(err, "could not fizz the migration %s", mf.Path)
    55  			}
    56  		}
    57  
    58  		return content, nil
    59  	}
    60  }
    61  

View as plain text