refactor(hive-c0re): shared additive-migration helper in db

db::apply_migrations runs ALTER lists and ignores duplicate-column
errors (turn_stats' pattern); approvals, operator_questions, broker
reminders, and scheduled_prompts drop their hand-rolled
pragma_table_info guards. broker's acked_at migration stays bespoke —
its backfill must only run when the column was just created
This commit is contained in:
müde 2026-07-06 21:53:48 +02:00
commit d190420946
7 changed files with 71 additions and 132 deletions

View file

@ -1121,33 +1121,19 @@ fn ensure_message_columns(conn: &Connection) -> Result<()> {
Ok(())
}
/// Idempotent reminder-table migrations. `ALTER TABLE ADD COLUMN`
/// has no `IF NOT EXISTS` form in sqlite, so we probe
/// `pragma_table_info` per column. New deploys (table created by
/// SCHEMA in this commit cycle) skip the ALTER; pre-existing
/// broker.sqlite files get the columns added on next boot.
/// Idempotent reminder-table migrations — plain additive columns, via
/// `db::apply_migrations`. (The messages-table migration above stays
/// bespoke: its backfill must run only when `acked_at` was just
/// created, which try-and-ignore can't express.)
fn ensure_reminder_columns(conn: &Connection) -> Result<()> {
for (name, sql) in [
(
"attempt_count",
"ALTER TABLE reminders ADD COLUMN attempt_count INTEGER NOT NULL DEFAULT 0;",
),
(
"last_error",
"ALTER TABLE reminders ADD COLUMN last_error TEXT;",
),
] {
let has: bool = conn
.prepare(&format!(
"SELECT 1 FROM pragma_table_info('reminders') WHERE name = '{name}'"
))?
.exists([])?;
if !has {
conn.execute_batch(sql)
.with_context(|| format!("add reminders.{name} column"))?;
}
}
Ok(())
crate::db::apply_migrations(
conn,
"broker reminders",
&[
"ALTER TABLE reminders ADD COLUMN attempt_count INTEGER NOT NULL DEFAULT 0",
"ALTER TABLE reminders ADD COLUMN last_error TEXT",
],
)
}
fn now_unix() -> i64 {