Changeset View
Changeset View
Standalone View
Standalone View
swh/provenance/postgresql/provenance.py
Show First 20 Lines • Show All 187 Lines • ▼ Show 20 Lines | ) -> bool: | ||||
dsts = tuple(set((sha1,) for (_, sha1, _) in rows)) | dsts = tuple(set((sha1,) for (_, sha1, _) in rows)) | ||||
sql = f""" | sql = f""" | ||||
LOCK TABLE ONLY {dst_table}; | LOCK TABLE ONLY {dst_table}; | ||||
INSERT INTO {dst_table}(sha1) VALUES %s | INSERT INTO {dst_table}(sha1) VALUES %s | ||||
ON CONFLICT DO NOTHING | ON CONFLICT DO NOTHING | ||||
""" | """ | ||||
psycopg2.extras.execute_values(self.cursor, sql, dsts) | psycopg2.extras.execute_values(self.cursor, sql, dsts) | ||||
sql = """ | # Put the next three queries in a manual single transaction: | ||||
SELECT * FROM swh_provenance_relation_add( | # they use the same temp table | ||||
%s, %s, %s, %s::rel_row[] | with self.conn: | ||||
with self.conn.cursor() as cur: | |||||
cur.execute("SELECT swh_mktemp_relation_add()") | |||||
psycopg2.extras.execute_values( | |||||
cur, | |||||
sql=( | |||||
"INSERT INTO tmp_relation_add (src, dst, path) " | |||||
"VALUES %s" | |||||
), | |||||
argslist=rows, | |||||
) | ) | ||||
""" | sql = "SELECT swh_provenance_relation_add_from_temp(%s, %s, %s)" | ||||
self.cursor.execute(sql, (rel_table, src_table, dst_table, rows)) | cur.execute(sql, (rel_table, src_table, dst_table)) | ||||
return True | return True | ||||
except: # noqa: E722 | except: # noqa: E722 | ||||
# Unexpected error occurred, rollback all changes and log message | # Unexpected error occurred, rollback all changes and log message | ||||
logging.exception("Unexpected error") | logging.exception("Unexpected error") | ||||
if self.raise_on_commit: | if self.raise_on_commit: | ||||
raise | raise | ||||
return False | return False | ||||
▲ Show 20 Lines • Show All 77 Lines • Show Last 20 Lines |