Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(storage): release index names when retiring legacy tables

Steven Enamakeldf2fa7ee4652
diff --git a/crates/openhuman-core/src/storage/local.rs b/crates/openhuman-core/src/storage/local.rs
index 9f9ebc9762..318e9c02d7 100644
--- a/crates/openhuman-core/src/storage/local.rs
+++ b/crates/openhuman-core/src/storage/local.rs
@@ -314,6 +314,17 @@ fn import(
Ok(written)
}
+/// Drops every cached default-store handle, so a data reset can delete the
+/// files (Windows refuses while a handle is open) and the next call opens
+/// and imports afresh.
+#[cfg(feature = "storage-sqlite")]
+pub fn release_all() {
+ OPENED
+ .lock()
+ .unwrap_or_else(PoisonError::into_inner)
+ .clear();
+}
+
/// Forgets that `db_path` was opened, so the next call opens (and imports)
/// it afresh; also how a test models a restart.
#[cfg(feature = "storage-sqlite")]
@@ -380,6 +391,16 @@ fn retire_tables(db_path: &Path, tables: &[&str]) -> Result<()> {
n += 1;
target = format!("_legacy_{table}_{n}");
}
+ // Index names are database-global and survive a rename, which would
+ // make a later `CREATE INDEX IF NOT EXISTS` on the recreated legacy
+ // table a silent no-op; the archive does not need them.
+ let indexes: Vec<String> = tx
+ .prepare("SELECT name FROM sqlite_master WHERE type = 'index' AND tbl_name = ?1 AND sql IS NOT NULL")?
+ .query_map([table], |row| row.get(0))?
+ .collect::<rusqlite::Result<_>>()?;
+ for index in indexes {
+ tx.execute_batch(&format!("DROP INDEX \"{index}\""))?;
+ }
tx.execute_batch(&format!("ALTER TABLE \"{table}\" RENAME TO \"{target}\""))
.with_context(|| format!("rename {table} to {target}"))?;
}
diff --git a/crates/openhuman-core/src/storage/local_tests.rs b/crates/openhuman-core/src/storage/local_tests.rs
index d36c4bb528..918767ccac 100644
--- a/crates/openhuman-core/src/storage/local_tests.rs
+++ b/crates/openhuman-core/src/storage/local_tests.rs
@@ -124,3 +124,21 @@ fn an_import_that_cannot_read_is_an_error_and_is_retried() {
assert!(open_things(&config(None), &path).is_some());
assert!(table_names(&path).contains(&"_legacy_old_things".to_string()));
}
+
+#[test]
+fn retiring_a_table_releases_its_index_names() {
+ let dir = TempDir::new().unwrap();
+ let path = legacy_file(&dir);
+ Connection::open(&path)
+ .unwrap()
+ .execute_batch("CREATE INDEX idx_old_things_n ON old_things(n)")
+ .unwrap();
+ open_things(&config(None), &path).unwrap();
+ let conn = Connection::open(&path).unwrap();
+ // A recreated legacy table can index itself under the same name again.
+ conn.execute_batch(
+ "CREATE TABLE old_things (id TEXT PRIMARY KEY, n INTEGER);
+ CREATE INDEX idx_old_things_n ON old_things(n)",
+ )
+ .unwrap();
+}