From f51ff0efae60d09ebf6a2871aef491c01d931eac Mon Sep 17 00:00:00 2001 From: JimFuller-RedHat Date: Wed, 7 Aug 2024 08:54:30 +0200 Subject: [PATCH] chore - add indexes to assist sorting at scale (fixes #645). --- migration/src/lib.rs | 2 + migration/src/m0000520_scale_indexes.rs | 113 ++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 migration/src/m0000520_scale_indexes.rs diff --git a/migration/src/lib.rs b/migration/src/lib.rs index 555036647..6c74a9f00 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -61,6 +61,7 @@ mod m0000490_cascade_advisory_delete; mod m0000500_fix_sbom_node_fks; mod m0000501_perf_indexes; mod m0000510_create_maven_cmp_fns; +mod m0000520_scale_indexes; pub struct Migrator; @@ -128,6 +129,7 @@ impl MigratorTrait for Migrator { Box::new(m0000500_fix_sbom_node_fks::Migration), Box::new(m0000501_perf_indexes::Migration), Box::new(m0000510_create_maven_cmp_fns::Migration), + Box::new(m0000520_scale_indexes::Migration), ] } } diff --git a/migration/src/m0000520_scale_indexes.rs b/migration/src/m0000520_scale_indexes.rs new file mode 100644 index 000000000..8c0461d6c --- /dev/null +++ b/migration/src/m0000520_scale_indexes.rs @@ -0,0 +1,113 @@ +use sea_orm_migration::prelude::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +#[allow(deprecated)] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_index( + Index::create() + .table(Cvss3::Table) + .name(Indexes::Cvss3VulnIdIdx.to_string()) + .col(Cvss3::VulnerabilityId) + .to_owned(), + ) + .await?; + manager + .create_index( + Index::create() + .table(Cvss3::Table) + .name(Indexes::Cvss3AdvIdIdx.to_string()) + .col(Cvss3::AdvisoryId) + .to_owned(), + ) + .await?; + manager + .create_index( + Index::create() + .table(Cvss4::Table) + .name(Indexes::Cvss4VulnIdIdx.to_string()) + .col(Cvss4::VulnerabilityId) + .to_owned(), + ) + .await?; + manager + .create_index( + Index::create() + .table(Cvss4::Table) + .name(Indexes::Cvss4AdvIdIdx.to_string()) + .col(Cvss4::AdvisoryId) + .to_owned(), + ) + .await?; + + Ok(()) + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_index( + Index::drop() + .if_exists() + .table(Cvss4::Table) + .name(Indexes::Cvss4AdvIdIdx.to_string()) + .to_owned(), + ) + .await?; + manager + .drop_index( + Index::drop() + .if_exists() + .table(Cvss4::Table) + .name(Indexes::Cvss4VulnIdIdx.to_string()) + .to_owned(), + ) + .await?; + manager + .drop_index( + Index::drop() + .if_exists() + .table(Cvss3::Table) + .name(Indexes::Cvss3AdvIdIdx.to_string()) + .to_owned(), + ) + .await?; + manager + .drop_index( + Index::drop() + .if_exists() + .table(Cvss3::Table) + .name(Indexes::Cvss3VulnIdIdx.to_string()) + .to_owned(), + ) + .await?; + + Ok(()) + } +} + +#[allow(clippy::enum_variant_names)] +#[derive(DeriveIden)] +pub enum Indexes { + Cvss3VulnIdIdx, + Cvss3AdvIdIdx, + Cvss4VulnIdIdx, + Cvss4AdvIdIdx, +} + +#[derive(DeriveIden)] +pub enum Cvss3 { + Table, + AdvisoryId, + VulnerabilityId, +} + +#[derive(DeriveIden)] +pub enum Cvss4 { + Table, + AdvisoryId, + VulnerabilityId, +}