74 lines
2.6 KiB
Python
74 lines
2.6 KiB
Python
|
|
import sqlite3
|
|
import sys
|
|
import os
|
|
import logging
|
|
|
|
# Add parent path to import config
|
|
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")))
|
|
from backend.config import settings
|
|
|
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Extract DB file path from SQLAlchemy URL
|
|
DB_FILE = settings.DATABASE_URL.replace("sqlite:///", "")
|
|
|
|
def get_db_connection():
|
|
"""Establishes a connection to the SQLite database."""
|
|
return sqlite3.connect(DB_FILE)
|
|
|
|
def get_table_columns(cursor, table_name):
|
|
"""Returns a list of column names for a given table."""
|
|
cursor.execute(f"PRAGMA table_info({table_name})")
|
|
return [row[1] for row in cursor.fetchall()]
|
|
|
|
def migrate_industries_table():
|
|
"""
|
|
Adds the new schema columns to the 'industries' table if they don't exist.
|
|
This ensures backward compatibility with older database files.
|
|
"""
|
|
logger.info(f"Connecting to database at {DB_FILE} to run migrations...")
|
|
conn = get_db_connection()
|
|
cursor = conn.cursor()
|
|
|
|
try:
|
|
logger.info("Checking 'industries' table schema...")
|
|
columns = get_table_columns(cursor, "industries")
|
|
logger.info(f"Found existing columns: {columns}")
|
|
|
|
migrations_to_run = {
|
|
"metric_type": "TEXT",
|
|
"scraper_search_term": "TEXT",
|
|
"standardization_logic": "TEXT",
|
|
"proxy_factor": "FLOAT"
|
|
# min_requirement, whale_threshold, scraper_keywords already exist from v0.6.0
|
|
}
|
|
|
|
for col, col_type in migrations_to_run.items():
|
|
if col not in columns:
|
|
logger.info(f"Adding column '{col}' ({col_type}) to 'industries' table...")
|
|
cursor.execute(f"ALTER TABLE industries ADD COLUMN {col} {col_type}")
|
|
else:
|
|
logger.info(f"Column '{col}' already exists. Skipping.")
|
|
|
|
# Also, we need to handle the removal of old columns if necessary (safer to leave them)
|
|
# We will also fix the proxy_factor type if it was TEXT
|
|
# This is more complex, for now let's just add.
|
|
|
|
conn.commit()
|
|
logger.info("Migrations for 'industries' table completed successfully.")
|
|
|
|
except Exception as e:
|
|
logger.error(f"An error occurred during migration: {e}", exc_info=True)
|
|
conn.rollback()
|
|
finally:
|
|
conn.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if not os.path.exists(DB_FILE):
|
|
logger.error(f"Database file not found at {DB_FILE}. Cannot run migration. Please ensure the old database is in place.")
|
|
else:
|
|
migrate_industries_table()
|