Skip to content

Commit

Permalink
Remove func support
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed Jun 25, 2022
1 parent cbe737a commit ddfb158
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 95 deletions.
19 changes: 5 additions & 14 deletions dbump.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,10 @@ type Loader interface {

// Migration represents migration step that will be runned on a database.
type Migration struct {
ID int // ID of the migration, unique, positive, starts from 1.
Name string // Name of the migration
Apply string // Apply query
Revert string // Revert query
ApplyFn MigrationFn // Apply func
RevertFn MigrationFn // Revert func

isQuery bool // shortcut for the type of migration (query or func)
ID int // ID of the migration, unique, positive, starts from 1.
Name string // Name of the migration
Apply string // Apply query
Revert string // Revert query
}

// MigrationFn gives ability to use Go functions as migrations.
Expand Down Expand Up @@ -141,10 +137,7 @@ func (m *mig) load() ([]*Migration, error) {
case m.ID > want:
return nil, fmt.Errorf("missing migration number: %d (have %d)", want, m.ID)
default:
if (m.Apply != "" || m.Revert != "") && (m.ApplyFn != nil || m.RevertFn != nil) {
return nil, fmt.Errorf("mixing queries and functions is not allowed (migration %d)", m.ID)
}
m.isQuery = m.Apply != ""
// pass
}
}
return ms, nil
Expand Down Expand Up @@ -277,14 +270,12 @@ func (m *Migration) toStep(up bool) step {
Version: m.ID,
IsQuery: m.Apply != "",
Query: m.Apply,
QueryFn: m.ApplyFn,
}
}
return step{
Version: m.ID - 1,
IsQuery: m.Revert != "",
Query: m.Revert,
QueryFn: m.RevertFn,
}
}

Expand Down
100 changes: 20 additions & 80 deletions dbump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ func TestRunCheck(t *testing.T) {
}
}

func TestLoaderError(t *testing.T) {
}

func TestMigrateUp(t *testing.T) {
wantLog := []string{
"init", "lockdb", "getversion",
Expand Down Expand Up @@ -406,44 +403,6 @@ func TestFailOnExec(t *testing.T) {
mustEqual(t, mm.log, wantLog)
}

func TestFailOnExecFunc(t *testing.T) {
testMigrations := make([]*Migration, len(testdataMigrations))
copy(testMigrations, testdataMigrations)

testMigrations[0] = &Migration{
ID: 1,
isQuery: false,
Apply: "",
Revert: "",
ApplyFn: func(ctx context.Context, conn Conn) error {
return errors.New("nil dereference")
},
RevertFn: func(ctx context.Context, conn Conn) error {
return errors.New("nil dereference")
},
}

wantLog := []string{
"init", "lockdb", "getversion", "unlockdb",
}
mm := &MockMigrator{
ExecFn: func(ctx context.Context, query string, args ...interface{}) error {
return errors.New("syntax error")
},
}
cfg := Config{
Migrator: mm,
Loader: NewSliceLoader(testMigrations),
Mode: ModeUp,
}

err := Run(context.Background(), cfg)
if err == nil {
t.Fail()
}
mustEqual(t, mm.log, wantLog)
}

func TestFailOnLoad(t *testing.T) {
cfg := Config{
Migrator: &MockMigrator{},
Expand Down Expand Up @@ -500,20 +459,6 @@ func Test_loadMigrations(t *testing.T) {
nil,
errors.New("duplicate migration number: 2 (mig2)"),
},
{
"fail (mix of query and func)",
[]*Migration{
{
ID: 1,
Apply: "do",
ApplyFn: func(ctx context.Context, conn Conn) error {
return nil
},
},
},
nil,
errors.New("mixing queries and functions is not allowed (migration 1)"),
},
}

for _, tc := range testCases {
Expand All @@ -529,39 +474,34 @@ func Test_loadMigrations(t *testing.T) {

var testdataMigrations = []*Migration{
{
ID: 1,
Name: `0001_init.sql`,
Apply: `SELECT 1;`,
Revert: `SELECT 10;`,
isQuery: true,
ID: 1,
Name: `0001_init.sql`,
Apply: `SELECT 1;`,
Revert: `SELECT 10;`,
},
{
ID: 2,
Name: `0002_another.sql`,
Apply: `SELECT 2;`,
Revert: `SELECT 20;`,
isQuery: true,
ID: 2,
Name: `0002_another.sql`,
Apply: `SELECT 2;`,
Revert: `SELECT 20;`,
},
{
ID: 3,
Name: `0003_even-better.sql`,
Apply: `SELECT 3;`,
Revert: `SELECT 30;`,
isQuery: true,
ID: 3,
Name: `0003_even-better.sql`,
Apply: `SELECT 3;`,
Revert: `SELECT 30;`,
},
{
ID: 4,
Name: `0004_but_fix.sql`,
Apply: `SELECT 4;`,
Revert: `SELECT 40;`,
isQuery: true,
ID: 4,
Name: `0004_but_fix.sql`,
Apply: `SELECT 4;`,
Revert: `SELECT 40;`,
},
{
ID: 5,
Name: `0005_final.sql`,
Apply: `SELECT 5;`,
Revert: `SELECT 50;`,
isQuery: true,
ID: 5,
Name: `0005_final.sql`,
Apply: `SELECT 5;`,
Revert: `SELECT 50;`,
},
}

Expand Down
1 change: 0 additions & 1 deletion load.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ func loadMigrationFromFS(fsys FS, path, id, name string) (*Migration, error) {
m := parseMigration(body)
m.ID = int(n)
m.Name = name
m.isQuery = true
return m, nil
}

Expand Down

0 comments on commit ddfb158

Please sign in to comment.