"""
修正 tenant_user_roles 表結構
新增缺少的 edit_by 欄位
"""
import psycopg2

conn = psycopg2.connect(
    host="10.1.0.20",
    port=5433,
    database="hr_portal",
    user="admin",
    password="DC1qaz2wsx"
)
conn.autocommit = False
cur = conn.cursor()

try:
    print("Adding edit_by column to tenant_user_roles...")

    # 新增 edit_by 欄位
    cur.execute("""
        ALTER TABLE tenant_user_roles
        ADD COLUMN IF NOT EXISTS edit_by VARCHAR(100);
    """)

    conn.commit()
    print("SUCCESS: edit_by column added")

    # 驗證
    cur.execute("""
        SELECT column_name
        FROM information_schema.columns
        WHERE table_name = 'tenant_user_roles' AND column_name = 'edit_by';
    """)
    result = cur.fetchone()
    if result:
        print(f"Verified: {result[0]} column exists")
    else:
        print("ERROR: Column not found after adding")

except Exception as e:
    conn.rollback()
    print(f"ERROR: {e}")
    import traceback
    traceback.print_exc()
finally:
    cur.close()
    conn.close()
