Skip to content

Commit

Permalink
fix: actually set a default value, instead of null
Browse files Browse the repository at this point in the history
Closes: #1089
  • Loading branch information
ctron committed Dec 10, 2024
1 parent b168e26 commit bcf4fc2
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions migration/src/m0000720_alter_sbom_fix_null_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

Expand All @@ -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[]"#
);
}
}

0 comments on commit bcf4fc2

Please sign in to comment.