Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add migration table name option #60

Merged
merged 5 commits into from
Apr 16, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,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{},
}
}