Skip to content

Commit 54f7ec7

Browse files
committed
refactor: extract method
1 parent 71cf487 commit 54f7ec7

File tree

6 files changed

+36
-22
lines changed

6 files changed

+36
-22
lines changed

migration/src/data/migration.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
async_trait,
3-
data::{Document, DocumentProcessor, Handler, Options, Partition},
3+
data::{Document, DocumentProcessor, Handler, Options},
44
};
55
use clap::Parser;
66
use futures::executor::block_on;
@@ -32,7 +32,9 @@ fn init_options() -> Options {
3232
}
3333

3434
impl MigrationWithData {
35-
#[allow(clippy::expect_used)]
35+
/// Wrap a data migration, turning it into a combined schema/data migration.
36+
///
37+
/// **NOTE:** This may panic if the storage configuration is missing.
3638
pub fn new(migration: Box<dyn MigrationTraitWithData>) -> Self {
3739
Self {
3840
storage: STORAGE.clone(),
@@ -55,7 +57,6 @@ pub struct SchemaDataManager<'c> {
5557
pub manager: &'c SchemaManager<'c>,
5658
storage: &'c DispatchBackend,
5759
options: &'c Options,
58-
partition: Partition,
5960
}
6061

6162
impl<'c> SchemaDataManager<'c> {
@@ -64,16 +65,10 @@ impl<'c> SchemaDataManager<'c> {
6465
storage: &'c DispatchBackend,
6566
options: &'c Options,
6667
) -> Self {
67-
let partition = Partition {
68-
current: options.current,
69-
total: options.total,
70-
};
71-
7268
Self {
7369
manager,
7470
storage,
7571
options,
76-
partition,
7772
}
7873
}
7974

migration/src/data/mod.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ impl Default for Options {
117117
}
118118
}
119119

120+
impl From<&Options> for Partition {
121+
fn from(value: &Options) -> Self {
122+
Self {
123+
current: value.current,
124+
total: value.total,
125+
}
126+
}
127+
}
128+
120129
pub trait DocumentProcessor {
121130
fn process<D>(
122131
&self,
@@ -138,7 +147,7 @@ impl<'c> DocumentProcessor for SchemaManager<'c> {
138147
where
139148
D: Document,
140149
{
141-
let partition = Partition::default();
150+
let partition: Partition = options.into();
142151
let db = self.get_connection();
143152

144153
let tx = db.begin().await?;
@@ -147,7 +156,7 @@ impl<'c> DocumentProcessor for SchemaManager<'c> {
147156

148157
stream::iter(
149158
all.into_iter()
150-
.filter(|model| partition.is_selected::<D>(&model)),
159+
.filter(|model| partition.is_selected::<D>(model)),
151160
)
152161
.map(async |model| {
153162
let tx = db.begin().await?;
@@ -207,6 +216,18 @@ pub struct Migrations {
207216
all: Vec<Migration>,
208217
}
209218

219+
impl Migrations {
220+
/// Return only [`Migration::Data`] migrations.
221+
pub fn only_data(self) -> Vec<Box<dyn MigrationTraitWithData>> {
222+
self.into_iter()
223+
.filter_map(|migration| match migration {
224+
Migration::Normal(_) => None,
225+
Migration::Data(migration) => Some(migration),
226+
})
227+
.collect()
228+
}
229+
}
230+
210231
impl IntoIterator for Migrations {
211232
type Item = Migration;
212233
type IntoIter = std::vec::IntoIter<Self::Item>;

migration/src/data/partition.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::data::Document;
2-
use std::hash::{DefaultHasher, Hash, Hasher};
3-
use std::num::{NonZeroU64, NonZeroUsize};
2+
use std::{
3+
hash::{DefaultHasher, Hash, Hasher},
4+
num::NonZeroU64,
5+
};
46
use trustify_entity::sbom;
57

68
#[derive(Debug, Copy, Clone)]

migration/src/lib.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,15 @@ impl Migrator {
6464
.normal(m0001110_sbom_node_checksum_indexes::Migration)
6565
.normal(m0001120_sbom_external_node_indexes::Migration)
6666
.normal(m0001130_gover_cmp::Migration)
67+
.normal(m0001140_expand_spdx_licenses_function::Migration)
68+
.normal(m0001150_case_license_text_sbom_id_function::Migration)
6769
.data(m0002000_example_data_migration::Migration)
6870
}
6971
}
7072

7173
impl MigratorWithData for Migrator {
7274
fn data_migrations() -> Vec<Box<dyn MigrationTraitWithData>> {
73-
Self::migrations()
74-
.into_iter()
75-
.filter_map(|migration| match migration {
76-
Migration::Normal(_) => None,
77-
Migration::Data(migration) => Some(migration),
78-
})
79-
.collect()
75+
Self::migrations().only_data()
8076
}
8177
}
8278

server/src/profile/api.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ use crate::{endpoints, profile::spawn_db_check, sample_data};
88
use actix_web::web;
99
use bytesize::ByteSize;
1010
use futures::FutureExt;
11-
use humantime::parse_duration;
12-
use std::{env, fs::create_dir_all, path::PathBuf, process::ExitCode, sync::Arc, time::Duration};
11+
use std::{env, process::ExitCode, sync::Arc, time::Duration};
1312
use tokio_schedule::{Job, every};
1413
use trustify_auth::{
1514
auth::AuthConfigArguments,

trustd/src/db.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct Run {
1414
}
1515

1616
#[derive(clap::Subcommand, Debug, Clone)]
17+
#[allow(clippy::large_enum_variant)]
1718
pub enum Command {
1819
/// Create database
1920
Create,

0 commit comments

Comments
 (0)