Skip to content

Commit 6dd8564

Browse files
committed
fix: remove nested transaction in populate_expanded_license
Signed-off-by: mrrajan <86094767+mrrajan@users.noreply.github.com.>
1 parent 1c9a0fd commit 6dd8564

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

modules/ingestor/src/graph/sbom/common/expanded_license.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use sea_orm::{ConnectionTrait, DbErr, Statement, TransactionTrait};
1+
use sea_orm::{ConnectionTrait, DbErr, Statement};
22
use 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.
2627
pub 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#"
3735
INSERT INTO expanded_license (expanded_text)
3836
SELECT 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#"
6159
WITH 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
}

modules/ingestor/src/graph/sbom/cyclonedx.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,11 @@ impl<'a> From<Information<'a>> for SbomInformation {
134134

135135
impl SbomContext {
136136
#[instrument(skip(connection, sbom, warnings), err(level=tracing::Level::INFO))]
137-
pub async fn ingest_cyclonedx<C: ConnectionTrait + sea_orm::TransactionTrait>(
137+
pub async fn ingest_cyclonedx(
138138
&self,
139139
mut sbom: Box<CycloneDx>,
140140
warnings: &dyn ReportSink,
141-
connection: &C,
141+
connection: &impl ConnectionTrait,
142142
) -> Result<(), Error> {
143143
// pre-flight checks
144144

@@ -283,7 +283,7 @@ impl<'a> Creator<'a> {
283283
#[instrument(skip(self, db, processors), err(level=tracing::Level::INFO))]
284284
pub async fn create(
285285
self,
286-
db: &(impl ConnectionTrait + sea_orm::TransactionTrait),
286+
db: &impl ConnectionTrait,
287287
processors: &mut [Box<dyn Processor>],
288288
) -> Result<(), Error> {
289289
let mut purls = PurlCreator::new();

modules/ingestor/src/graph/sbom/spdx.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ impl<'a> From<Information<'a>> for SbomInformation {
102102

103103
impl SbomContext {
104104
#[instrument(skip(db, sbom_data, warnings), ret(level=tracing::Level::DEBUG))]
105-
pub async fn ingest_spdx<C: ConnectionTrait + sea_orm::TransactionTrait>(
105+
pub async fn ingest_spdx(
106106
&self,
107107
sbom_data: SPDX,
108108
warnings: &dyn ReportSink,
109-
db: &C,
109+
db: &impl ConnectionTrait,
110110
) -> Result<(), Error> {
111111
// pre-flight checks
112112

0 commit comments

Comments
 (0)