fix: update schema for no migration, only nukes

This commit is contained in:
Jet 2026-03-18 17:52:33 -07:00
parent e8b60519e7
commit 553d7d1780
No known key found for this signature in database

View file

@ -10,7 +10,7 @@ pub fn init(path: &str) -> Result<Connection> {
" "
CREATE TABLE IF NOT EXISTS current_state ( CREATE TABLE IF NOT EXISTS current_state (
id INTEGER PRIMARY KEY CHECK (id = 1), id INTEGER PRIMARY KEY CHECK (id = 1),
status TEXT, status TEXT NOT NULL DEFAULT 'offline',
timestamp INTEGER, timestamp INTEGER,
last_seen INTEGER, last_seen INTEGER,
last_checked INTEGER last_checked INTEGER
@ -29,23 +29,11 @@ pub fn init(path: &str) -> Result<Connection> {
fetched_at INTEGER NOT NULL fetched_at INTEGER NOT NULL
); );
INSERT OR IGNORE INTO current_state (id, status, timestamp, last_seen, last_checked) VALUES (1, 'offline', NULL, NULL, NULL); INSERT OR IGNORE INTO current_state (id) VALUES (1);
INSERT OR IGNORE INTO pi_info (id, data, fetched_at) VALUES (1, '{}', 0); INSERT OR IGNORE INTO pi_info (id, data, fetched_at) VALUES (1, '{}', 0);
", ",
) )
.context("failed to initialize database schema")?; .context("failed to initialize database schema")?;
// Migration: add last_checked column if missing (existing databases)
let has_last_checked: bool = conn
.prepare("SELECT last_checked FROM current_state LIMIT 1")
.is_ok();
if !has_last_checked {
conn.execute_batch("ALTER TABLE current_state ADD COLUMN last_checked INTEGER")?;
}
// Migration: convert NULL status to 'offline'
conn.execute("UPDATE current_state SET status = 'offline' WHERE status IS NULL", [])?;
Ok(conn) Ok(conn)
} }