diff --git a/swh/provenance/postgresql/provenancedb_base.py b/swh/provenance/postgresql/provenancedb_base.py --- a/swh/provenance/postgresql/provenancedb_base.py +++ b/swh/provenance/postgresql/provenancedb_base.py @@ -12,6 +12,8 @@ class ProvenanceDBBase: + raise_on_commit: bool = False + def __init__(self, conn: psycopg2.extensions.connection): # TODO: consider adding a mutex for thread safety conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) @@ -40,17 +42,18 @@ self.select_cache = {"content": dict(), "directory": dict(), "revision": dict()} def commit(self): - result = False try: self.insert_all() self.clear_caches() - result = True + return True - except Exception as error: + except: # noqa: E722 # Unexpected error occurred, rollback all changes and log message - logging.error(f"Unexpected error: {error}") + logging.exception("Unexpected error") + if self.raise_on_commit: + raise - return result + return False def content_get_early_date(self, blob: FileEntry) -> Optional[datetime]: # First check if the date is being modified by current transection. diff --git a/swh/provenance/provenance.py b/swh/provenance/provenance.py --- a/swh/provenance/provenance.py +++ b/swh/provenance/provenance.py @@ -16,6 +16,8 @@ @runtime_checkable class ProvenanceInterface(Protocol): + raise_on_commit: bool = False + def commit(self): """Commit currently ongoing transactions in the backend DB""" ... diff --git a/swh/provenance/tests/conftest.py b/swh/provenance/tests/conftest.py --- a/swh/provenance/tests/conftest.py +++ b/swh/provenance/tests/conftest.py @@ -41,7 +41,10 @@ ) BaseDb.adapt_conn(provenance_db) - return ProvenanceDB(provenance_db) + prov = ProvenanceDB(provenance_db) + # in test sessions, we DO want to raise any exception occurring at commit time + prov.raise_on_commit = True + return prov @pytest.fixture