import psycopg2
from database import DB_CONFIG

try:
    print("正在升级数据库表结构...")
    conn = psycopg2.connect(**DB_CONFIG)
    cursor = conn.cursor()
    # 为指标表新增二级类目字段，旧数据默认填入 '未分类'
    cursor.execute("ALTER TABLE tb_metric_dict ADD COLUMN category_level_2 VARCHAR(100) DEFAULT '未分类';")
    conn.commit()
    print("✅ V3.0 数据库底座升级成功：tb_metric_dict 已增加 category_level_2 列！")
except psycopg2.errors.DuplicateColumn:
    print("⚠️ category_level_2 列已经存在，无需重复添加。")
except Exception as e:
    print(f"❌ 升级出错：{str(e)}")
finally:
    if 'cursor' in locals(): cursor.close()
    if 'conn' in locals(): conn.close()