@@ -217,11 +217,17 @@ CREATE TABLE IF NOT EXISTS _sqlx_migrations (
217
217
let mut tx = self . begin ( ) . await ?;
218
218
let start = Instant :: now ( ) ;
219
219
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
+ //
220
226
// language=MySQL
221
227
let _ = query (
222
228
r#"
223
229
INSERT INTO _sqlx_migrations ( version, description, success, checksum, execution_time )
224
- VALUES ( ?, ?, TRUE , ?, -1 )
230
+ VALUES ( ?, ?, FALSE , ?, -1 )
225
231
"# ,
226
232
)
227
233
. bind ( migration. version )
@@ -232,6 +238,18 @@ CREATE TABLE IF NOT EXISTS _sqlx_migrations (
232
238
233
239
let _ = tx. execute ( & * migration. sql ) . await ?;
234
240
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
+
235
253
tx. commit ( ) . await ?;
236
254
237
255
// Update `elapsed_time`.
@@ -266,6 +284,24 @@ CREATE TABLE IF NOT EXISTS _sqlx_migrations (
266
284
let mut tx = self . begin ( ) . await ?;
267
285
let start = Instant :: now ( ) ;
268
286
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
+
269
305
tx. execute ( & * migration. sql ) . await ?;
270
306
271
307
// language=SQL
0 commit comments