mirror of
https://github.com/openai/codex.git
synced 2026-11-23 23:19:04 +00:00
Centralize SQLite connection configuration (#33938)
## What changed - Add `SqliteConfig` as the shared entry point for read-write and read-only SQLite pools. - Apply consistent WAL, synchronization, auto-vacuum, busy-timeout, logging, and pool-size settings to writable Codex databases. - Route state runtime, audit, CLI, and test database connections through the shared configuration. ## Testing - Run migration tests against temporary on-disk databases opened through `SqliteConfig`, including the concurrent-writer repair case. GitOrigin-RevId: e3946b98bde04c47574532ac8b1a7bb2b03edd97
This commit is contained in:
1 parent
23899f7cb6
commit
6bd3f5e3db
14 files changed
+220
-166
No files matched your search
@@ -1,18 +1,12 @@
|
||||
use std::borrow::Cow;
|
||||
use std::time::Duration;
|
||||
|
||||
use sqlx::Connection;
|
||||
use sqlx::Row;
|
||||
use sqlx::SqliteConnection;
|
||||
use sqlx::migrate::Migration;
|
||||
use sqlx::migrate::Migrator;
|
||||
use sqlx::sqlite::SqliteConnectOptions;
|
||||
use sqlx::sqlite::SqliteJournalMode;
|
||||
use sqlx::sqlite::SqlitePoolOptions;
|
||||
use uuid::Uuid;
|
||||
use std::borrow::Cow;
|
||||
|
||||
use super::STATE_MIGRATOR;
|
||||
use super::repair_legacy_recency_migration_version;
|
||||
use crate::state_db_path;
|
||||
|
||||
fn migrator_through(version: i64) -> Migrator {
|
||||
Migrator {
|
||||
@@ -34,11 +28,18 @@ fn migrator_through(version: i64) -> Migrator {
|
||||
|
||||
#[tokio::test]
|
||||
async fn recency_migration_backfills_and_seeds_old_binary_inserts() {
|
||||
let pool = SqlitePoolOptions::new()
|
||||
.max_connections(1)
|
||||
.connect("sqlite::memory:")
|
||||
let sqlite_home = crate::runtime::test_support::unique_temp_dir();
|
||||
tokio::fs::create_dir_all(&sqlite_home)
|
||||
.await
|
||||
.expect("in-memory database should open");
|
||||
.expect("sqlite home should be created");
|
||||
let _cleanup = scopeguard::guard(sqlite_home.clone(), |sqlite_home| {
|
||||
let _ = std::fs::remove_dir_all(sqlite_home);
|
||||
});
|
||||
let sqlite = crate::SqliteConfig::new_for_testing(sqlite_home.clone());
|
||||
let pool = sqlite
|
||||
.open_read_write_pool(&state_db_path(&sqlite_home))
|
||||
.await
|
||||
.expect("sqlite database should open");
|
||||
migrator_through(/*version*/ 37)
|
||||
.run(&pool)
|
||||
.await
|
||||
@@ -134,15 +135,24 @@ INSERT INTO threads (
|
||||
.expect("old-binary row should load");
|
||||
assert_eq!(seeded.get::<i64, _>("recency_at"), 1_700_000_300);
|
||||
assert_eq!(seeded.get::<i64, _>("recency_at_ms"), 1_700_000_300_456);
|
||||
|
||||
pool.close().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn repairs_recency_migration_that_was_applied_as_version_38() {
|
||||
let pool = SqlitePoolOptions::new()
|
||||
.max_connections(1)
|
||||
.connect("sqlite::memory:")
|
||||
let sqlite_home = crate::runtime::test_support::unique_temp_dir();
|
||||
tokio::fs::create_dir_all(&sqlite_home)
|
||||
.await
|
||||
.expect("in-memory database should open");
|
||||
.expect("sqlite home should be created");
|
||||
let _cleanup = scopeguard::guard(sqlite_home.clone(), |sqlite_home| {
|
||||
let _ = std::fs::remove_dir_all(sqlite_home);
|
||||
});
|
||||
let sqlite = crate::SqliteConfig::new_for_testing(sqlite_home.clone());
|
||||
let pool = sqlite
|
||||
.open_read_write_pool(&state_db_path(&sqlite_home))
|
||||
.await
|
||||
.expect("sqlite database should open");
|
||||
migrator_through(/*version*/ 37)
|
||||
.run(&pool)
|
||||
.await
|
||||
@@ -201,44 +211,47 @@ async fn repairs_recency_migration_that_was_applied_as_version_38() {
|
||||
.map(|migration| (migration.version, migration.checksum.to_vec()))
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(applied, expected);
|
||||
|
||||
pool.close().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn repair_recency_migration_succeeds_while_another_connection_holds_writer_slot() {
|
||||
let database_path = std::env::temp_dir().join(format!(
|
||||
"codex-state-migrations-test-{}.sqlite",
|
||||
Uuid::new_v4()
|
||||
));
|
||||
let options = SqliteConnectOptions::new()
|
||||
.filename(&database_path)
|
||||
.create_if_missing(true)
|
||||
.journal_mode(SqliteJournalMode::Wal)
|
||||
.busy_timeout(Duration::from_millis(100));
|
||||
let pool = SqlitePoolOptions::new()
|
||||
.max_connections(1)
|
||||
.connect_with(options.clone())
|
||||
let sqlite_home = crate::runtime::test_support::unique_temp_dir();
|
||||
tokio::fs::create_dir_all(&sqlite_home)
|
||||
.await
|
||||
.expect("sqlite home should be created");
|
||||
let _cleanup = scopeguard::guard(sqlite_home.clone(), |sqlite_home| {
|
||||
let _ = std::fs::remove_dir_all(sqlite_home);
|
||||
});
|
||||
let sqlite = crate::SqliteConfig::new_for_testing(sqlite_home.clone());
|
||||
let state_path = state_db_path(&sqlite_home);
|
||||
let pool = sqlite
|
||||
.open_read_write_pool(&state_path)
|
||||
.await
|
||||
.expect("database should open");
|
||||
STATE_MIGRATOR
|
||||
.run(&pool)
|
||||
.await
|
||||
.expect("current migrations should apply");
|
||||
let mut write_connection = SqliteConnection::connect_with(&options)
|
||||
let read_pool = sqlite
|
||||
.open_read_only_pool(&state_path)
|
||||
.await
|
||||
.expect("write connection should open");
|
||||
.expect("read-only pool should open");
|
||||
let mut write_connection = pool.acquire().await.expect("write connection should open");
|
||||
let write_transaction = write_connection
|
||||
.begin_with("BEGIN IMMEDIATE")
|
||||
.await
|
||||
.expect("write transaction should acquire the writer slot");
|
||||
|
||||
let repair_result = repair_legacy_recency_migration_version(&pool, &STATE_MIGRATOR).await;
|
||||
let repair_result = repair_legacy_recency_migration_version(&read_pool, &STATE_MIGRATOR).await;
|
||||
|
||||
write_transaction
|
||||
.rollback()
|
||||
.await
|
||||
.expect("write transaction should roll back");
|
||||
drop(write_connection);
|
||||
read_pool.close().await;
|
||||
pool.close().await;
|
||||
let _ = tokio::fs::remove_file(database_path).await;
|
||||
repair_result.expect("current migration history should not need the writer slot");
|
||||
}
|
||||
Reference in new issue
Block a user