[db] Actually fix migrations

This commit is contained in:
2026-03-19 21:37:22 -04:00
parent 4e846635cc
commit 4d95b7711a
3 changed files with 25 additions and 6 deletions

View File

@ -1,3 +1,3 @@
ALTER TABLE message ADD COLUMN IF NOT EXISTS timestamp INTEGER AS (data ->> '$.time') STORED;
ALTER TABLE message ADD COLUMN timestamp INTEGER AS (data ->> '$.time') STORED;
CREATE INDEX IF NOT EXISTS message_server_topic_timestamp ON message (server, topic, timestamp);

View File

@ -1 +1 @@
ALTER TABLE subscription ADD COLUMN IF NOT EXISTS retention_hours INTEGER DEFAULT 0;
ALTER TABLE subscription ADD COLUMN retention_hours INTEGER DEFAULT 0;

View File

@ -28,10 +28,29 @@ impl Db {
}
fn migrate(&mut self) -> Result<()> {
let conn = self.conn.read().unwrap();
conn.execute_batch(include_str!("./migrations/00.sql"))?;
conn.execute_batch(include_str!("./migrations/01.sql"))?;
conn.execute_batch(include_str!("./migrations/02.sql"))?;
conn.execute_batch(include_str!("./migrations/03.sql"))?;
conn.execute_batch(
"CREATE TABLE IF NOT EXISTS schema_version (version INTEGER PRIMARY KEY);",
)?;
let version: i32 =
match conn.query_row("SELECT version FROM schema_version LIMIT 1", [], |row| {
row.get(0)
}) {
Ok(v) => v,
Err(rusqlite::Error::QueryReturnedNoRows) => 0,
Err(e) => return Err(e),
};
if version < 1 {
conn.execute_batch(include_str!("./migrations/00.sql"))?;
conn.execute_batch(include_str!("./migrations/01.sql"))?;
conn.execute_batch(include_str!("./migrations/02.sql"))?;
}
if version < 3 {
conn.execute_batch(include_str!("./migrations/03.sql"))?;
}
conn.execute("INSERT OR REPLACE INTO schema_version VALUES (3)", [])?;
Ok(())
}
fn get_or_insert_server(&mut self, server: &str) -> Result<i64> {