Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sbom license migration to update nulls #1176

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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[]
Loading