Skip to content

Commit

Permalink
feat(popx): allow running custom go migrations (#468)
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik authored Feb 17, 2022
1 parent 0df62fc commit 6b68933
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 4 deletions.
21 changes: 17 additions & 4 deletions popx/migration_box.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ type (
Dir fs.FS
l *logrusx.Logger
migrationContent MigrationContent
goMigrations Migrations
}
MigrationContent func(mf Migration, c *pop.Connection, r []byte, usingTemplate bool) (string, error)
GoMigration func(c *pop.Tx) error
)

func WithTemplateValues(v map[string]interface{}) func(*MigrationBox) *MigrationBox {
Expand All @@ -41,10 +43,17 @@ func WithMigrationContentMiddleware(middleware func(content string, err error) (
}
}

// NewMigrationBox from a packr.Dir and a Connection.
//
// migrations, err := NewMigrationBox(pkger.Dir("/migrations"))
//
// WithGoMigrations adds migrations that have a custom migration runner.
// TEST THEM THOROUGHLY!
// It will be very hard to fix a buggy migration.
func WithGoMigrations(migrations Migrations) func(*MigrationBox) *MigrationBox {
return func(m *MigrationBox) *MigrationBox {
m.goMigrations = migrations
return m
}
}

// NewMigrationBox creates a new migration box.
func NewMigrationBox(dir fs.FS, m *Migrator, opts ...func(*MigrationBox) *MigrationBox) (*MigrationBox, error) {
mb := &MigrationBox{
Migrator: m,
Expand Down Expand Up @@ -79,6 +88,10 @@ func NewMigrationBox(dir fs.FS, m *Migrator, opts ...func(*MigrationBox) *Migrat
return mb, err
}

for _, migration := range mb.goMigrations {
mb.Migrations[migration.Direction] = append(mb.Migrations[migration.Direction], migration)
}

return mb, nil
}

Expand Down
92 changes: 92 additions & 0 deletions popx/migration_box_gomigration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package popx_test

import (
"context"
"testing"
"time"

"github.com/gobuffalo/pop/v6"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ory/x/logrusx"
"github.com/ory/x/popx"
)

func TestGoMigrations(t *testing.T) {
var called []time.Time

goMigrations := popx.Migrations{
{
Path: "gomigration_0",
Version: "20000101000000",
Name: "gomigration_0",
Direction: "up",
Type: "go",
DBType: "all",
Runner: func(migration popx.Migration, _ *pop.Connection, tx *pop.Tx) error {
called[0] = time.Now()
return nil
},
},
{
Path: "gomigration_0",
Version: "20000101000000",
Name: "gomigration_0",
Direction: "down",
Type: "go",
DBType: "all",
Runner: func(migration popx.Migration, _ *pop.Connection, tx *pop.Tx) error {
called[1] = time.Now()
return nil
},
},
{
Path: "gomigration_1",
Version: "20220215110652",
Name: "gomigration_1",
Direction: "up",
Type: "go",
DBType: "all",
Runner: func(migration popx.Migration, _ *pop.Connection, tx *pop.Tx) error {
called[2] = time.Now()
return nil
},
},
{
Path: "gomigration_1",
Version: "20220215110652",
Name: "gomigration_1",
Direction: "down",
Type: "go",
DBType: "all",
Runner: func(migration popx.Migration, _ *pop.Connection, tx *pop.Tx) error {
called[3] = time.Now()
return nil
},
},
}

called = make([]time.Time, len(goMigrations))

c, err := pop.NewConnection(&pop.ConnectionDetails{
URL: "sqlite://file::memory:?_fk=true",
})
require.NoError(t, err)
require.NoError(t, c.Open())

mb, err := popx.NewMigrationBox(transactionalMigrations, popx.NewMigrator(c, logrusx.New("", ""), nil, 0), popx.WithGoMigrations(goMigrations))
require.NoError(t, err)
require.NoError(t, mb.Up(context.Background()))

assert.Zero(t, called[1])
assert.Zero(t, called[3])
assert.NotZero(t, called[0])
assert.NotZero(t, called[2])
assert.True(t, called[0].Before(called[2]))

require.NoError(t, mb.Down(context.Background(), -1))
assert.NotZero(t, called[1])
assert.NotZero(t, called[3])
assert.True(t, called[3].Before(called[1]))
}

0 comments on commit 6b68933

Please sign in to comment.