diff --git a/entity/src/sbom_external_package_node.rs b/entity/src/sbom_external_package_node.rs new file mode 100644 index 000000000..91346f3e6 --- /dev/null +++ b/entity/src/sbom_external_package_node.rs @@ -0,0 +1,61 @@ +use deepsize::DeepSizeOf; +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)] +#[sea_orm(table_name = "sbom_external_package_node")] +pub struct Model { + #[sea_orm(primary_key)] + pub sbom_id: Uuid, + #[sea_orm(primary_key)] + pub node_id: String, + + pub external_node_id: String, + + pub external_node_type: ExternalPackageType, + + pub target_sbom_id: Option, + +} + +#[sea_orm(rs_type = "i32", db_type = "Integer")] +#[serde(rename_all = "snake_case")] +#[derive(DeepSizeOf)] +pub enum ExternalPackageType { + #[sea_orm(num_value = 0)] + SPDX, + #[sea_orm(num_value = 1)] + CDX, + #[sea_orm(num_value = 2)] + RH_PRODUCT_COMPONENT, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm( + belongs_to = "super::sbom::Entity", + from = "Column::SbomId", + to = "super::sbom::Column::SbomId" + )] + Sbom, + #[sea_orm( + belongs_to = "super::package_relates_to_package::Entity", + from = "Column::SbomId", + to = "super::package_relates_to_package::Column::SbomId", + on_condition = r#"super::package_relates_to_package::Column::Relationship.eq(crate::relationship::Relationship::Describes)"# + )] + DescribesSbom, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Sbom.def() + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::DescribesSbom.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/migration/src/lib.rs b/migration/src/lib.rs index 3d1de9eb7..4b9bc7f35 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -104,6 +104,7 @@ mod m0000840_add_relationship_14_15; mod m0000850_python_version; mod m0000860_normalise_relationships; mod m0000870_source_doc_indexes; +mod m0000880_create_sbom_external_package_node; #[cfg(feature = "ai")] pub mod ai; @@ -217,6 +218,7 @@ impl MigratorTrait for Migrator { Box::new(m0000850_python_version::Migration), Box::new(m0000860_normalise_relationships::Migration), Box::new(m0000870_source_doc_indexes::Migration), + Box::new(m0000880_create_sbom_external_package_node::Migration), ] } } diff --git a/migration/src/m0000880_create_sbom_external_package_node.rs b/migration/src/m0000880_create_sbom_external_package_node.rs new file mode 100644 index 000000000..451975bca --- /dev/null +++ b/migration/src/m0000880_create_sbom_external_package_node.rs @@ -0,0 +1,68 @@ +use sea_orm_migration::prelude::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +pub(crate) const _EXTERNAL_NODE_TYPE_ENUM: [(i32, &str); 3] = + [(0, "SPDX"), (1, "CDX"), (2, "RH_PRODUCT_COMPONENT")]; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_table( + Table::create() + .table(SbomExternalPackageNode::Table) + .col( + ColumnDef::new(SbomExternalPackageNode::SbomId) + .uuid() + .not_null(), + ) + .col( + ColumnDef::new(SbomExternalPackageNode::NodeId) + .string() + .not_null(), + ) + .primary_key( + Index::create() + .col(SbomExternalPackageNode::SbomId) + .col(SbomExternalPackageNode::NodeId), + ) + .col( + ColumnDef::new(SbomExternalPackageNode::ExternalNodeId) + .string() + .not_null(), + ) + .col( + ColumnDef::new(SbomExternalPackageNode::ExternalNodeType) + .integer() + .not_null(), + ) + .col(ColumnDef::new(SbomExternalPackageNode::TargetSbomId).uuid()) + .to_owned(), + ) + .await?; + + Ok(()) + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table( + Table::drop() + .table(SbomExternalPackageNode::Table) + .to_owned(), + ) + .await + } +} + +#[derive(DeriveIden)] +enum SbomExternalPackageNode { + Table, + SbomId, + NodeId, + ExternalNodeId, + ExternalNodeType, + TargetSbomId, +}