File tree 1 file changed +9
-4
lines changed
1 file changed +9
-4
lines changed Original file line number Diff line number Diff line change @@ -261,18 +261,23 @@ CREATE TABLE IF NOT EXISTS _sqlx_migrations (
261
261
migration : & ' m Migration ,
262
262
) -> BoxFuture < ' m , Result < Duration , MigrateError > > {
263
263
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 ?;
264
267
let start = Instant :: now ( ) ;
265
268
266
- self . execute ( & * migration. sql ) . await ?;
267
-
268
- let elapsed = start. elapsed ( ) ;
269
+ tx. execute ( & * migration. sql ) . await ?;
269
270
270
271
// language=SQL
271
272
let _ = query ( r#"DELETE FROM _sqlx_migrations WHERE version = ?"# )
272
273
. bind ( migration. version )
273
- . execute ( self )
274
+ . execute ( & mut tx )
274
275
. await ?;
275
276
277
+ tx. commit ( ) . await ?;
278
+
279
+ let elapsed = start. elapsed ( ) ;
280
+
276
281
Ok ( elapsed)
277
282
} )
278
283
}
You can’t perform that action at this time.
0 commit comments