[db] Actually fix migrations
This commit is contained in:
@ -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);
|
CREATE INDEX IF NOT EXISTS message_server_topic_timestamp ON message (server, topic, timestamp);
|
||||||
|
|||||||
@ -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;
|
||||||
|
|||||||
@ -28,10 +28,29 @@ impl Db {
|
|||||||
}
|
}
|
||||||
fn migrate(&mut self) -> Result<()> {
|
fn migrate(&mut self) -> Result<()> {
|
||||||
let conn = self.conn.read().unwrap();
|
let conn = self.conn.read().unwrap();
|
||||||
conn.execute_batch(include_str!("./migrations/00.sql"))?;
|
conn.execute_batch(
|
||||||
conn.execute_batch(include_str!("./migrations/01.sql"))?;
|
"CREATE TABLE IF NOT EXISTS schema_version (version INTEGER PRIMARY KEY);",
|
||||||
conn.execute_batch(include_str!("./migrations/02.sql"))?;
|
)?;
|
||||||
conn.execute_batch(include_str!("./migrations/03.sql"))?;
|
|
||||||
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
fn get_or_insert_server(&mut self, server: &str) -> Result<i64> {
|
fn get_or_insert_server(&mut self, server: &str) -> Result<i64> {
|
||||||
|
|||||||
Reference in New Issue
Block a user