1- use sea_orm:: { ConnectionTrait , DbErr , Statement , TransactionTrait } ;
1+ use sea_orm:: { ConnectionTrait , DbErr , Statement } ;
22use uuid:: Uuid ;
33
44/// Populates expanded_license and sbom_license_expanded tables during SBOM ingestion
55///
6- /// This function uses two SQL statements within a transaction to:
6+ /// This function uses two SQL statements to:
77/// 1. Call expand_license_expression_with_mappings() once per license
88/// 2. Insert distinct expanded texts into the expanded_license dictionary
99/// 3. Populate the sbom_license_expanded junction table
@@ -17,22 +17,20 @@ use uuid::Uuid;
1717/// While SeaORM could express this via custom expressions, it would be significantly
1818/// more verbose and harder to maintain than the raw SQL.
1919///
20- /// **Transaction safety**: Both dictionary and junction table inserts run in a single
21- /// transaction to prevent partial state if the second insert fails.
20+ /// **Transaction safety**: This function expects to be called within a transaction
21+ /// (as it always is during SBOM ingestion). Both SQL statements will be atomic
22+ /// within the caller's transaction.
2223///
2324/// **Note on SQL duplication**: Similar SQL appears in migration m0002120 for backfilling
2425/// existing data. The migration processes ALL SBOMs at once, while this function runs
2526/// per-SBOM during ingestion. Keep both in sync when updating license expansion logic.
2627pub async fn populate_expanded_license (
2728 sbom_id : Uuid ,
28- db : & ( impl ConnectionTrait + TransactionTrait ) ,
29+ db : & impl ConnectionTrait ,
2930) -> Result < ( ) , DbErr > {
30- // Begin transaction to ensure atomicity between dictionary and junction table inserts
31- let txn = db. begin ( ) . await ?;
32-
3331 // Step 1: Insert into expanded_license dictionary
34- txn . execute ( Statement :: from_sql_and_values (
35- txn . get_database_backend ( ) ,
32+ db . execute ( Statement :: from_sql_and_values (
33+ db . get_database_backend ( ) ,
3634 r#"
3735INSERT INTO expanded_license (expanded_text)
3836SELECT DISTINCT expand_license_expression_with_mappings(
@@ -55,8 +53,8 @@ ON CONFLICT (text_hash) DO NOTHING
5553
5654 // Step 2: Insert into sbom_license_expanded junction table
5755 // Use CTE to call expand_license_expression_with_mappings() only once per (sbom_id, license_id)
58- txn . execute ( Statement :: from_sql_and_values (
59- txn . get_database_backend ( ) ,
56+ db . execute ( Statement :: from_sql_and_values (
57+ db . get_database_backend ( ) ,
6058 r#"
6159WITH license_expansions AS (
6260 SELECT DISTINCT
@@ -86,8 +84,5 @@ SET expanded_license_id = EXCLUDED.expanded_license_id
8684 ) )
8785 . await ?;
8886
89- // Commit transaction - both inserts succeed or both roll back
90- txn. commit ( ) . await ?;
91-
9287 Ok ( ( ) )
9388}
0 commit comments