import psycopg2
import rich.progress
import sys

con = psycopg2.connect(host="192.168.9.11", database="myios", user="postgres", password="123QweasdzxC!1")
def add_gaid(data, source):
    idfas = ",".join([f"'{it}'" for it in data])
    sql = f"""
    insert into data_app_idfa(idfaid, source)
select id as idfaid, '{source}' as source from idfa_data
where idfa in ({idfas})
    ON CONFLICT (idfaid,source) DO NOTHING"""
    cur = con.cursor()
    r = cur.execute(sql)
    # print("add gaid ", str(r), " records")
    con.commit()
    
            

    
def addfile(p):
    data = [] 
    with rich.progress.open(p,'r') as f:
        for line in f:
            gaid = line.strip()
            if gaid == "":
                continue
            data.append(gaid)
            if len(data) > 999:
                add_gaid(data, 'idfa-lift-new')
                data = []
        
        if len(data) > 0:
            add_gaid(data, 'idfa-lift-new')


if __name__ == "__main__":
    addfile(sys.argv[1])