Skip to content

Commit

Permalink
fix: work around MySQL implicit commits
Browse files Browse the repository at this point in the history
  • Loading branch information
crepererum committed Aug 2, 2022
1 parent eecacbe commit dc45ef7
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion sqlx-core/src/mysql/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,17 @@ CREATE TABLE IF NOT EXISTS _sqlx_migrations (
let mut tx = self.begin().await?;
let start = Instant::now();

// For MySQL we cannot really isolate migrations due to implicit commits caused by table modification, see
// https://dev.mysql.com/doc/refman/8.0/en/implicit-commit.html
//
// To somewhat try to detect this, we first insert the migration into the migration table with
// `success=FALSE` and later modify the flag.
//
// language=MySQL
let _ = query(
r#"
INSERT INTO _sqlx_migrations ( version, description, success, checksum, execution_time )
VALUES ( ?, ?, TRUE, ?, -1 )
VALUES ( ?, ?, FALSE, ?, -1 )
"#,
)
.bind(migration.version)
Expand All @@ -232,6 +238,18 @@ CREATE TABLE IF NOT EXISTS _sqlx_migrations (

let _ = tx.execute(&*migration.sql).await?;

// language=MySQL
let _ = query(
r#"
UPDATE _sqlx_migrations
SET success = TRUE
WHERE version = ?
"#,
)
.bind(migration.version)
.execute(&mut tx)
.await?;

tx.commit().await?;

// Update `elapsed_time`.
Expand Down Expand Up @@ -266,6 +284,24 @@ CREATE TABLE IF NOT EXISTS _sqlx_migrations (
let mut tx = self.begin().await?;
let start = Instant::now();

// For MySQL we cannot really isolate migrations due to implicit commits caused by table modification, see
// https://dev.mysql.com/doc/refman/8.0/en/implicit-commit.html
//
// To somewhat try to detect this, we first insert the migration into the migration table with
// `success=FALSE` and later remove the migration altogether.
//
// language=MySQL
let _ = query(
r#"
UPDATE _sqlx_migrations
SET success = FALSE
WHERE version = ?
"#,
)
.bind(migration.version)
.execute(&mut tx)
.await?;

tx.execute(&*migration.sql).await?;

// language=SQL
Expand Down

0 comments on commit dc45ef7

Please sign in to comment.