Skip to content

Commit ed727dd

Browse files
committed
refactor: simplify things a bit
1 parent d5e4619 commit ed727dd

File tree

4 files changed

+34
-66
lines changed

4 files changed

+34
-66
lines changed

migration/src/data/mod.rs

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::{
2121
};
2222
use trustify_module_storage::service::dispatch::DispatchBackend;
2323

24+
/// A handler for processing a [`Document`] data migration.
2425
#[allow(async_fn_in_trait)]
2526
pub trait Handler<D>: Send
2627
where
@@ -34,6 +35,21 @@ where
3435
) -> anyhow::Result<()>;
3536
}
3637

38+
impl<F, D> Handler<D> for F
39+
where
40+
D: Document,
41+
for<'x> F: AsyncFn(D, D::Model, &'x DatabaseTransaction) -> anyhow::Result<()> + Send,
42+
{
43+
async fn call(
44+
&self,
45+
document: D,
46+
model: D::Model,
47+
tx: &DatabaseTransaction,
48+
) -> anyhow::Result<()> {
49+
(self)(document, model, tx).await
50+
}
51+
}
52+
3753
#[derive(Clone, Debug, PartialEq, Eq, clap::Parser)]
3854
pub struct Options {
3955
/// Number of concurrent documents being processes
@@ -108,7 +124,7 @@ impl From<&Options> for Partition {
108124
}
109125
}
110126

