# -*- coding: utf-8 -*-
"""验证四个数据库的状态：表数量、schema 分布"""
import psycopg2

CONN = dict(host="100.64.0.4", port=5432, user="local-Python", password="Neo-local-1991125")
DBS = ["etl_feiqiu", "test_etl_feiqiu", "zqyy_app", "test_zqyy_app"]

for db in DBS:
    try:
        c = psycopg2.connect(**CONN, dbname=db)
        cur = c.cursor()
        cur.execute(
            "SELECT schemaname, count(*) FROM pg_tables "
            "WHERE schemaname NOT IN ('pg_catalog','information_schema') "
            "GROUP BY schemaname ORDER BY schemaname"
        )
        rows = cur.fetchall()
        total = sum(r[1] for r in rows)
        schemas = ", ".join(f"{r[0]}({r[1]})" for r in rows)
        print(f"[OK] {db}: {total} tables | {schemas}")

        # 物化视图数量
        cur.execute(
            "SELECT count(*) FROM pg_matviews "
            "WHERE schemaname NOT IN ('pg_catalog','information_schema')"
        )
        mv_count = cur.fetchone()[0]
        if mv_count:
            print(f"      matviews: {mv_count}")

        c.close()
    except Exception as e:
        print(f"[FAIL] {db}: {e}")

print("\n--- 配置文件指向 ---")
print("ETL .env PG_DSN -> test_etl_feiqiu (已确认)")
print("根 .env -> PG_DSN=test_etl_feiqiu, APP_DB_DSN=test_zqyy_app")
print("后端 .env.local -> APP_DB_NAME=test_zqyy_app, ETL_DB_NAME=test_etl_feiqiu")
print("后端 config.py 默认值 -> test_zqyy_app / test_etl_feiqiu")
print("FDW 生产 -> setup_fdw.sql (etl_feiqiu)")
print("FDW 测试 -> setup_fdw_test.sql (test_etl_feiqiu)")
