Skip to content

Commit

Permalink
Add migration table name option (#60)
Browse files Browse the repository at this point in the history
* add migration table name option

* update readme to include configuration example
  • Loading branch information
nawaitesidah authored and markbates committed Apr 16, 2018
1 parent 4f62567 commit 6bd7630
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 24 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,24 @@ 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.

```bash
$ 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{}
Expand Down
5 changes: 5 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions connection_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
18 changes: 11 additions & 7 deletions migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ 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 {
if mi.DBType != "all" && mi.DBType != c.Dialect.Name() {
// 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)
}
Expand All @@ -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 {
Expand All @@ -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")
}
Expand All @@ -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)
}
Expand All @@ -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 {
Expand All @@ -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")
Expand All @@ -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")
}
Expand Down
26 changes: 16 additions & 10 deletions schema_migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
},
}
}
14 changes: 8 additions & 6 deletions schema_migrations_appengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
}
}

0 comments on commit 6bd7630

Please sign in to comment.