Avoid unnecessary writes during migration repair (#33687)

## Why

Opening a database with current migration history should not require SQLite's
writer slot. The legacy recency repair previously issued an `UPDATE` even when
there was nothing to repair, so it could fail while another connection held the
writer slot.

## What changed

Check `_sqlx_migrations` for the legacy version and checksum before issuing the
repair update, and return early when the current recency migration is already
recorded.

## Testing

Add a WAL-mode regression test that holds the writer slot on a second
connection and verifies that current migration history needs no write access.

GitOrigin-RevId: 76649213912b510df42a836cf8ed7f84a7c70eb5
This commit is contained in:
Zanie Blue authored and copyberry committed 2026-07-16 20:55:13 +00:00
1 parent d3fc1950a9
commit 18110b810f
2 files changed
+67

No files matched your search

+21
View File
@@ -69,6 +69,27 @@ pub(crate) async fn repair_legacy_recency_migration_version(
return Ok(());
}
let legacy_recency_needs_repair = sqlx::query_scalar::<_, i64>(
r#"
SELECT 1
FROM _sqlx_migrations
WHERE version = ?
AND checksum = ?
AND NOT EXISTS (
SELECT 1 FROM _sqlx_migrations WHERE version = ?
)
"#,
)
.bind(38_i64)
.bind(recency_migration.checksum.as_ref())
.bind(recency_migration.version)
.fetch_optional(pool)
.await?
.is_some();
if !legacy_recency_needs_repair {
return Ok(());
}
sqlx::query(
r#"
UPDATE _sqlx_migrations