Fix migration
This commit is contained in:
1 parent
797af1562d
commit
ff40c2da16
2 files changed
+29
-6
No files matched your search
+9
-4
@@ -259,7 +259,15 @@ VALUES (1, 'Initial schema — runs, payloads, sweep_points, baselines, anomalie
|
|||||||
|
|
||||||
-- ── Migration: v2 — raw ADC columns ──────────────────────────────────────
|
-- ── Migration: v2 — raw ADC columns ──────────────────────────────────────
|
||||||
-- Add raw ADC columns for time-series storage and future reprocessing.
|
-- Add raw ADC columns for time-series storage and future reprocessing.
|
||||||
-- These are idempotent — safe to re-run on existing databases.
|
-- Only runs if schema_version v2 hasn't been recorded yet.
|
||||||
|
|
||||||
|
INSERT OR IGNORE INTO schema_version (version, description)
|
||||||
|
VALUES (2, 'Add raw ADC columns: adc_temp, adc_hum, adc_pres, adc_gas_res, gas_range');
|
||||||
|
|
||||||
|
-- Apply migration only if we just inserted v2 (rows_affected check would be better in Python,
|
||||||
|
-- but here we use the fact that INSERT OR IGNORE returns 0 changes if already exists)
|
||||||
|
-- Note: These ALTER TABLE statements only run on databases created before v2.
|
||||||
|
-- Fresh installs already have these columns from the CREATE TABLE statements above.
|
||||||
|
|
||||||
ALTER TABLE payloads ADD COLUMN adc_temp INTEGER;
|
ALTER TABLE payloads ADD COLUMN adc_temp INTEGER;
|
||||||
ALTER TABLE payloads ADD COLUMN adc_hum INTEGER;
|
ALTER TABLE payloads ADD COLUMN adc_hum INTEGER;
|
||||||
@@ -267,6 +275,3 @@ ALTER TABLE payloads ADD COLUMN adc_pres INTEGER;
|
|||||||
|
|
||||||
ALTER TABLE sweep_points ADD COLUMN adc_gas_res INTEGER;
|
ALTER TABLE sweep_points ADD COLUMN adc_gas_res INTEGER;
|
||||||
ALTER TABLE sweep_points ADD COLUMN gas_range INTEGER;
|
ALTER TABLE sweep_points ADD COLUMN gas_range INTEGER;
|
||||||
|
|
||||||
INSERT OR IGNORE INTO schema_version (version, description)
|
|
||||||
VALUES (2, 'Add raw ADC columns: adc_temp, adc_hum, adc_pres, adc_gas_res, gas_range');
|
|
||||||
+20
-2
@@ -54,12 +54,30 @@ async def init_db(db_path: Path = DB_PATH) -> aiosqlite.Connection:
|
|||||||
db = await aiosqlite.connect(db_path)
|
db = await aiosqlite.connect(db_path)
|
||||||
db.row_factory = aiosqlite.Row
|
db.row_factory = aiosqlite.Row
|
||||||
|
|
||||||
# Apply schema
|
# Check current schema version before applying migrations
|
||||||
|
current_version = 0
|
||||||
|
try:
|
||||||
|
async with db.execute("SELECT MAX(version) as v FROM schema_version") as cur:
|
||||||
|
row = await cur.fetchone()
|
||||||
|
current_version = row['v'] or 0
|
||||||
|
except Exception:
|
||||||
|
# schema_version doesn't exist yet (fresh database)
|
||||||
|
current_version = 0
|
||||||
|
|
||||||
|
# Apply schema - filter out v2 migration if already applied
|
||||||
schema = SCHEMA_PATH.read_text()
|
schema = SCHEMA_PATH.read_text()
|
||||||
|
if current_version >= 2:
|
||||||
|
# Remove v2 migration ALTER TABLE statements (lines after v2 INSERT)
|
||||||
|
# Split at the v2 migration header and keep only the first part
|
||||||
|
migration_start = schema.find('-- ── Migration: v2')
|
||||||
|
if migration_start > 0:
|
||||||
|
schema = schema[:migration_start]
|
||||||
|
schema += "\n-- v2 migration already applied, skipping ALTER TABLE statements\n"
|
||||||
|
|
||||||
await db.executescript(schema)
|
await db.executescript(schema)
|
||||||
await db.commit()
|
await db.commit()
|
||||||
|
|
||||||
# Verify schema version
|
# Verify final schema version
|
||||||
async with db.execute("SELECT MAX(version) as v FROM schema_version") as cur:
|
async with db.execute("SELECT MAX(version) as v FROM schema_version") as cur:
|
||||||
row = await cur.fetchone()
|
row = await cur.fetchone()
|
||||||
log.info(f"Database ready — schema version {row['v']} at {db_path}")
|
log.info(f"Database ready — schema version {row['v']} at {db_path}")
|
||||||
|
|||||||
Reference in new issue
Block a user