from app import db def import_data_from(file, table_name: str, without_header: bool = False): copy_sql = __copy_csv_sql(table_name, without_header) cursor = db.session.connection().connection.cursor() cursor.execute(f'DELETE FROM "{table_name}";') cursor.copy_expert(copy_sql, file) db.session.commit() def update_data_from(file, table_name: str, without_header: bool = False): cursor = db.session.connection().connection.cursor() cursor.execute(f"""CREATE TEMP TABLE tmp_table (LIKE \"{table_name}\" INCLUDING ALL)""") copy_sql = __copy_csv_sql("tmp_table", without_header) cursor.copy_expert(copy_sql, file) cursor.execute( f""" INSERT INTO \"{table_name}\" SELECT * FROM tmp_table ON CONFLICT DO NOTHING; """ ) cursor.execute("DROP TABLE tmp_table") db.session.commit() def __copy_csv_sql(table_name: str, without_header: False) -> str: copy_sql = f"COPY \"{table_name}\" FROM STDIN WITH CSV DELIMITER'\t' QUOTE E'\b' " if without_header: copy_sql = f"COPY \"{table_name}\" FROM STDIN WITH CSV HEADER DELIMITER'\t' QUOTE E'\b' " return copy_sql