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: ensure migration progress is not lost for PG, mysql and sqlite #1991

Merged
merged 10 commits into from
Sep 13, 2022

Conversation

crepererum
Copy link
Contributor

Fixes #1966.

The same issue existed for other database types, so I've fixed them as well. I couldn't find any runtime tests for migrations yet, so I've extended the migrations testing code to include some.

@crepererum crepererum changed the title fix: ensure migration progress is not lost for PG and sqlite fix: ensure migration progress is not lost for PG, mysql and sqlite Jul 21, 2022
@crepererum crepererum force-pushed the crepererum/issue1966 branch 3 times, most recently from a2613f1 to abb3d4d Compare July 28, 2022 10:03
@crepererum
Copy link
Contributor Author

ready to review

// execute migrations twice. See https://github.com/launchbadge/sqlx/issues/1966.
// The `execution_time` however can only be measured for the whole transaction. This value _only_ exists for
// data lineage and debugging reasons, so it is not super important if it is lost. So we initialize it to -1
// and update it once the actual transaction completed.
Copy link
Collaborator

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.html
https://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 with success = FALSE and then update it if we successfully apply the migration.

Copy link
Contributor Author

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).

Copy link
Contributor Author

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.

sqlx-test/src/lib.rs Outdated Show resolved Hide resolved
@crepererum
Copy link
Contributor Author

Will apply the recommendations next week.

@crepererum crepererum force-pushed the crepererum/issue1966 branch from abb3d4d to dc45ef7 Compare August 2, 2022 12:13
@crepererum crepererum requested a review from abonander August 2, 2022 12:26
@crepererum crepererum force-pushed the crepererum/issue1966 branch from dc45ef7 to fd24e8f Compare August 3, 2022 10:46
@crepererum
Copy link
Contributor Author

@abonander any update on this?

@abonander abonander merged commit 5e56da8 into launchbadge:main Sep 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Postgres migrations: incomplete transaction handling
2 participants