""" Migration script to add user_preferences table. This creates a new table for storing persistent user preferences: - Display settings (timezone, theme, date format) - Auto-refresh configuration - Calibration defaults - Status threshold customization Run this script once to migrate an existing database. """ import sqlite3 import os # Database path DB_PATH = "./data/seismo_fleet.db" def migrate_database(): """Create user_preferences table""" if not os.path.exists(DB_PATH): print(f"Database not found at {DB_PATH}") print("The database will be created automatically when you run the application.") return print(f"Migrating database: {DB_PATH}") conn = sqlite3.connect(DB_PATH) cursor = conn.cursor() # Check if user_preferences table already exists cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='user_preferences'") table_exists = cursor.fetchone() if table_exists: print("Migration already applied - user_preferences table exists") conn.close() return print("Creating user_preferences table...") try: cursor.execute(""" CREATE TABLE user_preferences ( id INTEGER PRIMARY KEY DEFAULT 1, timezone TEXT DEFAULT 'America/New_York', theme TEXT DEFAULT 'auto', auto_refresh_interval INTEGER DEFAULT 10, date_format TEXT DEFAULT 'MM/DD/YYYY', table_rows_per_page INTEGER DEFAULT 25, calibration_interval_days INTEGER DEFAULT 365, calibration_warning_days INTEGER DEFAULT 30, status_ok_threshold_hours INTEGER DEFAULT 12, status_pending_threshold_hours INTEGER DEFAULT 24, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) """) print(" ✓ Created user_preferences table") # Insert default preferences cursor.execute(""" INSERT INTO user_preferences (id) VALUES (1) """) print(" ✓ Inserted default preferences") conn.commit() print("\nMigration completed successfully!") except sqlite3.Error as e: print(f"\nError during migration: {e}") conn.rollback() raise finally: conn.close() if __name__ == "__main__": migrate_database()