app: P2 migrations (year/seller_type/extras) + importer backfill, guards idempotent

- db.init_db gains guarded ALTER TABLE additions for listings.year /
  seller_type / extras (schema.sql v1 untouched; PRAGMA-checked idempotent)
- rubric: extras_from_text() returns matched EXTRAS labels (table order)
- importer: normalize computes year (existing helper), seller_type ONLY
  from a real state-item signal (dealer/private, else NULL), extras JSON;
  upsert rewrites the new columns on both paths
- verified on live DB: import idempotent, counts stay 640/560/80, year
  backfilled on 599 rows, extras on 45, seller_type 0 (no state signal)
This commit is contained in:
Torres
2026-11-10 21:13:40 -04:00
parent 9bc6948861
commit 02a6b6df7e
3 changed files with 40 additions and 5 deletions

View File

@@ -23,7 +23,19 @@ def connect(db_path):
def init_db(conn):
"""Apply schema.sql (idempotent)."""
"""Apply schema.sql (idempotent) + guarded P2 column migrations.
P2 additions (docs/DATA-MODEL.md) are added with idempotent
``ALTER TABLE ... ADD COLUMN`` guarded by a PRAGMA check, so the locked
v1 schema.sql stays untouched and existing DBs migrate in place.
"""
with open(SCHEMA_PATH, encoding='utf-8') as fh:
conn.executescript(fh.read())
cols = {r['name'] for r in conn.execute("PRAGMA table_info('listings')")}
if 'year' not in cols:
conn.execute('ALTER TABLE listings ADD COLUMN year INTEGER')
if 'seller_type' not in cols:
conn.execute('ALTER TABLE listings ADD COLUMN seller_type TEXT')
if 'extras' not in cols:
conn.execute('ALTER TABLE listings ADD COLUMN extras TEXT')
conn.commit()