111-
/// A trait for processing documents
127+
/// A trait for processing documents using a [`Handler`].
112128
pub trait DocumentProcessor {
113129
fn process<D>(
114130
&self,
@@ -228,50 +244,6 @@ impl<'c> DocumentProcessor for SchemaManager<'c> {
228244
}
229245
}
230246

231-
/// A handler for data migration of documents.
232-
///
233-
/// Handlers have to be written in a way that they can be re-run multiple times on the same
234-
/// document without failing and creating the exact same output state.
235-
#[macro_export]
236-
macro_rules! handler {
237-
(async | $doc:ident: $doc_ty:ty, $model:ident, $tx:ident | $body:block) => {{
238-
struct H;
239-
240-
impl $crate::data::Handler<$doc_ty> for H {
241-
async fn call(
242-
&self,
243-
$doc: $doc_ty,
244-
$model: <$doc_ty as $crate::data::Document>::Model,
245-
$tx: &sea_orm::DatabaseTransaction,
246-
) -> anyhow::Result<()> {
247-
$body
248-
}
249-
}
250-
251-
H
252-
}};
253-
}
254-
255-
/// A handler for SBOMs.
256-
///
257-
/// See: [`handler!`].
258-
#[macro_export]
259-
macro_rules! sbom {
260-
(async | $doc:ident, $model:ident, $tx:ident | $body:block) => {
261-
$crate::handler!(async |$doc: $crate::data::Sbom, $model, $tx| $body)
262-
};
263-
}
264-
265-
/// A handler for advisories.
266-
///
267-
/// See: [`handler!`].
268-
#[macro_export]
269-
macro_rules! advisory {
270-
(async | $doc:ident, $model:ident, $tx:ident | $body:block) => {
271-
$crate::handler!(async |$doc: $crate::data::Advisory, $model, $tx| $body)
272-
};
273-
}
274-
275247
pub trait MigratorWithData {
276248
fn data_migrations() -> Vec<Box<dyn MigrationTraitWithData>>;
277249
}

migration/src/m0002000_add_sbom_properties.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
use crate::{
2-
data::{MigrationTraitWithData, Sbom as SbomDoc, SchemaDataManager},
3-
sbom,
4-
};
5-
use sea_orm::{ActiveModelTrait, IntoActiveModel, Set};
1+
use crate::data::{MigrationTraitWithData, Sbom as SbomDoc, SchemaDataManager};
2+
use sea_orm::{ActiveModelTrait, DatabaseTransaction, IntoActiveModel, Set};
63
use sea_orm_migration::prelude::*;
74
use trustify_common::advisory::cyclonedx::extract_properties_json;
5+
use trustify_entity::sbom;
86

97
#[derive(DeriveMigrationName)]
108
pub struct Migration;
@@ -38,7 +36,7 @@ impl MigrationTraitWithData for Migration {
3836
manager
3937
.process(
4038
self,
41-
sbom!(async |sbom, model, tx| {
39+
async |sbom: SbomDoc, model: sbom::Model, tx: &DatabaseTransaction| {
4240
let mut model = model.into_active_model();
4341
match sbom {
4442
SbomDoc::CycloneDx(sbom) => {
@@ -55,7 +53,7 @@ impl MigrationTraitWithData for Migration {
5553
model.save(tx).await?;
5654

5755
Ok(())
58-
}),
56+
},
5957
)
6058
.await?;
6159

migration/src/m0002010_add_advisory_scores.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
use crate::{
2-
advisory,
3-
data::{Advisory as AdvisoryDoc, MigrationTraitWithData, SchemaDataManager},
4-
};
5-
use sea_orm::sea_query::extension::postgres::*;
1+
use crate::data::{Advisory, MigrationTraitWithData, SchemaDataManager};
2+
use sea_orm::{DatabaseTransaction, sea_query::extension::postgres::*};
63
use sea_orm_migration::prelude::*;
74
use strum::VariantNames;
85
use trustify_common::db::create_enum_if_not_exists;
6+
use trustify_entity::advisory;
97
use trustify_module_ingestor::{
108
graph::cvss::ScoreCreator,
119
service::advisory::{csaf, cve, osv},
@@ -99,16 +97,16 @@ impl MigrationTraitWithData for Migration {
9997
manager
10098
.process(
10199
self,
102-
advisory!(async |advisory, model, tx| {
100+
async |advisory: Advisory, model: advisory::Model, tx: &DatabaseTransaction| {
103101
let mut creator = ScoreCreator::new(model.id);
104102
match advisory {
105-
AdvisoryDoc::Cve(advisory) => {
103+
Advisory::Cve(advisory) => {
106104
cve::extract_scores(&advisory, &mut creator);
107105
}
108-
AdvisoryDoc::Csaf(advisory) => {
106+
Advisory::Csaf(advisory) => {
109107
csaf::extract_scores(&advisory, &mut creator);
110108
}
111-
AdvisoryDoc::Osv(advisory) => {
109+
Advisory::Osv(advisory) => {
112110
osv::extract_scores(&advisory, &mut creator);
113111
}
114112
_ => {
@@ -119,7 +117,7 @@ impl MigrationTraitWithData for Migration {
119117
creator.create(tx).await?;
120118

121119
Ok(())
122-
}),
120+
},
123121
)
124122
.await?;
125123

migration/tests/data/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ mod sbom {
1717
use migration::{
1818
ColumnDef, DeriveIden, DeriveMigrationName, Table, async_trait,
1919
data::{MigrationTraitWithData, Sbom as SbomDoc, SchemaDataManager},
20-
sbom,
2120
};
22-
use sea_orm::{ConnectionTrait, DbErr, Statement};
21+
use sea_orm::{ConnectionTrait, DatabaseTransaction, DbErr, Statement};
22+
use trustify_entity::sbom;
2323

2424
#[derive(DeriveMigrationName)]
2525
pub struct Migration;
@@ -50,7 +50,7 @@ mod sbom {
5050
manager
5151
.process(
5252
self,
53-
sbom!(async |sbom, model, tx| {
53+
async |sbom: SbomDoc, model: sbom::Model, tx: &DatabaseTransaction| {
5454
// we just pick a random value
5555
let value = match sbom {
5656
SbomDoc::CycloneDx(sbom) => sbom.serial_number,
@@ -70,7 +70,7 @@ mod sbom {
7070
}
7171

7272
Ok(())
73-
}),
73+
},
7474
)
7575
.await?;
7676

0 commit comments

Comments
 (0)