From bcf4fc23c06e5664e55d44dc44cd7b7c0e1f4f05 Mon Sep 17 00:00:00 2001 From: Jens Reimann Date: Tue, 10 Dec 2024 09:06:19 +0100 Subject: [PATCH] fix: actually set a default value, instead of `null` Closes: https://github.com/trustification/trustify/issues/1089 --- .../src/m0000720_alter_sbom_fix_null_array.rs | 46 +++++++++++++------ 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/migration/src/m0000720_alter_sbom_fix_null_array.rs b/migration/src/m0000720_alter_sbom_fix_null_array.rs index 1ae6555f4..dc71b1a2c 100644 --- a/migration/src/m0000720_alter_sbom_fix_null_array.rs +++ b/migration/src/m0000720_alter_sbom_fix_null_array.rs @@ -6,21 +6,7 @@ pub struct Migration; #[async_trait::async_trait] impl MigrationTrait for Migration { async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { - manager - .alter_table( - Table::alter() - .table(Sbom::Table) - .modify_column( - ColumnDef::new(Sbom::DataLicenses) - .array(ColumnType::Text) - .not_null() - .default(Value::Array(ArrayType::String, None)) - .to_owned(), - ) - .to_owned(), - ) - .await?; - + manager.alter_table(Self::alter_table()).await?; Ok(()) } @@ -29,8 +15,38 @@ impl MigrationTrait for Migration { } } +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[]"# + ); + } +}