FA3-Datafetch/backend/scripts/fix_data_updates_schema.py
2026-01-11 21:33:47 +08:00

56 lines
2.1 KiB
Python

import os
import sys
import psycopg2
from pathlib import Path
from dotenv import load_dotenv
# Load .env
ROOT_DIR = Path(__file__).resolve().parent.parent.parent
load_dotenv(ROOT_DIR / ".env")
def get_db_connection():
db_host = os.getenv("DB_HOST", "192.168.3.195")
db_user = os.getenv("DB_USER", "value")
db_pass = os.getenv("DB_PASSWORD", "Value609!")
db_name = os.getenv("DB_NAME", "fa3")
db_port = os.getenv("DB_PORT", "5432")
return psycopg2.connect(
host=db_host, user=db_user, password=db_pass, dbname=db_name, port=db_port
)
def fix_schema():
print("🔧 Fixing data_updates table schema...")
try:
conn = get_db_connection()
conn.autocommit = True
with conn.cursor() as cur:
# 1. Alter status column to TEXT to avoid varchar(8) limits
print("Altering status column to TEXT...")
cur.execute("ALTER TABLE data_updates ALTER COLUMN status TYPE TEXT;")
# 2. Check if error_message needs widening too
print("Altering error_message column to TEXT...")
cur.execute("ALTER TABLE data_updates ALTER COLUMN error_message TYPE TEXT;")
# 3. Ensure other columns are safe
# fetched_tables and row_counts should be JSONB or TEXT
# Just in case they are restricted varchar
print("Altering fetched_tables and row_counts to TEXT...")
cur.execute("ALTER TABLE data_updates ALTER COLUMN fetched_tables TYPE TEXT;")
cur.execute("ALTER TABLE data_updates ALTER COLUMN row_counts TYPE TEXT;")
# 4. Fix date columns that might also be varchar(8)
print("Altering data_start_date and data_end_date to TEXT...")
cur.execute("ALTER TABLE data_updates ALTER COLUMN data_start_date TYPE TEXT;")
cur.execute("ALTER TABLE data_updates ALTER COLUMN data_end_date TYPE TEXT;")
print("✅ Schema fixed successfully.")
conn.close()
except Exception as e:
print(f"❌ Error fixing schema: {e}")
if __name__ == "__main__":
fix_schema()