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

Fix 88, down with catchup #235

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
23 changes: 20 additions & 3 deletions migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,19 @@ type PlannedMigration struct {
Queries []string
}

// isUpQuery returns true if the query is an "up" query.
func (m PlannedMigration) isUpQuery() bool {
if len(m.Up) != len(m.Queries) {
return false
}
for i, query := range m.Up {
if query != m.Queries[i] {
return false
}
}
return true
}

type byId []*Migration

func (b byId) Len() int { return len(b) }
Expand Down Expand Up @@ -505,8 +518,12 @@ func (ms MigrationSet) applyMigrations(dir MigrationDirection, migrations []*Pla
}
}

switch dir {
case Up:
switch {
// case when we are migrating down, but a migration up was not applied.
// See MigrationSet.planMigrationCommon and the ToCatchup call.
case dir == Down && migration.isUpQuery():
fallthrough
case dir == Up:
err = executor.Insert(&MigrationRecord{
Id: migration.Id,
AppliedAt: time.Now(),
Expand All @@ -518,7 +535,7 @@ func (ms MigrationSet) applyMigrations(dir MigrationDirection, migrations []*Pla

return applied, newTxError(migration, err)
}
case Down:
case dir == Down:
_, err := executor.Delete(&MigrationRecord{
Id: migration.Id,
})
Expand Down
54 changes: 54 additions & 0 deletions migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,60 @@ func (s *SqliteMigrateSuite) TestPlanMigrationWithHoles(c *C) {
c.Assert(plannedMigrations[2].Queries[0], Equals, down)
}

func (s *SqliteMigrateSuite) TestPlanMigrationWithHolesThenCatchupOnDown(c *C) {
up := "SELECT 0"
down := "SELECT 1"
migrations := &MemoryMigrationSource{
Migrations: []*Migration{
{
Id: "1",
Up: []string{up},
Down: []string{down},
},
{
Id: "3",
Up: []string{up},
Down: []string{down},
},
},
}

// apply #1 and #3
n, err := Exec(s.Db, "sqlite3", migrations, Up)
c.Assert(err, IsNil)
c.Assert(n, Equals, 2)

records, err := GetMigrationRecords(s.Db, "sqlite3")
c.Assert(err, IsNil)
c.Assert(records, HasLen, 2)

// add #2 (a missing migration up)
migrations.Migrations = append(migrations.Migrations, &Migration{
Id: "2",
Up: []string{up},
Down: []string{down},
})

// going down of 1 operation (3), will apply #2 also.
plannedMigrations, _, err := PlanMigration(s.Db, "sqlite3", migrations, Down, 1)
c.Assert(err, IsNil)
c.Assert(plannedMigrations, HasLen, 2)
c.Assert(plannedMigrations[0].Migration.Id, Equals, "2")
c.Assert(plannedMigrations[0].Queries[0], Equals, up)
c.Assert(plannedMigrations[1].Migration.Id, Equals, "3")
c.Assert(plannedMigrations[1].Queries[0], Equals, down)

// exec down of 1
n, err = ExecMax(s.Db, "sqlite3", migrations, Down, 1)
c.Assert(err, IsNil)
c.Assert(n, Equals, 2)

// should be 2: #1 do not change, #2 is applied by catchup, #3 is down
records, err = GetMigrationRecords(s.Db, "sqlite3")
c.Assert(err, IsNil)
c.Assert(records, HasLen, 2)
}

func (s *SqliteMigrateSuite) TestLess(c *C) {
c.Assert((Migration{Id: "1"}).Less(&Migration{Id: "2"}), Equals, true) // 1 less than 2
c.Assert((Migration{Id: "2"}).Less(&Migration{Id: "1"}), Equals, false) // 2 not less than 1
Expand Down