Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.
Rust ai-agentsai-assistantai-botautomationbotbots-teamchatbotclaude-duetgrokgrok-botgrok-bot-alternativeharnessllmmulti-agentopenai-dotsopenbotpersonal-agentrustself-hostedselfhosted-ai
DB: run migrations on one connection, then open the runtime pool
Lin (kwant bot)1f50ba9366fa
diff --git a/src/db/mod.rs b/src/db/mod.rsindex c4cda901..53f76ddf 100644--- a/src/db/mod.rs+++ b/src/db/mod.rs@@ -95,8 +95,14 @@ impl Database {let fresh = !db_path.exists();let conn_str = format!("sqlite://{}?mode=rwc", db_path.to_string_lossy());+ // Migrations run on one connection, closed before the runtime pool opens. A connection+ // whose schema was loaded before another one altered a table prepares `SELECT *` from+ // the old schema; SQLite re-prepares it at the first step with the new column count,+ // and sqlx reads the row with the old column list (an index-out-of-bounds panic, then+ // RowNotFound). With one connection every statement sees every earlier change, and the+ // runtime pool's connections all start from the finished schema.let pool = SqlitePoolOptions::new()- .max_connections(10)+ .max_connections(1).connect(&conn_str).await?;@@ -139,8 +145,14 @@ impl Database {db.migrate_hires().await?;db.migrate_imports().await?;}+ db.pool.close().await;- Ok(db)+ let pool = SqlitePoolOptions::new()+ .max_connections(10)+ .connect(&conn_str)+ .await?;+ sqlx::query("PRAGMA synchronous = NORMAL;").execute(&pool).await?;+ Ok(Self { pool })}async fn seed_defaults(&self) -> Result<(), DbError> {diff --git a/tests/custom_api_migration_test.rs b/tests/custom_api_migration_test.rsindex 1822df8c..7724b901 100644--- a/tests/custom_api_migration_test.rs+++ b/tests/custom_api_migration_test.rs@@ -127,3 +127,34 @@ async fn a_failed_change_rolls_back_and_keeps_running_on_version_1() {let db = Database::init(tmp.path()).await.unwrap();assert_eq!(db.schema_version().await.unwrap(), CUSTOM_API_SCHEMA_VERSION);}++/// Every connection the server reads with sees the migrated schema. Before the fix the+/// migrations ran on the server's own 10-connection pool: a connection whose schema was loaded+/// before another one added `space_id` prepared `SELECT *` with the old 15 columns, SQLite+/// re-prepared it with 16 at the first step, and sqlx panicked on its worker thread+/// (`index out of bounds: the len is 15 but the index is 15`) and returned RowNotFound.+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]+async fn every_connection_reads_the_migrated_schema() {+ for _ in 0..5 {+ let tmp = tempfile::tempdir().unwrap();+ legacy(tmp.path()).await;+ let db = Database::init(tmp.path()).await.expect("migrates the legacy database");+ let space = kwnt::db::spaces::SpaceId::default_space();+ let ids: Vec<String> = db.list_kwants(&space).await.unwrap().into_iter().map(|k| k.id).collect();+ assert!(!ids.is_empty());+ // More readers than connections, so every pooled connection serves `SELECT *` reads+ let reads = (0..40).map(|i| {+ let (db, space, id) = (db.clone(), space.clone(), ids[i % ids.len()].clone());+ tokio::spawn(async move {+ assert!(db.get_kwant(&id).await.unwrap().is_some());+ assert!(!db.list_kwants(&space).await.unwrap().is_empty());+ db.list_channels(&space).await.unwrap();+ db.list_routines(&space).await.unwrap();+ db.list_profiles(&space).await.unwrap();+ })+ });+ for read in reads.collect::<Vec<_>>() {+ read.await.expect("no read sees a stale column list");+ }+ }+}