db8d666aa1
New UserPreferences field controls the mic channel's unit on the SFM event-detail modal's waveform chart only. "dBL" default, "psi" alternate. Peaks everywhere else (tables, KPI tiles, modal summary) stay in dBL regardless — this is strictly a chart-axis preference. Surfaced as a single dropdown on Settings → General, below the auto-refresh interval. Setting up the storage half ahead of the chart port in the next commit, so the chart can read the value from /api/settings/preferences on first render instead of needing a follow-up wiring pass. Includes idempotent backend/migrate_add_mic_unit_pref.py for fleets already on an older schema. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
57 lines
1.6 KiB
Python
57 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Database migration: Add mic_unit_pref column to user_preferences.
|
|
|
|
Adds a single field controlling the mic channel's unit on the event-
|
|
report waveform chart in the SFM event detail modal. "dBL" (default)
|
|
or "psi". Peaks and KPI tiles elsewhere are always dBL regardless.
|
|
|
|
Idempotent — safe to re-run.
|
|
"""
|
|
|
|
import sqlite3
|
|
from pathlib import Path
|
|
|
|
|
|
def migrate():
|
|
possible_paths = [
|
|
Path("data/seismo_fleet.db"),
|
|
Path("data/sfm.db"),
|
|
Path("data/seismo.db"),
|
|
]
|
|
db_path = next((p for p in possible_paths if p.exists()), None)
|
|
if db_path is None:
|
|
print(f"Database not found in any of: {[str(p) for p in possible_paths]}")
|
|
print("Will be created with the new column when models.py initialises.")
|
|
return
|
|
|
|
print(f"Using database: {db_path}")
|
|
conn = sqlite3.connect(db_path)
|
|
cur = conn.cursor()
|
|
|
|
cur.execute("PRAGMA table_info(user_preferences)")
|
|
existing = {row[1] for row in cur.fetchall()}
|
|
|
|
if "mic_unit_pref" in existing:
|
|
print("mic_unit_pref already exists — nothing to do.")
|
|
conn.close()
|
|
return
|
|
|
|
cur.execute(
|
|
"ALTER TABLE user_preferences "
|
|
"ADD COLUMN mic_unit_pref TEXT DEFAULT 'dBL'"
|
|
)
|
|
# Backfill the single row that should exist (id=1) to the default,
|
|
# in case the column ends up NULL on existing rows.
|
|
cur.execute(
|
|
"UPDATE user_preferences SET mic_unit_pref = 'dBL' "
|
|
"WHERE mic_unit_pref IS NULL"
|
|
)
|
|
conn.commit()
|
|
conn.close()
|
|
print("Added mic_unit_pref to user_preferences (default 'dBL').")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
migrate()
|