fix: make ALTER TABLE migrations idempotent with IF NOT EXISTS

SQLite's executescript() auto-commits each DDL statement, so a partial
migration failure leaves columns in the DB without marking the migration
done. On the next startup the runner retries and hits duplicate column errors.

Use ADD COLUMN IF NOT EXISTS (SQLite 3.35+, shipped in Python 3.11+)
so migrations 004 and 005 are safe to re-run in any partial state.
This commit is contained in:
pyr0ball 2026-04-05 22:18:25 -07:00
parent 81e41e39ab
commit 59f728cba0
2 changed files with 7 additions and 5 deletions

View file

@ -1,10 +1,12 @@
-- Staging DB: persistent listing tracking across searches.
-- Adds temporal metadata to listings so we can detect stale/repriced/recurring items.
-- first_seen_at already defined in 001_init.sql CREATE TABLE; skip to avoid duplicate column error on fresh installs
ALTER TABLE listings ADD COLUMN last_seen_at TEXT;
ALTER TABLE listings ADD COLUMN times_seen INTEGER NOT NULL DEFAULT 1;
ALTER TABLE listings ADD COLUMN price_at_first_seen REAL;
-- IF NOT EXISTS (SQLite 3.35+, Python 3.11 ships 3.39+) makes this safe to re-run
-- in any state: fresh install, partial-failure recovery, or already-applied.
ALTER TABLE listings ADD COLUMN IF NOT EXISTS first_seen_at TEXT;
ALTER TABLE listings ADD COLUMN IF NOT EXISTS last_seen_at TEXT;
ALTER TABLE listings ADD COLUMN IF NOT EXISTS times_seen INTEGER NOT NULL DEFAULT 1;
ALTER TABLE listings ADD COLUMN IF NOT EXISTS price_at_first_seen REAL;
-- Backfill existing rows so columns are non-null where we have data
UPDATE listings SET

View file

@ -1,3 +1,3 @@
-- Add per-listing category name, extracted from eBay API response.
-- Used to derive seller category_history_json without _ssn scraping.
ALTER TABLE listings ADD COLUMN category_name TEXT;
ALTER TABLE listings ADD COLUMN IF NOT EXISTS category_name TEXT;