Skip to content

Commit

Permalink
fix: sbom license migration to update nulls
Browse files Browse the repository at this point in the history
  • Loading branch information
dejanb committed Jan 22, 2025
1 parent f54d171 commit b048e4b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 37 deletions.
44 changes: 7 additions & 37 deletions migration/src/m0000720_alter_sbom_fix_null_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,17 @@ pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager.alter_table(Self::alter_table()).await?;
manager
.get_connection()
.execute_unprepared(include_str!(
"m0000720_alter_sbom_fix_null_array/migration_up.sql"
))
.await?;

Ok(())
}

async fn down(&self, _manager: &SchemaManager) -> Result<(), DbErr> {
Ok(())
}
}

impl Migration {
fn alter_table() -> TableAlterStatement {
Table::alter()
.table(Sbom::Table)
.modify_column(
ColumnDef::new(Sbom::DataLicenses)
.array(ColumnType::Text)
.not_null()
.default(SimpleExpr::Custom("ARRAY[]::text[]".to_string()))
.to_owned(),
)
.to_owned()
}
}

#[derive(DeriveIden)]
enum Sbom {
Table,
DataLicenses,
}

#[cfg(test)]
mod test {
use crate::m0000720_alter_sbom_fix_null_array::Migration;
use crate::PostgresQueryBuilder;

#[test]
fn test() {
let sql = Migration::alter_table().build(PostgresQueryBuilder);
assert_eq!(
sql,
r#"ALTER TABLE "sbom" ALTER COLUMN "data_licenses" TYPE text[], ALTER COLUMN "data_licenses" SET NOT NULL, ALTER COLUMN "data_licenses" SET DEFAULT ARRAY[]::text[]"#
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE "sbom"
ALTER COLUMN "data_licenses" TYPE text[] USING COALESCE("data_licenses", ARRAY[]::text[]),
ALTER COLUMN "data_licenses" SET NOT NULL,
ALTER COLUMN "data_licenses" SET DEFAULT ARRAY[]::text[]

0 comments on commit b048e4b

Please sign in to comment.