goose generate
Closed this issue · 0 comments
We should have a goose generate
command which generates a .go
file with embedded configuration & migrations. Alleviating the need for loading files at runtime, and granting the ability to build a self-sufficient binary.
This might look like
goose generate -w goose.go -p main
Which would write goose.go
, using package main
.
If no output file is specified, write to STDOUT.
If no package is provided, we might default to main
, or scan for other .go
files in the destination directory, and use whatever package they are using.
The generated .go
file should import the github.com/CloudCom/goose/lib/goose
package, and not try to embed the entire goose library into the file. It should instead operate in a fashion such as:
var gooseMigrator *goose.Migrator
func init() {
gooseMigrator = goose.NewMigrator(gooseConfig)
gooseMigrator.AddMigration(migrationID, upFunc, downFunc)
}
migrationID
would come from the migration file name.
In the case of a go migration (goose create -type=go foo
), upFunc
and downFunc
would be the functions in that migration.
In the case of a sql migration (goose create -type=sql foo
), upFunc
and downFunc
would be the sql file converted into go code.
The user's code would then call gooseMigrator.MigrateUp()
.
Once we have this capability, we can then provide a goose build
command, which utilizes much of the above to build a standalone migrator binary.
This would mostly be taking github.com/CloudCom/goose/cmd/goose
, and throwing the goose generate
file in that package. The package can then look to see if the gooseMigrator
variable has been set, and if so use it, otherwise do the standard runtime file loading.