in glam/api/management/commands/import_desktop_aggs.py [0:0]
def import_file(self, tmp_table, fp, model, channel):
csv_columns = [f.name for f in model._meta.get_fields() if f.name not in ["id"]]
log(channel, " Importing file into temp table.")
with connection.cursor() as cursor:
with open(fp.name, "r") as tmp_file:
sql = f"""
COPY {tmp_table} ({", ".join(csv_columns)}) FROM STDIN WITH CSV
"""
cursor.copy_expert(sql, tmp_file)
log(channel, " Inserting data from temp table into aggregation tables.")
with connection.cursor() as cursor:
sql = f"""
INSERT INTO {model._meta.db_table} ({", ".join(csv_columns)})
SELECT * from {tmp_table}
ON CONFLICT ON CONSTRAINT desktop_{channel}_unique_dimensions
DO UPDATE SET
total_users = EXCLUDED.total_users,
histogram = EXCLUDED.histogram,
percentiles = EXCLUDED.percentiles,
total_sample = EXCLUDED.total_sample,
non_norm_histogram = EXCLUDED.non_norm_histogram,
non_norm_percentiles = EXCLUDED.non_norm_percentiles
"""
cursor.execute(sql)