@@ -685,18 +685,23 @@ def initialize_wide_catalog(
685685 audit_relations = audit_relations , phase = "catalog initialization" ,
686686 ) as connection :
687687 inspector = inspect (connection )
688+ tables = set (inspector .get_table_names ())
688689 views = set (inspector .get_view_names ())
689- if views and not inspector .has_table (normative .catalog_identity .name ):
690- raise UnsupportedOperationError (
691- "The selected database catalog is foreign; initialization is not permitted."
692- )
693- try :
694- create_normative_catalog (connection , normative )
695- except RuntimeError as error :
696- raise UnsupportedOperationError (
697- "The selected database catalog is foreign or incompatible; "
698- "initialization is not permitted."
699- ) from error
690+ if tables or views :
691+ if normative .catalog_identity .name not in tables :
692+ raise UnsupportedOperationError (
693+ "The selected database catalog is foreign; "
694+ "initialization is not permitted."
695+ )
696+ require_verified_catalog (connection )
697+ else :
698+ try :
699+ create_normative_catalog (connection , normative )
700+ except RuntimeError as error :
701+ raise UnsupportedOperationError (
702+ "The selected database catalog is foreign or incompatible; "
703+ "initialization is not permitted."
704+ ) from error
700705 require_verified_catalog (connection )
701706 return {"profile" : profile .name , "catalog" : "verified" }
702707
@@ -1345,6 +1350,26 @@ def read_fidelity_events(
13451350 return tuple (result )
13461351
13471352
1353+ @contextmanager
1354+ def _bound_export_audit_transaction (
1355+ * , database_url : str , dolt_conformance_source : Any | None , phase : str ,
1356+ ):
1357+ """Open one verified transaction bound to the effective Dolt working set."""
1358+ validate_connection_url (database_url )
1359+ require_persistent_database_url (database_url )
1360+ profile , active = effective_profile (
1361+ database_url , dolt_conformance_source = dolt_conformance_source ,
1362+ )
1363+ engine = create_engine (database_url )
1364+ normative = normative_catalog (MetaData ())
1365+ with _bound_catalog_transaction (
1366+ engine = engine , profile_name = profile .name , active = active ,
1367+ audit_relations = {normative .operation .name , normative .fidelity_event .name },
1368+ phase = phase ,
1369+ ) as connection :
1370+ yield connection , normative
1371+
1372+
13481373def record_export_operation (
13491374 * , database_url : str , dataset_id : str , destination : str ,
13501375 allowed_fidelity_events : Iterable [Mapping [str , Any ]],
@@ -1354,14 +1379,12 @@ def record_export_operation(
13541379) -> str :
13551380 """Persist a completed export only in the normative audit catalog."""
13561381 operation_details = dict (operation_details or {})
1357- engine = create_engine (database_url )
1358- effective_profile (
1359- database_url , dolt_conformance_source = dolt_conformance_source ,
1360- )
1361- normative = normative_catalog (MetaData ())
13621382 operation_id = str (uuid4 ())
13631383 events = tuple (allowed_fidelity_events )
1364- with engine .begin () as connection :
1384+ with _bound_export_audit_transaction (
1385+ database_url = database_url , dolt_conformance_source = dolt_conformance_source ,
1386+ phase = "export audit creation" ,
1387+ ) as (connection , normative ):
13651388 _verify_normative_catalog (connection , normative )
13661389 dataset = _resolve_normative_dataset (connection , normative , dataset_id )
13671390 completed_at = datetime .now (UTC ).replace (tzinfo = None )
@@ -1416,11 +1439,10 @@ def finish_export_operation(
14161439 dolt_conformance_source : Any | None = None ,
14171440) -> None :
14181441 """Mark a started normative export operation as succeeded."""
1419- effective_profile (
1420- database_url , dolt_conformance_source = dolt_conformance_source ,
1421- )
1422- normative = normative_catalog (MetaData ())
1423- with create_engine (database_url ).begin () as connection :
1442+ with _bound_export_audit_transaction (
1443+ database_url = database_url , dolt_conformance_source = dolt_conformance_source ,
1444+ phase = "export audit finalization" ,
1445+ ) as (connection , normative ):
14241446 _verify_normative_catalog (connection , normative )
14251447 row = _export_operation_row (connection , normative , operation_id )
14261448 if row ["status" ] != "started" :
@@ -1465,17 +1487,16 @@ def fail_export_operation(
14651487 dolt_conformance_source : Any | None = None ,
14661488) -> None :
14671489 """Close a started normative export after filesystem compensation."""
1468- effective_profile (
1469- database_url , dolt_conformance_source = dolt_conformance_source ,
1470- )
1471- normative = normative_catalog (MetaData ())
14721490 event = {
14731491 "code" : "export_failed" ,
14741492 "detail" : "Export publication or finalization failed after audit start." ,
14751493 "severity" : "error" ,
14761494 "details" : dict (failure_details ),
14771495 }
1478- with create_engine (database_url ).begin () as connection :
1496+ with _bound_export_audit_transaction (
1497+ database_url = database_url , dolt_conformance_source = dolt_conformance_source ,
1498+ phase = "export audit failure" ,
1499+ ) as (connection , normative ):
14791500 _verify_normative_catalog (connection , normative )
14801501 row = _export_operation_row (connection , normative , operation_id )
14811502 if row ["status" ] != "started" :
@@ -1497,11 +1518,10 @@ def record_export_backup_retained(
14971518 dolt_conformance_source : Any | None = None ,
14981519) -> None :
14991520 """Append a warning to a successfully finalized normative export."""
1500- effective_profile (
1501- database_url , dolt_conformance_source = dolt_conformance_source ,
1502- )
1503- normative = normative_catalog (MetaData ())
1504- with create_engine (database_url ).begin () as connection :
1521+ with _bound_export_audit_transaction (
1522+ database_url = database_url , dolt_conformance_source = dolt_conformance_source ,
1523+ phase = "export backup-retention audit" ,
1524+ ) as (connection , normative ):
15051525 _verify_normative_catalog (connection , normative )
15061526 row = _export_operation_row (connection , normative , operation_id )
15071527 if row ["status" ] != "succeeded" :
@@ -1532,10 +1552,6 @@ def record_export_cleanup_failure(
15321552 dolt_conformance_source : Any | None = None ,
15331553) -> str :
15341554 """Persist terminal cleanup failure in the normative audit catalog."""
1535- effective_profile (
1536- database_url , dolt_conformance_source = dolt_conformance_source ,
1537- )
1538- normative = normative_catalog (MetaData ())
15391555 operation_id = operation_id or str (uuid4 ())
15401556 event = {
15411557 "code" : "cleanup_failed" ,
@@ -1551,7 +1567,10 @@ def record_export_cleanup_failure(
15511567 ),
15521568 },
15531569 }
1554- with create_engine (database_url ).begin () as connection :
1570+ with _bound_export_audit_transaction (
1571+ database_url = database_url , dolt_conformance_source = dolt_conformance_source ,
1572+ phase = "export cleanup-failure audit" ,
1573+ ) as (connection , normative ):
15551574 _verify_normative_catalog (connection , normative )
15561575 row = connection .execute (
15571576 select (normative .operation ).where (
0 commit comments