Skip to content

Commit

Permalink
fix(cherry-pick): fix type mismatch of compressed_binary column in fu…
Browse files Browse the repository at this point in the history
…nction table (#17312) (#17341)
  • Loading branch information
yezizp2012 authored Jun 19, 2024
1 parent 16c9759 commit bbd4dc7
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/meta/model_v2/migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod m20240417_062305_subscription_internal_table_name;
mod m20240418_142249_function_runtime;
mod m20240506_112555_subscription_partial_ckpt;
mod m20240525_090457_secret;
mod m20240618_072634_function_compressed_binary;

pub struct Migrator;

Expand All @@ -27,6 +28,7 @@ impl MigratorTrait for Migrator {
Box::new(m20240418_142249_function_runtime::Migration),
Box::new(m20240506_112555_subscription_partial_ckpt::Migration),
Box::new(m20240525_090457_secret::Migration),
Box::new(m20240618_072634_function_compressed_binary::Migration),
]
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use sea_orm_migration::prelude::*;

use crate::sea_orm::{DatabaseBackend, DbBackend, Statement};

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// Fix mismatch column `compressed_binary` type and do data migration
match manager.get_database_backend() {
DbBackend::MySql => {
// Creating function with compressed binary will fail in previous version, so we can
// safely assume that the column is always empty and we can just modify the column type
// without any data migration.
manager
.alter_table(
Table::alter()
.table(Function::Table)
.modify_column(
ColumnDef::new(Function::CompressedBinary).blob(BlobSize::Medium),
)
.to_owned(),
)
.await?;
}
DbBackend::Postgres => {
manager.get_connection().execute(Statement::from_string(
DatabaseBackend::Postgres,
"ALTER TABLE function ALTER COLUMN compressed_binary TYPE bytea USING compressed_binary::bytea",
)).await?;
}
DbBackend::Sqlite => {
// Sqlite does not support modifying column type, so we need to do data migration and column renaming.
// Note that: all these DDLs are not transactional, so if some of them fail, we need to manually run it again.
let conn = manager.get_connection();
conn.execute(Statement::from_string(
DatabaseBackend::Sqlite,
"ALTER TABLE function ADD COLUMN compressed_binary_new BLOB",
))
.await?;
conn.execute(Statement::from_string(
DatabaseBackend::Sqlite,
"UPDATE function SET compressed_binary_new = compressed_binary",
))
.await?;
conn.execute(Statement::from_string(
DatabaseBackend::Sqlite,
"ALTER TABLE function DROP COLUMN compressed_binary",
))
.await?;
conn.execute(Statement::from_string(
DatabaseBackend::Sqlite,
"ALTER TABLE function RENAME COLUMN compressed_binary_new TO compressed_binary",
))
.await?;
}
}

Ok(())
}

async fn down(&self, _manager: &SchemaManager) -> Result<(), DbErr> {
// DO nothing, the operations in `up` are idempotent and required to fix the column type mismatch.
Ok(())
}
}

#[derive(DeriveIden)]
enum Function {
Table,
CompressedBinary,
}

0 comments on commit bbd4dc7

Please sign in to comment.