gomigrate icon indicating copy to clipboard operation
gomigrate copied to clipboard

Support for migrations from non filesystem sources

Open hobeone opened this issue 8 years ago • 1 comments

I'd like to add support for getting migrations from various sources. Either directly passed in as strings or built into the binary as an asset using something like go-bindata.

I think doing this the right way would substantially change gomigrate's API though. Here's what I was thinking:

NewMigrator(db, adapter, []Migrations)

  • This would change to just initializing the struct and wouldn't actually connect to the db
  • Since it's just initializing you wouldn't need a logger there and you could change the logger field to be a public field allowing users to set it to what the user wants after initialization

MigrationsFromDir("./path/to/migrations")

  • This would support the current behavior.

MigrationsFromAssets(Asset, AssetDir, "path")

  • This would allow consumption of migrations built with go-bindata

You could also just construct straight migrations in your go code and pass them in as an array.

Example use cases:

m := []Migrations{
  Migration{
    Id: 100,
    Up: `create table "foo" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" varchar(255) NOT NULL UNIQUE
);`,
   Down: `drop table "users";`,
}

migrator := NewMigrator(db, "sqlite3", m)
migrator.Logger = logrus.StandardLogger()
err := migrator.Migrate()
if err != nil {
  panic(err)
}
migrations, err := MigrationsFromDir(dirpath)
if err != nil {
  //handle error
}
migrator := NewMigrator(db, "sqlite3", migrations)
migrator.Migrate()

Since this would be a fairly substantial API change I wanted to see what you thought of the proposal before doing any work on it.

Thoughts?

hobeone avatar Jun 20 '16 02:06 hobeone

I had time to hack on this and this is now supported in my fork: https://github.com/hobeone/gomigrate

I got a little carried away and fixed some lint errors when they were highlighted in my editor.

hobeone avatar Jun 22 '16 21:06 hobeone