Skip to content

Commit dc45ef7

Browse files
committed
fix: work around MySQL implicit commits
1 parent eecacbe commit dc45ef7

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

sqlx-core/src/mysql/migrate.rs

+37-1
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,17 @@ CREATE TABLE IF NOT EXISTS _sqlx_migrations (
217217
let mut tx = self.begin().await?;
218218
let start = Instant::now();
219219

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

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

241+
// language=MySQL
242+
let _ = query(
243+
r#"
244+
UPDATE _sqlx_migrations
245+
SET success = TRUE
246+
WHERE version = ?
247+
"#,
248+
)
249+
.bind(migration.version)
250+
.execute(&mut tx)
251+
.await?;
252+
235253
tx.commit().await?;
236254

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

287+
// For MySQL we cannot really isolate migrations due to implicit commits caused by table modification, see
288+
// https://dev.mysql.com/doc/refman/8.0/en/implicit-commit.html
289+
//
290+
// To somewhat try to detect this, we first insert the migration into the migration table with
291+
// `success=FALSE` and later remove the migration altogether.
292+
//
293+
// language=MySQL
294+
let _ = query(
295+
r#"
296+
UPDATE _sqlx_migrations
297+
SET success = FALSE
298+
WHERE version = ?
299+
"#,
300+
)
301+
.bind(migration.version)
302+
.execute(&mut tx)
303+
.await?;
304+
269305
tx.execute(&*migration.sql).await?;
270306

271307
// language=SQL

0 commit comments

Comments
 (0)