script
This commit is contained in:
75
backend/apply_migration.py
Executable file
75
backend/apply_migration.py
Executable file
@@ -0,0 +1,75 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Apply SQL migration to existing database
|
||||
Usage: python apply_migration.py
|
||||
"""
|
||||
|
||||
import sqlite3
|
||||
import os
|
||||
|
||||
# Database path
|
||||
DB_PATH = os.path.join(os.path.dirname(__file__), "data", "data.db")
|
||||
MIGRATION_PATH = os.path.join(os.path.dirname(__file__), "migrations", "001_add_ram_stats_and_smart.sql")
|
||||
|
||||
def apply_migration():
|
||||
"""Apply the SQL migration"""
|
||||
|
||||
if not os.path.exists(DB_PATH):
|
||||
print(f"❌ Database not found at {DB_PATH}")
|
||||
print(" The database will be created automatically on first run.")
|
||||
return
|
||||
|
||||
if not os.path.exists(MIGRATION_PATH):
|
||||
print(f"❌ Migration file not found at {MIGRATION_PATH}")
|
||||
return
|
||||
|
||||
print(f"📂 Database: {DB_PATH}")
|
||||
print(f"📄 Migration: {MIGRATION_PATH}")
|
||||
print()
|
||||
|
||||
# Read migration SQL
|
||||
with open(MIGRATION_PATH, 'r') as f:
|
||||
migration_sql = f.read()
|
||||
|
||||
# Connect to database
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
cursor = conn.cursor()
|
||||
|
||||
try:
|
||||
# Check if columns already exist
|
||||
cursor.execute("PRAGMA table_info(hardware_snapshots)")
|
||||
columns = [row[1] for row in cursor.fetchall()]
|
||||
|
||||
if 'ram_used_mb' in columns:
|
||||
print("⚠️ Migration already applied (ram_used_mb column exists)")
|
||||
|
||||
# Check if disk_smart_data table exists
|
||||
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='disk_smart_data'")
|
||||
if cursor.fetchone():
|
||||
print("⚠️ disk_smart_data table already exists")
|
||||
print("✅ Database is up to date")
|
||||
return
|
||||
|
||||
# Apply migration
|
||||
print("🔄 Applying migration...")
|
||||
cursor.executescript(migration_sql)
|
||||
conn.commit()
|
||||
|
||||
print("✅ Migration applied successfully!")
|
||||
print()
|
||||
print("New columns added to hardware_snapshots:")
|
||||
print(" - ram_used_mb")
|
||||
print(" - ram_free_mb")
|
||||
print(" - ram_shared_mb")
|
||||
print()
|
||||
print("New table created:")
|
||||
print(" - disk_smart_data")
|
||||
|
||||
except sqlite3.Error as e:
|
||||
print(f"❌ Error applying migration: {e}")
|
||||
conn.rollback()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
apply_migration()
|
||||
Reference in New Issue
Block a user