From 59f728cba07924edaa40efa51e95a516068d2735 Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Sun, 5 Apr 2026 22:18:25 -0700 Subject: [PATCH] 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. --- app/db/migrations/004_staging_tracking.sql | 10 ++++++---- app/db/migrations/005_listing_category.sql | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/db/migrations/004_staging_tracking.sql b/app/db/migrations/004_staging_tracking.sql index 8090203..9a3aa0d 100644 --- a/app/db/migrations/004_staging_tracking.sql +++ b/app/db/migrations/004_staging_tracking.sql @@ -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 diff --git a/app/db/migrations/005_listing_category.sql b/app/db/migrations/005_listing_category.sql index 2d0ca4c..fc5e1cc 100644 --- a/app/db/migrations/005_listing_category.sql +++ b/app/db/migrations/005_listing_category.sql @@ -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;