Lemontropia-Suite/migrate_db.py

140 lines
4.7 KiB
Python

"""
Database migration script for Lemontropia Suite
Adds loadout support to existing database
"""
import sqlite3
import sys
from pathlib import Path
def migrate_database(db_path: str):
"""Migrate database to latest schema."""
print(f"Migrating database: {db_path}")
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Check if loadout_id column exists
cursor.execute("PRAGMA table_info(hunting_sessions)")
columns = [col[1] for col in cursor.fetchall()]
if 'loadout_id' not in columns:
print("Adding loadout_id column to hunting_sessions...")
cursor.execute("""
ALTER TABLE hunting_sessions
ADD COLUMN loadout_id INTEGER
REFERENCES loadouts(id) ON DELETE SET NULL
""")
else:
print("loadout_id column already exists")
# Check if mindforce_cost_ped column exists
if 'mindforce_cost_ped' not in columns:
print("Adding mindforce_cost_ped column...")
cursor.execute("""
ALTER TABLE hunting_sessions
ADD COLUMN mindforce_cost_ped REAL DEFAULT 0.0
""")
else:
print("mindforce_cost_ped column already exists")
# Check if hits_taken column exists
if 'hits_taken' not in columns:
print("Adding hits_taken column...")
cursor.execute("""
ALTER TABLE hunting_sessions
ADD COLUMN hits_taken INTEGER DEFAULT 0
""")
else:
print("hits_taken column already exists")
# Check if heals_used column exists
if 'heals_used' not in columns:
print("Adding heals_used column...")
cursor.execute("""
ALTER TABLE hunting_sessions
ADD COLUMN heals_used INTEGER DEFAULT 0
""")
else:
print("heals_used column already exists")
# Create loadouts table if not exists
cursor.execute("""
CREATE TABLE IF NOT EXISTS loadouts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
description TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
weapon_name TEXT,
weapon_damage REAL DEFAULT 0.0,
weapon_decay_pec REAL DEFAULT 0.0,
weapon_ammo_pec REAL DEFAULT 0.0,
weapon_dpp REAL DEFAULT 0.0,
weapon_efficiency REAL DEFAULT 0.0,
amplifier_name TEXT,
amplifier_decay_pec REAL DEFAULT 0.0,
scope_name TEXT,
scope_decay_pec REAL DEFAULT 0.0,
absorber_name TEXT,
absorber_decay_pec REAL DEFAULT 0.0,
armor_name TEXT,
armor_decay_per_hp REAL DEFAULT 0.05,
plates_json TEXT,
healing_tool_name TEXT,
healing_decay_pec REAL DEFAULT 0.0,
healing_amount REAL DEFAULT 0.0,
mindforce_implant_name TEXT,
mindforce_decay_pec REAL DEFAULT 0.0,
left_ring TEXT,
right_ring TEXT,
pet_name TEXT,
accessories_json TEXT,
enhancers_json TEXT,
cost_per_shot_ped REAL DEFAULT 0.0,
cost_per_hit_ped REAL DEFAULT 0.0,
cost_per_heal_ped REAL DEFAULT 0.0,
is_active BOOLEAN DEFAULT 0,
metadata TEXT
)
""")
print("Created loadouts table (if not exists)")
# Create indexes
cursor.execute("CREATE INDEX IF NOT EXISTS idx_loadouts_name ON loadouts(name)")
cursor.execute("CREATE INDEX IF NOT EXISTS idx_loadouts_active ON loadouts(is_active)")
cursor.execute("CREATE INDEX IF NOT EXISTS idx_hunting_sessions_loadout ON hunting_sessions(loadout_id)")
print("Created indexes")
# Update schema version
cursor.execute("""
CREATE TABLE IF NOT EXISTS schema_version (
version INTEGER PRIMARY KEY,
applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
description TEXT
)
""")
cursor.execute("""
INSERT OR REPLACE INTO schema_version (version, description)
VALUES (3, 'Added loadout support with loadout_id, mindforce costs, hits/heals tracking')
""")
conn.commit()
conn.close()
print("✅ Migration complete!")
if __name__ == "__main__":
# Default path
db_path = Path.home() / "Documents" / "AA2-Repositories" / "Lemontropia-Tool-Alpha" / "Lemontropia-Suite" / "data" / "lemontropia.db"
if len(sys.argv) > 1:
db_path = sys.argv[1]
if not Path(db_path).exists():
print(f"Database not found: {db_path}")
print("Run gui_main.py to create a new database")
sys.exit(1)
migrate_database(str(db_path))