fix(explorer): resolve initialization and import errors for v0.7.0 backend
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
|
||||
import sqlite3
|
||||
import sys
|
||||
import os
|
||||
@@ -23,41 +22,53 @@ def get_table_columns(cursor, table_name):
|
||||
cursor.execute(f"PRAGMA table_info({table_name})")
|
||||
return [row[1] for row in cursor.fetchall()]
|
||||
|
||||
def migrate_industries_table():
|
||||
def migrate_tables():
|
||||
"""
|
||||
Adds the new schema columns to the 'industries' table if they don't exist.
|
||||
This ensures backward compatibility with older database files.
|
||||
Adds new columns to existing tables to support v0.7.0 features.
|
||||
"""
|
||||
logger.info(f"Connecting to database at {DB_FILE} to run migrations...")
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
|
||||
try:
|
||||
# 1. Update INDUSTRIES Table
|
||||
logger.info("Checking 'industries' table schema...")
|
||||
columns = get_table_columns(cursor, "industries")
|
||||
logger.info(f"Found existing columns: {columns}")
|
||||
|
||||
migrations_to_run = {
|
||||
ind_columns = get_table_columns(cursor, "industries")
|
||||
|
||||
ind_migrations = {
|
||||
"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
|
||||
"proxy_factor": "FLOAT",
|
||||
"scraper_keywords": "TEXT",
|
||||
"scraper_search_term": "TEXT"
|
||||
}
|
||||
|
||||
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...")
|
||||
for col, col_type in ind_migrations.items():
|
||||
if col not in ind_columns:
|
||||
logger.info(f"Adding column '{col}' 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.
|
||||
# 2. Update COMPANIES Table (New for v0.7.0)
|
||||
logger.info("Checking 'companies' table schema...")
|
||||
comp_columns = get_table_columns(cursor, "companies")
|
||||
|
||||
comp_migrations = {
|
||||
"calculated_metric_name": "TEXT",
|
||||
"calculated_metric_value": "FLOAT",
|
||||
"calculated_metric_unit": "TEXT",
|
||||
"standardized_metric_value": "FLOAT",
|
||||
"standardized_metric_unit": "TEXT",
|
||||
"metric_source": "TEXT"
|
||||
}
|
||||
|
||||
for col, col_type in comp_migrations.items():
|
||||
if col not in comp_columns:
|
||||
logger.info(f"Adding column '{col}' to 'companies' table...")
|
||||
cursor.execute(f"ALTER TABLE companies ADD COLUMN {col} {col_type}")
|
||||
|
||||
conn.commit()
|
||||
logger.info("Migrations for 'industries' table completed successfully.")
|
||||
logger.info("All migrations completed successfully.")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"An error occurred during migration: {e}", exc_info=True)
|
||||
@@ -65,9 +76,8 @@ def migrate_industries_table():
|
||||
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.")
|
||||
logger.error(f"Database file not found at {DB_FILE}.")
|
||||
else:
|
||||
migrate_industries_table()
|
||||
migrate_tables()
|
||||
Reference in New Issue
Block a user