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: codegen write binary column definition #1529

Merged
merged 1 commit into from
Mar 10, 2023
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
61 changes: 54 additions & 7 deletions sea-orm-codegen/src/entity/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ impl Column {
ColumnType::Text => Some("Text".to_owned()),
ColumnType::JsonBinary => Some("JsonBinary".to_owned()),
ColumnType::Custom(iden) => Some(format!("custom(\"{}\")", iden.to_string())),
ColumnType::Binary(BlobSize::Blob(None)) => Some("Binary(BlobSize::Blob(None))".into()),
ColumnType::Binary(BlobSize::Blob(Some(s))) => {
Some(format!("Binary(BlobSize::Blob(Some({s})))"))
}
ColumnType::Binary(BlobSize::Tiny) => Some("Binary(BlobSize::Tiny)".into()),
ColumnType::Binary(BlobSize::Medium) => Some("Binary(BlobSize::Medium)".into()),
ColumnType::Binary(BlobSize::Long) => Some("Binary(BlobSize::Long)".into()),
ColumnType::VarBinary(s) => Some(format!("VarBinary({s})")),
_ => None,
};
col_type.map(|ty| quote! { column_type = #ty })
Expand Down Expand Up @@ -134,12 +142,24 @@ impl Column {
}
ColumnType::Time => quote! { ColumnType::Time },
ColumnType::Date => quote! { ColumnType::Date },
ColumnType::Binary(BlobSize::Blob(_)) | ColumnType::VarBinary(_) => {
quote! { ColumnType::Binary }
ColumnType::Binary(BlobSize::Blob(None)) => {
quote! { ColumnType::Binary(sea_orm::sea_query::BlobSize::Blob(None)) }
}
ColumnType::Binary(BlobSize::Blob(Some(s))) => {
quote! { ColumnType::Binary(sea_orm::sea_query::BlobSize::Blob(Some(#s))) }
}
ColumnType::Binary(BlobSize::Tiny) => {
quote! { ColumnType::Binary(sea_orm::sea_query::BlobSize::Tiny) }
}
ColumnType::Binary(BlobSize::Medium) => {
quote! { ColumnType::Binary(sea_orm::sea_query::BlobSize::Medium) }
}
ColumnType::Binary(BlobSize::Long) => {
quote! { ColumnType::Binary(sea_orm::sea_query::BlobSize::Long) }
}
ColumnType::VarBinary(s) => {
quote! { ColumnType::VarBinary(#s) }
}
ColumnType::Binary(BlobSize::Tiny) => quote! { ColumnType::TinyBinary },
ColumnType::Binary(BlobSize::Medium) => quote! { ColumnType::MediumBinary },
ColumnType::Binary(BlobSize::Long) => quote! { ColumnType::LongBinary },
ColumnType::Boolean => quote! { ColumnType::Boolean },
ColumnType::Money(s) => match s {
Some((s1, s2)) => quote! { ColumnType::Money(Some((#s1, #s2))) },
Expand Down Expand Up @@ -302,6 +322,13 @@ mod tests {
make_col!("cake-filling-id", ColumnType::Float),
make_col!("CAKE_FILLING_ID", ColumnType::Double),
make_col!("CAKE-FILLING-ID", ColumnType::Binary(BlobSize::Blob(None))),
make_col!(
"CAKE-FILLING-ID",
ColumnType::Binary(BlobSize::Blob(Some(10)))
),
make_col!("CAKE-FILLING-ID", ColumnType::Binary(BlobSize::Tiny)),
make_col!("CAKE-FILLING-ID", ColumnType::Binary(BlobSize::Medium)),
make_col!("CAKE-FILLING-ID", ColumnType::Binary(BlobSize::Long)),
make_col!("CAKE-FILLING-ID", ColumnType::VarBinary(10)),
make_col!("CAKE", ColumnType::Boolean),
make_col!("date", ColumnType::Date),
Expand Down Expand Up @@ -330,6 +357,10 @@ mod tests {
"cake_filling_id",
"cake_filling_id",
"cake_filling_id",
"cake_filling_id",
"cake_filling_id",
"cake_filling_id",
"cake_filling_id",
"cake",
"date",
"time",
Expand Down Expand Up @@ -360,6 +391,10 @@ mod tests {
"CakeFillingId",
"CakeFillingId",
"CakeFillingId",
"CakeFillingId",
"CakeFillingId",
"CakeFillingId",
"CakeFillingId",
"Cake",
"Date",
"Time",
Expand Down Expand Up @@ -391,6 +426,10 @@ mod tests {
"f64",
"Vec<u8>",
"Vec<u8>",
"Vec<u8>",
"Vec<u8>",
"Vec<u8>",
"Vec<u8>",
"bool",
"Date",
"Time",
Expand Down Expand Up @@ -434,6 +473,10 @@ mod tests {
"f64",
"Vec<u8>",
"Vec<u8>",
"Vec<u8>",
"Vec<u8>",
"Vec<u8>",
"Vec<u8>",
"bool",
"TimeDate",
"TimeTime",
Expand Down Expand Up @@ -474,8 +517,12 @@ mod tests {
"ColumnType::BigUnsigned.def()",
"ColumnType::Float.def()",
"ColumnType::Double.def()",
"ColumnType::Binary.def()",
"ColumnType::Binary.def()",
"ColumnType::Binary(sea_orm::sea_query::BlobSize::Blob(None)).def()",
"ColumnType::Binary(sea_orm::sea_query::BlobSize::Blob(Some(10u32))).def()",
"ColumnType::Binary(sea_orm::sea_query::BlobSize::Tiny).def()",
"ColumnType::Binary(sea_orm::sea_query::BlobSize::Medium).def()",
"ColumnType::Binary(sea_orm::sea_query::BlobSize::Long).def()",
"ColumnType::VarBinary(10u32).def()",
"ColumnType::Boolean.def()",
"ColumnType::Date.def()",
"ColumnType::Time.def()",
Expand Down
25 changes: 25 additions & 0 deletions tests/common/features/binary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use sea_orm::{entity::prelude::*, sea_query::BlobSize};

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "binary")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
#[sea_orm(column_type = "Binary(BlobSize::Blob(None))")]
pub binary: Vec<u8>,
#[sea_orm(column_type = "Binary(BlobSize::Blob(Some(10)))")]
pub binary_10: Vec<u8>,
#[sea_orm(column_type = "Binary(BlobSize::Tiny)")]
pub binary_tiny: Vec<u8>,
#[sea_orm(column_type = "Binary(BlobSize::Medium)")]
pub binary_medium: Vec<u8>,
#[sea_orm(column_type = "Binary(BlobSize::Long)")]
pub binary_long: Vec<u8>,
#[sea_orm(column_type = "VarBinary(10)")]
pub var_binary: Vec<u8>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}
2 changes: 2 additions & 0 deletions tests/common/features/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod active_enum;
pub mod active_enum_child;
pub mod applog;
pub mod binary;
pub mod byte_primary_key;
pub mod collection;
pub mod custom_active_model;
Expand All @@ -23,6 +24,7 @@ pub mod uuid_fmt;
pub use active_enum::Entity as ActiveEnum;
pub use active_enum_child::Entity as ActiveEnumChild;
pub use applog::Entity as Applog;
pub use binary::Entity as Binary;
pub use byte_primary_key::Entity as BytePrimaryKey;
pub use collection::Entity as Collection;
pub use edit_log::Entity as EditLog;
Expand Down
46 changes: 45 additions & 1 deletion tests/common/features/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use sea_orm::{
error::*, sea_query, ConnectionTrait, DatabaseConnection, DbBackend, DbConn, EntityName,
ExecResult, Schema,
};
use sea_query::{extension::postgres::Type, Alias, ColumnDef, ForeignKeyCreateStatement, IntoIden};
use sea_query::{
extension::postgres::Type, Alias, BlobSize, ColumnDef, ForeignKeyCreateStatement, IntoIden,
};

pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {
let db_backend = db.get_database_backend();
Expand Down Expand Up @@ -45,6 +47,7 @@ pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {
create_uuid_fmt_table(db).await?;
create_edit_log_table(db).await?;
create_teas_table(db).await?;
create_binary_table(db).await?;

if DbBackend::Postgres == db_backend {
create_collection_table(db).await?;
Expand Down Expand Up @@ -515,3 +518,44 @@ pub async fn create_teas_table(db: &DbConn) -> Result<ExecResult, DbErr> {

create_table(db, &create_table_stmt, Teas).await
}

pub async fn create_binary_table(db: &DbConn) -> Result<ExecResult, DbErr> {
let create_table_stmt = sea_query::Table::create()
.table(binary::Entity.table_ref())
.col(
ColumnDef::new(binary::Column::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(binary::Column::Binary).binary().not_null())
.col(
ColumnDef::new(binary::Column::Binary10)
.blob(BlobSize::Blob(Some(10)))
.not_null(),
)
.col(
ColumnDef::new(binary::Column::BinaryTiny)
.blob(BlobSize::Tiny)
.not_null(),
)
.col(
ColumnDef::new(binary::Column::BinaryMedium)
.blob(BlobSize::Medium)
.not_null(),
)
.col(
ColumnDef::new(binary::Column::BinaryLong)
.blob(BlobSize::Long)
.not_null(),
)
.col(
ColumnDef::new(binary::Column::VarBinary)
.var_binary(10)
.not_null(),
)
.to_owned();

create_table(db, &create_table_stmt, Binary).await
}