Skip to content

Commit

Permalink
Add ModeDrop
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed Jun 30, 2022
1 parent 84eb32b commit 8e4dd43
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
15 changes: 14 additions & 1 deletion dbump.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ type Migrator interface {
// and might be different between databases.
Init(ctx context.Context) error

// Drop is used only in ModeDrop to remove dbump database.
// Before drop all the migrations will be reverted (as for ModeDown).
Drop(ctx context.Context) error

// Version of the migration. Used only once in the beginning.
Version(ctx context.Context) (version int, err error)

Expand Down Expand Up @@ -94,6 +98,7 @@ const (
ModeUpOne
ModeDownOne
ModeRedo
ModeDrop
modeMaxPossible
)

Expand Down Expand Up @@ -184,6 +189,10 @@ func (m *mig) runMigrations(ctx context.Context, ms []*Migration) (err error) {
return fmt.Errorf("init: %w", err)
}
err = m.runMigrationsLocked(ctx, ms)

if m.Mode == ModeDrop {
err = m.Drop(ctx)
}
return err
}

Expand All @@ -207,7 +216,7 @@ func (m *mig) getCurrAndTargetVersions(ctx context.Context, migrations int) (cur
return 0, 0, fmt.Errorf("get version: %w", err)
}

switch m.Config.Mode {
switch m.Mode {
case ModeUp:
target = migrations
if curr > target {
Expand Down Expand Up @@ -238,6 +247,9 @@ func (m *mig) getCurrAndTargetVersions(ctx context.Context, migrations int) (cur
}
target = curr

case ModeDrop:
target = 0

default:
panic("unreachable")
}
Expand Down Expand Up @@ -302,6 +314,7 @@ func (llm *locklessMigrator) LockDB(ctx context.Context) error { return nil }
func (llm *locklessMigrator) UnlockDB(ctx context.Context) error { return nil }

func (llm *locklessMigrator) Init(ctx context.Context) error { return llm.m.Init(ctx) }
func (llm *locklessMigrator) Drop(ctx context.Context) error { return llm.m.Drop(ctx) }

func (llm *locklessMigrator) Version(ctx context.Context) (version int, err error) {
return llm.m.Version(ctx)
Expand Down
26 changes: 26 additions & 0 deletions dbump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,32 @@ func TestMigrateRedo(t *testing.T) {
mustEqual(t, mm.log, wantLog)
}

func TestMigrateDrop(t *testing.T) {
currVersion := 3
wantLog := []string{
"lockdb", "init", "getversion",
"dostep", "{v:2 q:'SELECT 30;' notx:false}",
"dostep", "{v:1 q:'SELECT 20;' notx:false}",
"dostep", "{v:0 q:'SELECT 10;' notx:false}",
"drop",
"unlockdb",
}

mm := &MockMigrator{
VersionFn: func(ctx context.Context) (version int, err error) {
return currVersion, nil
},
}
cfg := Config{
Migrator: mm,
Loader: NewSliceLoader(testdataMigrations),
Mode: ModeDrop,
}

failIfErr(t, Run(context.Background(), cfg))
mustEqual(t, mm.log, wantLog)
}

func TestUseForce(t *testing.T) {
currVersion := 3
wantLog := []string{
Expand Down
9 changes: 9 additions & 0 deletions mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type MockMigrator struct {
LockDBFn func(ctx context.Context) error
UnlockDBFn func(ctx context.Context) error
InitFn func(ctx context.Context) error
DropFn func(ctx context.Context) error
VersionFn func(ctx context.Context) (version int, err error)
DoStepFn func(ctx context.Context, step Step) error
}
Expand Down Expand Up @@ -41,6 +42,14 @@ func (mm *MockMigrator) Init(ctx context.Context) error {
return mm.InitFn(ctx)
}

func (mm *MockMigrator) Drop(ctx context.Context) error {
mm.log = append(mm.log, "drop")
if mm.DropFn == nil {
return nil
}
return mm.DropFn(ctx)
}

func (mm *MockMigrator) Version(ctx context.Context) (version int, err error) {
mm.log = append(mm.log, "getversion")
if mm.VersionFn == nil {
Expand Down

0 comments on commit 8e4dd43

Please sign in to comment.