Skip to content

Commit

Permalink
create sbom_external_package_node
Browse files Browse the repository at this point in the history
  • Loading branch information
JimFuller-RedHat committed Feb 4, 2025
1 parent 0b70b91 commit 103cfe5
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
61 changes: 61 additions & 0 deletions entity/src/sbom_external_package_node.rs
Original file line number Diff line number Diff line change
@@ -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<Uuid>,

}

#[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<super::sbom::Entity> for Entity {
fn to() -> RelationDef {
Relation::Sbom.def()
}
}

impl Related<super::package_relates_to_package::Entity> for Entity {
fn to() -> RelationDef {
Relation::DescribesSbom.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
2 changes: 2 additions & 0 deletions migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
]
}
}
Expand Down
68 changes: 68 additions & 0 deletions migration/src/m0000880_create_sbom_external_package_node.rs
Original file line number Diff line number Diff line change
@@ -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,
}

0 comments on commit 103cfe5

Please sign in to comment.