import os, psycopg2
from etl_billiards.tasks.dwd_load_task import DwdLoadTask

conn=psycopg2.connect(os.environ['PG_DSN'])
cur=conn.cursor()
problems=[]
for dwd_table, ods_table in DwdLoadTask.TABLE_MAP.items():
    if dwd_table.split('.')[-1].startswith('dwd_'):
        if '.' in dwd_table:
            dschema, dtable = dwd_table.split('.')
        else:
            dschema, dtable = 'billiards_dwd', dwd_table
        if '.' in ods_table:
            oschema, otable = ods_table.split('.')
        else:
            oschema, otable = 'billiards_ods', ods_table
        cur.execute("SELECT column_name,data_type FROM information_schema.columns WHERE table_schema=%s AND table_name=%s", (dschema,dtable))
        dcols={r[0].lower():r[1].lower() for r in cur.fetchall()}
        cur.execute("SELECT column_name,data_type FROM information_schema.columns WHERE table_schema=%s AND table_name=%s", (oschema,otable))
        ocols={r[0].lower():r[1].lower() for r in cur.fetchall()}
        common=set(dcols)&set(ocols)
        missing_dwd=list(set(ocols)-set(dcols))
        missing_ods=list(set(dcols)-set(ocols))
        mismatches=[(c,dcols[c],ocols[c]) for c in sorted(common) if dcols[c]!=ocols[c]]
        problems.append((dwd_table,missing_dwd,missing_ods,mismatches))
cur.close();conn.close()
for p in problems:
    print(p)
