Skip to content

Commit 298319d

Browse files
author
Qinghua Chen
committed
add ApplyMigrationToVersion
1 parent 04d833d commit 298319d

4 files changed

Lines changed: 83 additions & 64 deletions

File tree

migrate.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -459,15 +459,15 @@ func ExecVersion(db *sql.DB, dialect string, m MigrationSource, dir MigrationDir
459459

460460
// Returns the number of applied migrations.
461461
func (ms MigrationSet) ExecMax(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, max int) (int, error) {
462-
migrations, dbMap, err := ms.PlanMigration(db, dialect, m, dir, max, -1)
462+
migrations, dbMap, err := ms.PlanMigration(db, dialect, m, dir, max)
463463
if err != nil {
464464
return 0, err
465465
}
466466
return ms.applyMigrations(dir, migrations, dbMap)
467467
}
468468

469469
func (ms MigrationSet) ExecVersion(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, version int64) (int, error) {
470-
migrations, dbMap, err := ms.PlanMigration(db, dialect, m, dir, 0, version)
470+
migrations, dbMap, err := ms.PlanMigrationToVersion(db, dialect, m, dir, version)
471471
if err != nil {
472472
return 0, err
473473
}
@@ -544,11 +544,24 @@ func (ms MigrationSet) applyMigrations(dir MigrationDirection, migrations []*Pla
544544
}
545545

546546
// Plan a migration.
547-
func PlanMigration(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, max int, version int64) ([]*PlannedMigration, *gorp.DbMap, error) {
548-
return migSet.PlanMigration(db, dialect, m, dir, max, version)
547+
func PlanMigration(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, max int) ([]*PlannedMigration, *gorp.DbMap, error) {
548+
return migSet.PlanMigration(db, dialect, m, dir, max)
549549
}
550550

551-
func (ms MigrationSet) PlanMigration(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, max int, version int64) ([]*PlannedMigration, *gorp.DbMap, error) {
551+
// Plan a migration to version.
552+
func PlanMigrationToVersion(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, version int64) ([]*PlannedMigration, *gorp.DbMap, error) {
553+
return migSet.PlanMigrationToVersion(db, dialect, m, dir, version)
554+
}
555+
556+
func (ms MigrationSet) PlanMigration(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, max int) ([]*PlannedMigration, *gorp.DbMap, error) {
557+
return ms.planMigration(db, dialect, m, dir, max, -1)
558+
}
559+
560+
func (ms MigrationSet) PlanMigrationToVersion(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, version int64) ([]*PlannedMigration, *gorp.DbMap, error) {
561+
return ms.planMigration(db, dialect, m, dir, 0, version)
562+
}
563+
564+
func (ms MigrationSet) planMigration(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, max int, version int64) ([]*PlannedMigration, *gorp.DbMap, error) {
552565
dbMap, err := ms.getMigrationDbMap(db, dialect)
553566
if err != nil {
554567
return nil, nil, err
@@ -651,7 +664,7 @@ func (ms MigrationSet) PlanMigration(db *sql.DB, dialect string, m MigrationSour
651664
//
652665
// Returns the number of skipped migrations.
653666
func SkipMax(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, max int) (int, error) {
654-
migrations, dbMap, err := PlanMigration(db, dialect, m, dir, max, -1)
667+
migrations, dbMap, err := PlanMigration(db, dialect, m, dir, max)
655668
if err != nil {
656669
return 0, err
657670
}

migrate_test.go

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func (s *SqliteMigrateSuite) TestMigrateVersionInt2(c *C) {
238238
c.Assert(id, Equals, int64(1))
239239
}
240240

241-
func (s *SqliteMigrateSuite) TestMigrateVersionIntFailed(c *C) {
241+
func (s *SqliteMigrateSuite) TestMigrateVersionIntFailedWithNotExistingVerion(c *C) {
242242
migrations := &FileMigrationSource{
243243
Dir: "test-migrations",
244244
}
@@ -248,7 +248,7 @@ func (s *SqliteMigrateSuite) TestMigrateVersionIntFailed(c *C) {
248248
c.Assert(err, NotNil)
249249
}
250250

251-
func (s *SqliteMigrateSuite) TestMigrateVersionIntFailed2(c *C) {
251+
func (s *SqliteMigrateSuite) TestMigrateVersionIntFailedWithInvalidVerion(c *C) {
252252
migrations := &FileMigrationSource{
253253
Dir: "test-migrations",
254254
}
@@ -380,55 +380,12 @@ func (s *SqliteMigrateSuite) TestPlanMigration(c *C) {
380380
Down: []string{"ALTER TABLE people DROP COLUMN middle_name"},
381381
})
382382

383-
plannedMigrations, _, err := PlanMigration(s.Db, "sqlite3", migrations, Up, 0, -1)
383+
plannedMigrations, _, err := PlanMigration(s.Db, "sqlite3", migrations, Up, 0)
384384
c.Assert(err, IsNil)
385385
c.Assert(plannedMigrations, HasLen, 1)
386386
c.Assert(plannedMigrations[0].Migration, Equals, migrations.Migrations[3])
387387

388-
plannedMigrations, _, err = PlanMigration(s.Db, "sqlite3", migrations, Down, 0, -1)
389-
c.Assert(err, IsNil)
390-
c.Assert(plannedMigrations, HasLen, 3)
391-
c.Assert(plannedMigrations[0].Migration, Equals, migrations.Migrations[2])
392-
c.Assert(plannedMigrations[1].Migration, Equals, migrations.Migrations[1])
393-
c.Assert(plannedMigrations[2].Migration, Equals, migrations.Migrations[0])
394-
}
395-
396-
func (s *SqliteMigrateSuite) TestPlanMigrationWithVersion(c *C) {
397-
migrations := &MemoryMigrationSource{
398-
Migrations: []*Migration{
399-
{
400-
Id: "1_create_table.sql",
401-
Up: []string{"CREATE TABLE people (id int)"},
402-
Down: []string{"DROP TABLE people"},
403-
},
404-
{
405-
Id: "2_alter_table.sql",
406-
Up: []string{"ALTER TABLE people ADD COLUMN first_name text"},
407-
Down: []string{"SELECT 0"}, // Not really supported
408-
},
409-
{
410-
Id: "10_add_last_name.sql",
411-
Up: []string{"ALTER TABLE people ADD COLUMN last_name text"},
412-
Down: []string{"ALTER TABLE people DROP COLUMN last_name"},
413-
},
414-
},
415-
}
416-
n, err := Exec(s.Db, "sqlite3", migrations, Up)
417-
c.Assert(err, IsNil)
418-
c.Assert(n, Equals, 3)
419-
420-
migrations.Migrations = append(migrations.Migrations, &Migration{
421-
Id: "11_add_middle_name.sql",
422-
Up: []string{"ALTER TABLE people ADD COLUMN middle_name text"},
423-
Down: []string{"ALTER TABLE people DROP COLUMN middle_name"},
424-
})
425-
426-
plannedMigrations, _, err := PlanMigration(s.Db, "sqlite3", migrations, Up, 0, 11)
427-
c.Assert(err, IsNil)
428-
c.Assert(plannedMigrations, HasLen, 1)
429-
c.Assert(plannedMigrations[0].Migration, Equals, migrations.Migrations[3])
430-
431-
plannedMigrations, _, err = PlanMigration(s.Db, "sqlite3", migrations, Down, 0, 1)
388+
plannedMigrations, _, err = PlanMigration(s.Db, "sqlite3", migrations, Down, 0)
432389
c.Assert(err, IsNil)
433390
c.Assert(plannedMigrations, HasLen, 3)
434391
c.Assert(plannedMigrations[0].Migration, Equals, migrations.Migrations[2])
@@ -514,7 +471,7 @@ func (s *SqliteMigrateSuite) TestPlanMigrationWithHoles(c *C) {
514471
})
515472

516473
// apply all the missing migrations
517-
plannedMigrations, _, err := PlanMigration(s.Db, "sqlite3", migrations, Up, 0, -1)
474+
plannedMigrations, _, err := PlanMigration(s.Db, "sqlite3", migrations, Up, 0)
518475
c.Assert(err, IsNil)
519476
c.Assert(plannedMigrations, HasLen, 3)
520477
c.Assert(plannedMigrations[0].Migration.Id, Equals, "2")
@@ -525,7 +482,7 @@ func (s *SqliteMigrateSuite) TestPlanMigrationWithHoles(c *C) {
525482
c.Assert(plannedMigrations[2].Queries[0], Equals, up)
526483

527484
// first catch up to current target state 123, then migrate down 1 step to 12
528-
plannedMigrations, _, err = PlanMigration(s.Db, "sqlite3", migrations, Down, 1, -1)
485+
plannedMigrations, _, err = PlanMigration(s.Db, "sqlite3", migrations, Down, 1)
529486
c.Assert(err, IsNil)
530487
c.Assert(plannedMigrations, HasLen, 2)
531488
c.Assert(plannedMigrations[0].Migration.Id, Equals, "2")
@@ -534,7 +491,7 @@ func (s *SqliteMigrateSuite) TestPlanMigrationWithHoles(c *C) {
534491
c.Assert(plannedMigrations[1].Queries[0], Equals, down)
535492

536493
// first catch up to current target state 123, then migrate down 2 steps to 1
537-
plannedMigrations, _, err = PlanMigration(s.Db, "sqlite3", migrations, Down, 2, -1)
494+
plannedMigrations, _, err = PlanMigration(s.Db, "sqlite3", migrations, Down, 2)
538495
c.Assert(err, IsNil)
539496
c.Assert(plannedMigrations, HasLen, 3)
540497
c.Assert(plannedMigrations[0].Migration.Id, Equals, "2")
@@ -598,12 +555,12 @@ func (s *SqliteMigrateSuite) TestPlanMigrationWithUnknownDatabaseMigrationApplie
598555
Down: []string{"ALTER TABLE people DROP COLUMN middle_name"},
599556
})
600557

601-
_, _, err = PlanMigration(s.Db, "sqlite3", migrations, Up, 0, -1)
558+
_, _, err = PlanMigration(s.Db, "sqlite3", migrations, Up, 0)
602559
c.Assert(err, NotNil, Commentf("Up migrations should not have been applied when there "+
603560
"is an unknown migration in the database"))
604561
c.Assert(err, FitsTypeOf, &PlanError{})
605562

606-
_, _, err = PlanMigration(s.Db, "sqlite3", migrations, Down, 0, -1)
563+
_, _, err = PlanMigration(s.Db, "sqlite3", migrations, Down, 0)
607564
c.Assert(err, NotNil, Commentf("Down migrations should not have been applied when there "+
608565
"is an unknown migration in the database"))
609566
c.Assert(err, FitsTypeOf, &PlanError{})
@@ -642,14 +599,57 @@ func (s *SqliteMigrateSuite) TestPlanMigrationWithIgnoredUnknownDatabaseMigratio
642599
Down: []string{"ALTER TABLE people DROP COLUMN middle_name"},
643600
})
644601

645-
_, _, err = PlanMigration(s.Db, "sqlite3", migrations, Up, 0, -1)
602+
_, _, err = PlanMigration(s.Db, "sqlite3", migrations, Up, 0)
646603
c.Assert(err, IsNil)
647604

648-
_, _, err = PlanMigration(s.Db, "sqlite3", migrations, Down, 0, -1)
605+
_, _, err = PlanMigration(s.Db, "sqlite3", migrations, Down, 0)
649606
c.Assert(err, IsNil)
650607
SetIgnoreUnknown(false) // Make sure we are not breaking other tests as this is globaly set
651608
}
652609

610+
func (s *SqliteMigrateSuite) TestPlanMigrationToVersion(c *C) {
611+
migrations := &MemoryMigrationSource{
612+
Migrations: []*Migration{
613+
{
614+
Id: "1_create_table.sql",
615+
Up: []string{"CREATE TABLE people (id int)"},
616+
Down: []string{"DROP TABLE people"},
617+
},
618+
{
619+
Id: "2_alter_table.sql",
620+
Up: []string{"ALTER TABLE people ADD COLUMN first_name text"},
621+
Down: []string{"SELECT 0"}, // Not really supported
622+
},
623+
{
624+
Id: "10_add_last_name.sql",
625+
Up: []string{"ALTER TABLE people ADD COLUMN last_name text"},
626+
Down: []string{"ALTER TABLE people DROP COLUMN last_name"},
627+
},
628+
},
629+
}
630+
n, err := Exec(s.Db, "sqlite3", migrations, Up)
631+
c.Assert(err, IsNil)
632+
c.Assert(n, Equals, 3)
633+
634+
migrations.Migrations = append(migrations.Migrations, &Migration{
635+
Id: "11_add_middle_name.sql",
636+
Up: []string{"ALTER TABLE people ADD COLUMN middle_name text"},
637+
Down: []string{"ALTER TABLE people DROP COLUMN middle_name"},
638+
})
639+
640+
plannedMigrations, _, err := PlanMigrationToVersion(s.Db, "sqlite3", migrations, Up, 11)
641+
c.Assert(err, IsNil)
642+
c.Assert(plannedMigrations, HasLen, 1)
643+
c.Assert(plannedMigrations[0].Migration, Equals, migrations.Migrations[3])
644+
645+
plannedMigrations, _, err = PlanMigrationToVersion(s.Db, "sqlite3", migrations, Down, 1)
646+
c.Assert(err, IsNil)
647+
c.Assert(plannedMigrations, HasLen, 3)
648+
c.Assert(plannedMigrations[0].Migration, Equals, migrations.Migrations[2])
649+
c.Assert(plannedMigrations[1].Migration, Equals, migrations.Migrations[1])
650+
c.Assert(plannedMigrations[2].Migration, Equals, migrations.Migrations[0])
651+
}
652+
653653
// TestExecWithUnknownMigrationInDatabase makes sure that problems found with planning the
654654
// migrations are propagated and returned by Exec.
655655
func (s *SqliteMigrateSuite) TestExecWithUnknownMigrationInDatabase(c *C) {

sql-migrate/command_common.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@ func ApplyMigrations(dir migrate.MigrationDirection, dryrun bool, limit int, ver
2323
}
2424

2525
if dryrun {
26-
migrations, _, err := migrate.PlanMigration(db, dialect, source, dir, limit, version)
26+
var migrations []*migrate.PlannedMigration
27+
28+
if version >= 0 {
29+
migrations, _, err = migrate.PlanMigrationToVersion(db, dialect, source, dir, version)
30+
} else {
31+
migrations, _, err = migrate.PlanMigration(db, dialect, source, dir, limit)
32+
}
33+
2734
if err != nil {
2835
return fmt.Errorf("Cannot plan migration: %s", err)
2936
}
@@ -33,9 +40,8 @@ func ApplyMigrations(dir migrate.MigrationDirection, dryrun bool, limit int, ver
3340
}
3441
} else {
3542
var n int
36-
var err error
3743

38-
if version > 0 {
44+
if version >= 0 {
3945
n, err = migrate.ExecVersion(db, dialect, source, dir, version)
4046
} else {
4147
n, err = migrate.ExecMax(db, dialect, source, dir, limit)

sql-migrate/command_redo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (c *RedoCommand) Run(args []string) int {
6060
Dir: env.Dir,
6161
}
6262

63-
migrations, _, err := migrate.PlanMigration(db, dialect, source, migrate.Down, 1, -1)
63+
migrations, _, err := migrate.PlanMigration(db, dialect, source, migrate.Down, 1)
6464
if err != nil {
6565
ui.Error(fmt.Sprintf("Migration (redo) failed: %v", err))
6666
return 1

0 commit comments

Comments
 (0)