Skip to content

Support for migrations from non filesystem sources #20

@hobeone

Description

@hobeone

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions