"""一次性脚本：执行项目标签表迁移到测试库"""
import os
import sys
from pathlib import Path
from dotenv import load_dotenv
import psycopg2

load_dotenv(Path(__file__).resolve().parents[2] / ".env")

dsn = os.environ.get("PG_DSN")
if not dsn:
    print("ERROR: PG_DSN 未配置")
    sys.exit(1)
if "test_etl_feiqiu" not in dsn:
    print(f"ERROR: 非测试库 {dsn}")
    sys.exit(1)

sql_path = Path(__file__).resolve().parents[2] / "db/etl_feiqiu/migrations/2026-03-07__create_project_tag_tables.sql"
sql = sql_path.read_text(encoding="utf-8")

conn = psycopg2.connect(dsn)
conn.autocommit = True
cur = conn.cursor()
cur.execute(sql)
print("迁移执行成功")

cur.execute("""
    SELECT table_name FROM information_schema.tables
    WHERE table_schema='dws' AND table_name LIKE '%project_tag%'
    ORDER BY table_name
""")
for r in cur.fetchall():
    print(f"  表: {r[0]}")

cur.execute("""
    SELECT indexname FROM pg_indexes
    WHERE schemaname='dws' AND tablename LIKE '%project_tag%'
    ORDER BY indexname
""")
for r in cur.fetchall():
    print(f"  索引: {r[0]}")

cur.close()
conn.close()
