Skip to content

Commit c52e129

Browse files
DosenpfandMarkus Gasser
andauthored
fix(sqlite) Migrate revert with no-transaction (#4024)
* Fix migration reverts for no-TX SQLite * Add regression test --------- Co-authored-by: Markus Gasser <markus.gasser@frauscher.com>
1 parent 8152689 commit c52e129

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

sqlx-sqlite/src/migrate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ CREATE TABLE IF NOT EXISTS {table_name} (
207207
let start = Instant::now();
208208

209209
if migration.no_tx {
210-
execute_migration(self, table_name, migration).await?;
210+
revert_migration(self, table_name, migration).await?;
211211
} else {
212212
// Use a single transaction for the actual migration script and the essential bookkeeping so we never
213213
// execute migrations twice. See https://github.com/launchbadge/sqlx/issues/1966.

tests/sqlite/migrate.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,29 @@ async fn no_tx(mut conn: PoolConnection<Sqlite>) -> anyhow::Result<()> {
7777
Ok(())
7878
}
7979

80+
#[sqlx::test(migrations = false)]
81+
async fn no_tx_reversible(mut conn: PoolConnection<Sqlite>) -> anyhow::Result<()> {
82+
clean_up(&mut conn).await?;
83+
84+
let migrator = Migrator::new(Path::new("tests/sqlite/migrations_no_tx_reversible")).await?;
85+
86+
// run migration
87+
migrator.run(&mut conn).await?;
88+
89+
// check outcome
90+
let res: String = conn.fetch_one("PRAGMA JOURNAL_MODE").await?.get(0);
91+
assert_eq!(res, "wal".to_string());
92+
93+
// roll back
94+
migrator.undo(&mut conn, -1).await?;
95+
96+
// check outcome
97+
let res: String = conn.fetch_one("PRAGMA JOURNAL_MODE").await?.get(0);
98+
assert_eq!(res, "delete".to_string());
99+
100+
Ok(())
101+
}
102+
80103
/// Ensure that we have a clean initial state.
81104
async fn clean_up(conn: &mut SqliteConnection) -> anyhow::Result<()> {
82105
conn.execute("DROP TABLE migrations_simple_test").await.ok();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- no-transaction
2+
3+
PRAGMA JOURNAL_MODE = DELETE;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- no-transaction
2+
3+
PRAGMA JOURNAL_MODE = WAL;

0 commit comments

Comments
 (0)