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 6, 2025
1 parent a074606 commit c2ff34e
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 0 deletions.
60 changes: 60 additions & 0 deletions entity/src/sbom_external_node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use deepsize::DeepSizeOf;
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "sbom_external_node")]
pub struct Model {
#[sea_orm(primary_key)]
pub sbom_id: Uuid,
#[sea_orm(primary_key)]
pub node_id: String,
pub external_doc_ref: String,
pub external_node_ref: String,
pub external_type: ExternalType,
pub target_sbom_id: Option<Uuid>,

}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[sea_orm(rs_type = "i32", db_type = "Integer")]
#[serde(rename_all = "snake_case")]
#[derive(DeepSizeOf)]
pub enum ExternalType {
#[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 @@ -105,6 +105,7 @@ mod m0000850_python_version;
mod m0000860_normalise_relationships;
mod m0000870_source_doc_indexes;
mod m0000880_org_name_index;
mod m0000890_create_sbom_external_node;

#[cfg(feature = "ai")]
pub mod ai;
Expand Down Expand Up @@ -219,6 +220,7 @@ impl MigratorTrait for Migrator {
Box::new(m0000860_normalise_relationships::Migration),
Box::new(m0000870_source_doc_indexes::Migration),
Box::new(m0000880_org_name_index::Migration),
Box::new(m0000890_create_sbom_external_node::Migration),
]
}
}
Expand Down
108 changes: 108 additions & 0 deletions migration/src/m0000890_create_sbom_external_node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

pub(crate) const _EXTERNAL_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(SbomExternalNode::Table)
.col(ColumnDef::new(SbomExternalNode::SbomId).uuid().not_null())
.col(ColumnDef::new(SbomExternalNode::NodeId).string().not_null())
.primary_key(
Index::create()
.col(SbomExternalNode::SbomId)
.col(SbomExternalNode::NodeId),
)
.col(
ColumnDef::new(SbomExternalNode::ExternalDocRef)
.string()
.not_null(),
)
.col(
ColumnDef::new(SbomExternalNode::ExternalNodeRef)
.string()
.not_null(),
)
.col(
ColumnDef::new(SbomExternalNode::ExternalType)
.integer()
.not_null(),
)
.col(ColumnDef::new(SbomExternalNode::TargetSbomId).uuid())
.to_owned(),
)
.await?;

manager
.create_index(
Index::create()
.table(SbomExternalNode::Table)
.name(Indexes::SbomExternalNodeExternalDocRefIdx.to_string())
.col(SbomExternalNode::ExternalType)
.to_owned(),
)
.await?;

manager
.create_index(
Index::create()
.table(SbomExternalNode::Table)
.name(Indexes::SbomExternalNodeExternalNodeRefIdx.to_string())
.col(SbomExternalNode::ExternalDocRef)
.to_owned(),
)
.await?;

Ok(())
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_index(
Index::drop()
.if_exists()
.table(SbomExternalNode::Table)
.name(Indexes::SbomExternalNodeExternalNodeRefIdx.to_string())
.to_owned(),
)
.await?;

manager
.drop_index(
Index::drop()
.if_exists()
.table(SbomExternalNode::Table)
.name(Indexes::SbomExternalNodeExternalDocRefIdx.to_string())
.to_owned(),
)
.await?;

manager
.drop_table(Table::drop().table(SbomExternalNode::Table).to_owned())
.await
}
}

#[derive(DeriveIden)]
enum Indexes {
SbomExternalNodeExternalDocRefIdx,
SbomExternalNodeExternalNodeRefIdx,
}

#[derive(DeriveIden)]
enum SbomExternalNode {
Table,
SbomId,
NodeId,
ExternalDocRef,
ExternalNodeRef,
ExternalType,
TargetSbomId,
}

0 comments on commit c2ff34e

Please sign in to comment.