-
Notifications
You must be signed in to change notification settings - Fork 267
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
Add --strict flag #441
Add --strict flag #441
Changes from 3 commits
eb36c90
68da288
9822a96
d278d6a
dc0e0a1
f912ddf
5b890ff
d9d7ce2
b9e0bdf
d0ded5d
311dc19
d20cf09
cd88906
3323fa5
d896ce2
4b85b89
5d95143
c216c93
9949e88
02d2d7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,8 @@ type DB struct { | |
MigrationsTableName string | ||
// SchemaFile specifies the location for schema.sql file | ||
SchemaFile string | ||
// Ignore out of order pending migrations | ||
Strict bool | ||
// Verbose prints the result of each statement execution | ||
Verbose bool | ||
// WaitBefore will wait for database to become available before running any actions | ||
|
@@ -75,6 +77,7 @@ func New(databaseURL *url.URL) *DB { | |
MigrationsDir: []string{"./db/migrations"}, | ||
MigrationsTableName: "schema_migrations", | ||
SchemaFile: "./db/schema.sql", | ||
Strict: false, | ||
Verbose: false, | ||
WaitBefore: false, | ||
WaitInterval: time.Second, | ||
|
@@ -412,6 +415,13 @@ func (db *DB) FindMigrations() ([]Migration, error) { | |
} | ||
} | ||
|
||
var latestAppliedMigration string | ||
for migrationVersion := range appliedMigrations { | ||
if migrationVersion > latestAppliedMigration { | ||
latestAppliedMigration = migrationVersion | ||
} | ||
dossy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
migrations := []Migration{} | ||
for _, dir := range db.MigrationsDir { | ||
// find filesystem migrations | ||
|
@@ -441,6 +451,10 @@ func (db *DB) FindMigrations() ([]Migration, error) { | |
migration.Applied = true | ||
} | ||
|
||
if db.Strict && !migration.Applied && migration.Version < latestAppliedMigration { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not the right place for this check to live. The check should be implemented in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without an additional db query the check needs access to a list of applied migrations to find the applied migration with the highest version number. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I put the strict version check in |
||
continue | ||
} | ||
|
||
migrations = append(migrations, migration) | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a precedent for duplication with the verbose flag, but it seems like it would be better to define the flag just once
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That could really tidy up the duplicate flag usage. I was already a little annoyed writing that part but I tried to do it similar to the verbose flag. I will change it 👍