import sqlite3 import os BASE_DIR = os.path.dirname(os.path.abspath(__file__)) DB_PATH = os.path.join(os.path.dirname(BASE_DIR), 'kis_stock.db') def migrate(): print(f"Migrating database v2 at {DB_PATH}...") conn = sqlite3.connect(DB_PATH) cursor = conn.cursor() # Financial Status (Health) try: cursor.execute("ALTER TABLE stocks ADD COLUMN financial_status VARCHAR") print("Added financial_status") except Exception as e: print(f"Skipping financial_status: {e}") # ETF Facet try: cursor.execute("ALTER TABLE stocks ADD COLUMN is_etf BOOLEAN DEFAULT 0") print("Added is_etf") except Exception as e: print(f"Skipping is_etf: {e}") # Current Price (Cache) try: cursor.execute("ALTER TABLE stocks ADD COLUMN current_price FLOAT DEFAULT 0") print("Added current_price") except Exception as e: print(f"Skipping current_price: {e}") # Stock Price History Table try: cursor.execute(""" CREATE TABLE IF NOT EXISTS stock_prices ( id INTEGER PRIMARY KEY AUTOINCREMENT, code VARCHAR NOT NULL, price FLOAT, change FLOAT, volume INTEGER, created_at DATETIME DEFAULT CURRENT_TIMESTAMP ) """) cursor.execute("CREATE INDEX IF NOT EXISTS idx_stock_prices_code ON stock_prices (code)") cursor.execute("CREATE INDEX IF NOT EXISTS idx_stock_prices_created_at ON stock_prices (created_at)") print("Created stock_prices table") except Exception as e: print(f"Error creating stock_prices: {e}") conn.commit() conn.close() print("Migration v2 complete.") if __name__ == "__main__": migrate()