Skip to content

Commit

Permalink
Allow migrations to target a specific database (#62)
Browse files Browse the repository at this point in the history
* Ensure current non-suffixed migrations are executed for every supported DB type.
  • Loading branch information
stanislas-m authored and markbates committed Apr 13, 2018
1 parent 5fd1842 commit 5f09ca7
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 5 deletions.
4 changes: 4 additions & 0 deletions cockroach.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ type cockroach struct {
ConnectionDetails *ConnectionDetails
}

func (p *cockroach) Name() string {
return "cockroach"
}

func (p *cockroach) Details() *ConnectionDetails {
return p.ConnectionDetails
}
Expand Down
1 change: 1 addition & 0 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func init() {
}

type dialect interface {
Name() string
URL() string
MigrationURL() string
Details() *ConnectionDetails
Expand Down
15 changes: 13 additions & 2 deletions file_migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pop

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
Expand Down Expand Up @@ -48,12 +49,22 @@ func (fm *FileMigrator) findMigrations() error {
return nil
}
m := matches[0]
var dbType string
if m[3] == "" {
dbType = "all"
} else {
dbType = m[3][1:]
if !DialectSupported(dbType) {
return fmt.Errorf("unsupported dialect %s", dbType)
}
}
mf := Migration{
Path: p,
Version: m[1],
Name: m[2],
Direction: m[3],
Type: m[4],
DBType: dbType,
Direction: m[4],
Type: m[5],
Runner: func(mf Migration, tx *Connection) error {
f, err := os.Open(p)
if err != nil {
Expand Down
16 changes: 14 additions & 2 deletions migration_box.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package pop

import (
"fmt"

"github.com/gobuffalo/packr"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -39,12 +41,22 @@ func (fm *MigrationBox) findMigrations() error {
return nil
}
m := matches[0]
var dbType string
if m[3] == "" {
dbType = "all"
} else {
dbType = m[3][1:]
if !DialectSupported(dbType) {
return fmt.Errorf("unsupported dialect %s", dbType)
}
}
mf := Migration{
Path: p,
Version: m[1],
Name: m[2],
Direction: m[3],
Type: m[4],
DBType: dbType,
Direction: m[4],
Type: m[5],
Runner: func(mf Migration, tx *Connection) error {
content, err := migrationContent(mf, tx, f)
if err != nil {
Expand Down
21 changes: 21 additions & 0 deletions migration_box_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package pop_test

import (
"testing"

"github.com/gobuffalo/packr"

"github.com/gobuffalo/pop"
"github.com/stretchr/testify/require"
)

func Test_MigrationBox(t *testing.T) {
r := require.New(t)

b, err := pop.NewMigrationBox(packr.NewBox("./migrations/multiple"), PDB)
r.NoError(err)
r.Equal(3, len(b.Migrations["up"]))
r.Equal("mysql", b.Migrations["up"][0].DBType)
r.Equal("postgres", b.Migrations["up"][1].DBType)
r.Equal("all", b.Migrations["up"][2].DBType)
}
2 changes: 2 additions & 0 deletions migration_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type Migration struct {
Direction string
// Type of migration (sql)
Type string
// DB type (all|postgres|mysql...)
DBType string
// Runner function to run/execute the migration
Runner func(Migration, *Connection) error
}
Expand Down
Empty file.
Empty file.
Empty file.
6 changes: 5 additions & 1 deletion migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/pkg/errors"
)

var mrx = regexp.MustCompile("(\\d+)_(.+)\\.(up|down)\\.(sql|fizz)")
var mrx = regexp.MustCompile(`(\d+)_([^\.]+)(\.[a-z]+)?\.(up|down)\.(sql|fizz)`)

// NewMigrator returns a new "blank" migrator. It is recommended
// to use something like MigrationBox or FileMigrator. A "blank"
Expand Down Expand Up @@ -45,6 +45,10 @@ func (m Migrator) Up() error {
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")
if err != nil {
return errors.Wrapf(err, "problem checking for migration version %s", mi.Version)
Expand Down
4 changes: 4 additions & 0 deletions mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ type mysql struct {
ConnectionDetails *ConnectionDetails
}

func (m *mysql) Name() string {
return "mysql"
}

func (m *mysql) Details() *ConnectionDetails {
return m.ConnectionDetails
}
Expand Down
11 changes: 11 additions & 0 deletions pop.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/fatih/color"
)

// AvailableDialects lists the available database dialects
var AvailableDialects = []string{"postgres", "mysql", "cockroach"}

// Debug mode, to toggle verbose log traces
Expand Down Expand Up @@ -38,3 +39,13 @@ var Log = func(s string, args ...interface{}) {
logger.Println(s)
}
}

// DialectSupported checks support for the given database dialect
func DialectSupported(d string) bool {
for _, ad := range AvailableDialects {
if ad == d {
return true
}
}
return false
}
4 changes: 4 additions & 0 deletions postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ type postgresql struct {
ConnectionDetails *ConnectionDetails
}

func (p *postgresql) Name() string {
return "postgresql"
}

func (p *postgresql) Details() *ConnectionDetails {
return p.ConnectionDetails
}
Expand Down
4 changes: 4 additions & 0 deletions sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ type sqlite struct {
ConnectionDetails *ConnectionDetails
}

func (m *sqlite) Name() string {
return "sqlite3"
}

func (m *sqlite) Details() *ConnectionDetails {
return m.ConnectionDetails
}
Expand Down

0 comments on commit 5f09ca7

Please sign in to comment.