Skip to content

Commit 16b63cb

Browse files
committed
fix: ensure reverse migration progress is not lost for mysql
See #1966.
1 parent 43a101a commit 16b63cb

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

sqlx-core/src/mysql/migrate.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,18 +261,23 @@ CREATE TABLE IF NOT EXISTS _sqlx_migrations (
261261
migration: &'m Migration,
262262
) -> BoxFuture<'m, Result<Duration, MigrateError>> {
263263
Box::pin(async move {
264+
// Use a single transaction for the actual migration script and the essential bookeeping so we never
265+
// execute migrations twice. See https://github.com/launchbadge/sqlx/issues/1966.
266+
let mut tx = self.begin().await?;
264267
let start = Instant::now();
265268

266-
self.execute(&*migration.sql).await?;
267-
268-
let elapsed = start.elapsed();
269+
tx.execute(&*migration.sql).await?;
269270

270271
// language=SQL
271272
let _ = query(r#"DELETE FROM _sqlx_migrations WHERE version = ?"#)
272273
.bind(migration.version)
273-
.execute(self)
274+
.execute(&mut tx)
274275
.await?;
275276

277+
tx.commit().await?;
278+
279+
let elapsed = start.elapsed();
280+
276281
Ok(elapsed)
277282
})
278283
}

0 commit comments

Comments
 (0)