Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

perf(host-core): replace session updated_at index with composite index

Zihao Pengeff8e658eaf0
diff --git a/crates/host-core/src/db.rs b/crates/host-core/src/db.rs
index 6d984a75a..3b133f677 100644
--- a/crates/host-core/src/db.rs
+++ b/crates/host-core/src/db.rs
@@ -7,7 +7,7 @@ pub(crate) use serde_json::Value;
pub(crate) use std::path::{Path, PathBuf};
/// Current SQLite schema version.
-pub const SCHEMA_VERSION: i64 = 21;
+pub const SCHEMA_VERSION: i64 = 22;
/// Absolute approval deadline for a newly submitted Plan or Goal proposal.
pub const PLAN_APPROVAL_TIMEOUT_MS: i64 = 30 * 60 * 1000;
diff --git a/crates/host-core/src/db/migrations.rs b/crates/host-core/src/db/migrations.rs
index db92ea4a3..cf064fa87 100644
--- a/crates/host-core/src/db/migrations.rs
+++ b/crates/host-core/src/db/migrations.rs
@@ -962,3 +962,25 @@ pub(crate) fn migrate_v20_to_v21(conn: &Connection, path: &Path) -> Result<()> {
})?;
Ok(())
}
+
+pub(crate) fn migrate_v21_to_v22_tx(tx: &rusqlite::Transaction<'_>) -> Result<()> {
+ tx.execute_batch(
+ "DROP INDEX IF EXISTS idx_sessions_updated;
+ CREATE INDEX IF NOT EXISTS idx_sessions_updated_id ON sessions(updated_at DESC, id DESC);"
+ )?;
+ tx.pragma_update(None, "user_version", 22i64)?;
+ Ok(())
+}
+
+pub(crate) fn migrate_v21_to_v22(conn: &Connection, path: &Path) -> Result<()> {
+ let backup = create_migration_backup(conn, path, 21)?;
+ let tx = conn.unchecked_transaction()?;
+ migrate_v21_to_v22_tx(&tx)?;
+ tx.commit().with_context(|| {
+ format!(
+ "commit schema v21 to v22 migration; backup {} remains",
+ backup.display()
+ )
+ })?;
+ Ok(())
+}
diff --git a/crates/host-core/src/db/repositories.rs b/crates/host-core/src/db/repositories.rs
index 32076ba47..5ad6ba12c 100644
--- a/crates/host-core/src/db/repositories.rs
+++ b/crates/host-core/src/db/repositories.rs
@@ -194,6 +194,9 @@ impl Database {
20 => {
migrate_v20_to_v21(&conn, path)?;
}
+ 21 => {
+ crate::db::migrations::migrate_v21_to_v22(&conn, path)?;
+ }
legacy @ 1..=6 => {
let _ = conn.execute_batch("PRAGMA wal_checkpoint(TRUNCATE);");
drop(conn);
@@ -232,6 +235,10 @@ impl Database {
}
if migrated_version == 20 {
migrate_v20_to_v21(&conn, path)?;
+ migrated_version = conn.query_row("PRAGMA user_version", [], |r| r.get(0))?;
+ }
+ if migrated_version == 21 {
+ crate::db::migrations::migrate_v21_to_v22(&conn, path)?;
}
let db = Self { conn, data_dir };
db.boot_maintenance()?;
diff --git a/crates/host-core/src/db/schema.rs b/crates/host-core/src/db/schema.rs
index 1a0aba457..f096a2fca 100644
--- a/crates/host-core/src/db/schema.rs
+++ b/crates/host-core/src/db/schema.rs
@@ -103,7 +103,7 @@ CREATE TABLE sessions (
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
);
-CREATE INDEX idx_sessions_updated ON sessions(updated_at DESC);
+CREATE INDEX idx_sessions_updated_id ON sessions(updated_at DESC, id DESC);
CREATE INDEX idx_sessions_project ON sessions(project_id) WHERE project_id IS NOT NULL;
CREATE INDEX idx_sessions_deleted ON sessions(deleted_at) WHERE deleted_at IS NOT NULL;
diff --git a/crates/host-core/src/db/tests.rs b/crates/host-core/src/db/tests.rs
index b4679290a..90ef57848 100644
--- a/crates/host-core/src/db/tests.rs
+++ b/crates/host-core/src/db/tests.rs
@@ -158,6 +158,8 @@ fn v20_database_migrates_the_session_checklist_with_a_backup() {
DROP TABLE IF EXISTS session_todo;
ALTER TABLE sessions DROP COLUMN todo_revision;
ALTER TABLE sessions DROP COLUMN todo_updated_at;
+ DROP INDEX IF EXISTS idx_sessions_updated_id;
+ CREATE INDEX idx_sessions_updated ON sessions(updated_at DESC);
PRAGMA user_version = 20;",
)
.unwrap();
@@ -1659,3 +1661,39 @@ fn a_v16_file_gains_the_provider_owner_column() {
.unwrap();
assert!(owner.is_none());
}
+
+#[test]
+fn migrates_v21_to_v22_replaces_session_index() {
+ let dir = tempfile::tempdir().unwrap();
+ let db_path = dir.path().join("pi.sqlite");
+ {
+ // Open a fresh database which creates the latest schema
+ let db = Database::open(&db_path).unwrap();
+ db.conn().execute_batch(
+ "
+ DROP INDEX IF EXISTS idx_sessions_updated_id;
+ CREATE INDEX idx_sessions_updated ON sessions(updated_at DESC);
+ PRAGMA user_version = 21;
+ "
+ ).unwrap();
+ }
+
+ let db = Database::open(&db_path).unwrap();
+
+ let version: i64 = db.conn().query_row("PRAGMA user_version", [], |r| r.get(0)).unwrap();
+ assert_eq!(version, crate::db::SCHEMA_VERSION);
+
+ let old_index_exists: bool = db.conn().query_row(
+ "SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type='index' AND name='idx_sessions_updated')",
+ [],
+ |r| r.get(0)
+ ).unwrap();
+ assert!(!old_index_exists, "idx_sessions_updated should be dropped");
+
+ let new_index_exists: bool = db.conn().query_row(
+ "SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type='index' AND name='idx_sessions_updated_id')",
+ [],
+ |r| r.get(0)
+ ).unwrap();
+ assert!(new_index_exists, "idx_sessions_updated_id should be created");
+}