-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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: ensure migration progress is not lost for PG, mysql and sqlite #1991
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4d6fc17
fix: ensure migration progress is not lost for PG
crepererum 17b99c5
fix: ensure migration progress is not lost for sqlite
crepererum 8138065
fix: ensure reverse migration progress is not lost for PG
crepererum 7c94461
fix: ensure reverse migration progress is not lost for sqlite
crepererum d1bffe7
fix: ensure migration progress is not lost for mysql
crepererum aebacfd
fix: ensure reverse migration progress is not lost for mysql
crepererum 9030249
test: check migration type as well
crepererum 682ceea
test: extend migrations testing
crepererum 9b0c27f
fix: work around MySQL implicit commits
crepererum fd24e8f
refactor: simplify migration testing
crepererum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,29 @@ | ||
use sqlx::migrate::Migrator; | ||
use std::path::Path; | ||
|
||
static EMBEDDED: Migrator = sqlx::migrate!("tests/migrate/migrations"); | ||
static EMBEDDED_SIMPLE: Migrator = sqlx::migrate!("tests/migrate/migrations_simple"); | ||
static EMBEDDED_REVERSIBLE: Migrator = sqlx::migrate!("tests/migrate/migrations_reversible"); | ||
|
||
#[sqlx_macros::test] | ||
async fn same_output() -> anyhow::Result<()> { | ||
let runtime = Migrator::new(Path::new("tests/migrate/migrations")).await?; | ||
let runtime_simple = Migrator::new(Path::new("tests/migrate/migrations_simple")).await?; | ||
let runtime_reversible = | ||
Migrator::new(Path::new("tests/migrate/migrations_reversible")).await?; | ||
|
||
assert_eq!(runtime.migrations.len(), EMBEDDED.migrations.len()); | ||
assert_same(&EMBEDDED_SIMPLE, &runtime_simple); | ||
assert_same(&EMBEDDED_REVERSIBLE, &runtime_reversible); | ||
|
||
for (e, r) in EMBEDDED.iter().zip(runtime.iter()) { | ||
Ok(()) | ||
} | ||
|
||
fn assert_same(embedded: &Migrator, runtime: &Migrator) { | ||
assert_eq!(runtime.migrations.len(), embedded.migrations.len()); | ||
|
||
for (e, r) in embedded.iter().zip(runtime.iter()) { | ||
assert_eq!(e.version, r.version); | ||
assert_eq!(e.description, r.description); | ||
assert_eq!(e.migration_type, r.migration_type); | ||
assert_eq!(e.sql, r.sql); | ||
assert_eq!(e.checksum, r.checksum); | ||
} | ||
|
||
Ok(()) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
tests/migrate/migrations_reversible/20220721124650_add_table.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE migrations_reversible_test; |
7 changes: 7 additions & 0 deletions
7
tests/migrate/migrations_reversible/20220721124650_add_table.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CREATE TABLE migrations_reversible_test ( | ||
some_id BIGINT NOT NULL PRIMARY KEY, | ||
some_payload BIGINT NOT NUll | ||
); | ||
|
||
INSERT INTO migrations_reversible_test (some_id, some_payload) | ||
VALUES (1, 100); |
2 changes: 2 additions & 0 deletions
2
tests/migrate/migrations_reversible/20220721125033_modify_column.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
UPDATE migrations_reversible_test | ||
SET some_payload = some_payload - 1; |
2 changes: 2 additions & 0 deletions
2
tests/migrate/migrations_reversible/20220721125033_modify_column.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
UPDATE migrations_reversible_test | ||
SET some_payload = some_payload + 1; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
MySQL is a problem case because any DDL statements like
CREATE TABLE
implicitly commit any open transaction: https://dev.mysql.com/doc/refman/5.6/en/implicit-commit.htmlhttps://dev.mysql.com/doc/refman/8.0/en/implicit-commit.html
There's no real way we can actually get any isolation with MySQL. All the user can really do is either manually reverse the changes or restore from a backup.
What we probably should be doing is inserting into
_sqlx_migrations
first withsuccess = FALSE
and then update it if we successfully apply the 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.
ah, this is why this construct was there, I was wondering why the mysql code looked so much different. I'll apply your suggestion and add some doc comment explaining it (including the links you've provided).
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.
Applied. However I needed a similar trick for the reverse direction and there I first toggle the flag from true to false and then delete the entry (have a look at the code), let me know if that makes sense to you.