Skip to content

Commit 514cd20

Browse files
dixonwilleWill Dixon
authored andcommitted
Add ability to skip unknown migrations in database
1 parent 737decc commit 514cd20

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

migrate.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ type MigrationSet struct {
3232
TableName string
3333
// SchemaName schema that the migration table be referenced.
3434
SchemaName string
35+
// IgnoreUnknown skips the check to see if there is a migration
36+
// ran in the database that is not in MigrationSource
37+
IgnoreUnknown bool
3538
}
3639

3740
var migSet = MigrationSet{}
@@ -101,6 +104,12 @@ func SetSchema(name string) {
101104
}
102105
}
103106

107+
// SetIgnoreUnknown sets the flag that skips database check to see if there is a
108+
// migration in the database that is not in migration source.
109+
func SetIgnoreUnknown(v bool) {
110+
migSet.IgnoreUnknown = v
111+
}
112+
104113
type Migration struct {
105114
Id string
106115
Up []string
@@ -510,13 +519,15 @@ func (ms MigrationSet) PlanMigration(db *sql.DB, dialect string, m MigrationSour
510519

511520
// Make sure all migrations in the database are among the found migrations which
512521
// are to be applied.
513-
migrationsSearch := make(map[string]struct{})
514-
for _, migration := range migrations {
515-
migrationsSearch[migration.Id] = struct{}{}
516-
}
517-
for _, existingMigration := range existingMigrations {
518-
if _, ok := migrationsSearch[existingMigration.Id]; !ok {
519-
return nil, nil, newPlanError(existingMigration, "unknown migration in database")
522+
if !ms.IgnoreUnknown {
523+
migrationsSearch := make(map[string]struct{})
524+
for _, migration := range migrations {
525+
migrationsSearch[migration.Id] = struct{}{}
526+
}
527+
for _, existingMigration := range existingMigrations {
528+
if _, ok := migrationsSearch[existingMigration.Id]; !ok {
529+
return nil, nil, newPlanError(existingMigration, "unknown migration in database")
530+
}
520531
}
521532
}
522533

0 commit comments

Comments
 (0)