Make SQLite schema migrations safe for concurrent startup (#59015)

## Why

SQLx's SQLite migration lock is a no-op, allowing concurrent database openers to race between schema version checks and migration changes.

## What changed

Run schema checks and migrations in one transaction, retrying SQLite busy errors from a fresh snapshot. Databases with a current schema need only a read lock, including on NFS. Include the error cause chain when the app server reports SQLite initialization failures.

## Testing

Add regression tests for eight concurrent database openers applying each migration once and for reopening a current schema while another connection holds a writer reservation with NFS settings.

GitOrigin-RevId: 38004d58a5c637ac1b2be53d07fd9a4c8efec7ab
This commit is contained in:
Dmitriy Kovalenko
2026-11-03 00:04:40 +00:00
committed by copyberry
parent 2816088b68
commit 9e77fb4ad5
3 changed files with 78 additions and 2 deletions
+61
View File
@@ -8,6 +8,67 @@ use sqlx::sqlite::SqliteConnectOptions;
use sqlx::sqlite::SqliteJournalMode;
use sqlx::sqlite::SqlitePoolOptions;
#[tokio::test]
async fn current_schema_opens_while_another_connection_has_writer_reservation() -> anyhow::Result<()>
{
let home = tempfile::TempDir::new()?;
let sqlite = SqliteConfig::new_for_testing(home.path().abs());
sqlite.is_nfs.set(/*value*/ true)?;
let migrator = crate::migrations::runtime_thread_history_migrator();
let pool = sqlite
.open_thread_history_db(&migrator, /*telemetry_override*/ None)
.await?;
let transaction = pool.begin_with("BEGIN IMMEDIATE").await?;
let reopened = sqlite
.open_thread_history_db(&migrator, /*telemetry_override*/ None)
.await?;
let check: String = sqlx::query_scalar("PRAGMA integrity_check")
.fetch_one(&reopened)
.await?;
assert_eq!(check, "ok");
transaction.rollback().await?;
reopened.close().await;
pool.close().await;
Ok(())
}
#[tokio::test]
async fn concurrent_openers_apply_schema_migrations_once() -> anyhow::Result<()> {
let home = tempfile::TempDir::new()?;
let sqlite = SqliteConfig::new_for_testing(home.path().abs());
sqlite.is_nfs.set(/*value*/ true)?;
let migrator = crate::migrations::runtime_thread_history_migrator();
let mut openers = tokio::task::JoinSet::new();
for _ in 0..8 {
let sqlite = sqlite.clone();
openers.spawn(async move {
sqlite
.open_thread_history_db(
&crate::migrations::runtime_thread_history_migrator(),
/*telemetry_override*/ None,
)
.await
});
}
while let Some(result) = openers.join_next().await {
let pool = result??;
let versions =
sqlx::query_scalar::<_, i64>("SELECT version FROM _sqlx_migrations ORDER BY version")
.fetch_all(&pool)
.await?;
assert_eq!(
versions,
migrator
.iter()
.map(|migration| migration.version)
.collect::<Vec<_>>()
);
pool.close().await;
}
Ok(())
}
#[tokio::test]
async fn configured_log_directory_is_initialized_on_use_and_retries_after_failure()
-> anyhow::Result<()> {