diff --git a/README.md b/README.md index ff5ee853..66be5c26 100644 --- a/README.md +++ b/README.md @@ -247,7 +247,7 @@ The `soda` command will run the migrations using the following command: $ soda migrate up ``` -Migrations will be run in sequential order. The previously run migrations will be kept track of in a table named `schema_migrations` in the database. +Migrations will be run in sequential order. Migrations can also be run in reverse to rollback the schema. @@ -255,6 +255,16 @@ Migrations can also be run in reverse to rollback the schema. $ soda migrate down ``` +The previously run migrations will be kept track of in a table named `schema_migration` in the database. The table name can be configured by setting `migration_table_name` of the configuration options. The example below will use `migrations` as the table name. + +```yaml +development: + dialect: "postgres" + url: "your_db_development" + options: + migration_table_name: migrations +``` + #### Find ```go user := models.User{} diff --git a/connection.go b/connection.go index 1b1f0e14..41566de1 100644 --- a/connection.go +++ b/connection.go @@ -39,6 +39,11 @@ func (c *Connection) MigrationURL() string { return c.Dialect.MigrationURL() } +// MigrationTableName returns the name of the table to track migrations +func (c *Connection) MigrationTableName() string { + return c.Dialect.Details().MigrationTableName() +} + // NewConnection creates a new connection, and sets it's `Dialect` // appropriately based on the `ConnectionDetails` passed into it. func NewConnection(deets *ConnectionDetails) (*Connection, error) { diff --git a/connection_details.go b/connection_details.go index c0eadf50..96a7f5b7 100644 --- a/connection_details.go +++ b/connection_details.go @@ -142,3 +142,8 @@ func (cd *ConnectionDetails) RetryLimit() int { } return i } + +// MigrationTableName returns the name of the table to track migrations +func (cd *ConnectionDetails) MigrationTableName() string { + return defaults.String(cd.Options["migration_table_name"], "schema_migration") +} diff --git a/migrator.go b/migrator.go index 3bec383d..95e8af89 100644 --- a/migrator.go +++ b/migrator.go @@ -42,6 +42,7 @@ type Migrator struct { func (m Migrator) Up() error { c := m.Connection return m.exec(func() error { + mtn := c.MigrationTableName() mfs := m.Migrations["up"] sort.Sort(mfs) for _, mi := range mfs { @@ -49,7 +50,7 @@ func (m Migrator) Up() error { // Skip migration for non-matching dialect continue } - exists, err := c.Where("version = ?", mi.Version).Exists("schema_migration") + exists, err := c.Where("version = ?", mi.Version).Exists(mtn) if err != nil { return errors.Wrapf(err, "problem checking for migration version %s", mi.Version) } @@ -61,7 +62,7 @@ func (m Migrator) Up() error { if err != nil { return err } - _, err = tx.Store.Exec(fmt.Sprintf("insert into schema_migration (version) values ('%s')", mi.Version)) + _, err = tx.Store.Exec(fmt.Sprintf("insert into %s (version) values ('%s')", mtn, mi.Version)) return errors.Wrapf(err, "problem inserting migration version %s", mi.Version) }) if err != nil { @@ -78,7 +79,8 @@ func (m Migrator) Up() error { func (m Migrator) Down(step int) error { c := m.Connection return m.exec(func() error { - count, err := c.Count("schema_migration") + mtn := c.MigrationTableName() + count, err := c.Count(mtn) if err != nil { return errors.Wrap(err, "migration down: unable count existing migration") } @@ -93,7 +95,7 @@ func (m Migrator) Down(step int) error { mfs = mfs[:step] } for _, mi := range mfs { - exists, err := c.Where("version = ?", mi.Version).Exists("schema_migration") + exists, err := c.Where("version = ?", mi.Version).Exists(mtn) if err != nil || !exists { return errors.Wrapf(err, "problem checking for migration version %s", mi.Version) } @@ -102,7 +104,7 @@ func (m Migrator) Down(step int) error { if err != nil { return err } - err = tx.RawQuery("delete from schema_migration where version = ?", mi.Version).Exec() + err = tx.RawQuery("delete from %s where version = ?", mtn, mi.Version).Exec() return errors.Wrapf(err, "problem deleting migration version %s", mi.Version) }) if err != nil { @@ -128,16 +130,18 @@ func (m Migrator) Reset() error { // operation. func (m Migrator) CreateSchemaMigrations() error { c := m.Connection + mtn := c.MigrationTableName() err := c.Open() if err != nil { return errors.Wrap(err, "could not open connection") } - _, err = c.Store.Exec("select * from schema_migration") + _, err = c.Store.Exec(fmt.Sprintf("select * from %s", mtn)) if err == nil { return nil } return c.Transaction(func(tx *Connection) error { + schemaMigrations := newSchemaMigrations(mtn) smSQL, err := c.Dialect.FizzTranslator().CreateTable(schemaMigrations) if err != nil { return errors.Wrap(err, "could not build SQL for schema migration table") @@ -159,7 +163,7 @@ func (m Migrator) Status() error { w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', tabwriter.TabIndent) fmt.Fprintln(w, "Version\tName\tStatus\t") for _, mf := range m.Migrations["up"] { - exists, err := m.Connection.Where("version = ?", mf.Version).Exists("schema_migration") + exists, err := m.Connection.Where("version = ?", mf.Version).Exists(m.Connection.MigrationTableName()) if err != nil { return errors.Wrapf(err, "problem with migration") } diff --git a/schema_migrations.go b/schema_migrations.go index 41de837c..43d8b833 100644 --- a/schema_migrations.go +++ b/schema_migrations.go @@ -2,14 +2,20 @@ package pop -import "github.com/gobuffalo/pop/fizz" - -var schemaMigrations = fizz.Table{ - Name: "schema_migration", - Columns: []fizz.Column{ - {Name: "version", ColType: "string"}, - }, - Indexes: []fizz.Index{ - {Name: "version_idx", Columns: []string{"version"}, Unique: true}, - }, +import ( + "fmt" + + "github.com/gobuffalo/pop/fizz" +) + +func newSchemaMigrations(name string) fizz.Table { + return fizz.Table{ + Name: name, + Columns: []fizz.Column{ + {Name: "version", ColType: "string"}, + }, + Indexes: []fizz.Index{ + {Name: fmt.Sprintf("%s_version_idx", name), Columns: []string{"version"}, Unique: true}, + }, + } } diff --git a/schema_migrations_appengine.go b/schema_migrations_appengine.go index 0e042bfb..b11f5703 100644 --- a/schema_migrations_appengine.go +++ b/schema_migrations_appengine.go @@ -4,10 +4,12 @@ package pop import "github.com/gobuffalo/pop/fizz" -var schemaMigrations = fizz.Table{ - Name: "schema_migration", - Columns: []fizz.Column{ - {Name: "version", ColType: "string"}, - }, - Indexes: []fizz.Index{}, +func newSchemaMigrations(name string) fizz.Table { + return fizz.Table{ + Name: name, + Columns: []fizz.Column{ + {Name: "version", ColType: "string"}, + }, + Indexes: []fizz.Index{}, + } }