From b775027feee0411ef70eec14474fb5a46ada0e36 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Tue, 5 Mar 2024 15:31:58 +0800 Subject: [PATCH] Explicit `StringLen` on `ColumnType::String` (#2123) --- examples/basic/src/example_filling.rs | 2 +- examples/basic/src/example_fruit.rs | 2 +- .../1143/src/entity/sea_orm_active_enums.rs | 2 +- issues/1599/entity/src/filling.rs | 2 +- sea-orm-codegen/src/entity/base_entity.rs | 4 +-- sea-orm-codegen/src/entity/column.rs | 14 +++++----- sea-orm-codegen/src/entity/writer.rs | 8 +++--- sea-orm-codegen/tests/expanded/filling.rs | 2 +- sea-orm-codegen/tests/expanded/fruit.rs | 2 +- sea-orm-codegen/tests/expanded/vendor.rs | 2 +- .../expanded_with_schema_name/filling.rs | 2 +- .../tests/expanded_with_schema_name/fruit.rs | 2 +- .../tests/expanded_with_schema_name/vendor.rs | 2 +- sea-orm-macros/src/lib.rs | 8 +++--- src/entity/active_enum.rs | 28 +++++++++++++------ src/entity/column.rs | 8 ++++-- src/entity/mod.rs | 2 +- src/entity/primary_key.rs | 2 +- src/tests_cfg/cake_expanded.rs | 2 +- src/tests_cfg/filling.rs | 2 +- src/tests_cfg/lunch_set_expanded.rs | 2 +- .../features/dyn_table_name_lazy_static.rs | 2 +- tests/common/features/event_trigger.rs | 2 +- tests/common/features/schema.rs | 9 ++++-- tests/common/features/sea_orm_active_enums.rs | 2 +- tests/value_type_tests.rs | 2 +- 26 files changed, 67 insertions(+), 50 deletions(-) diff --git a/examples/basic/src/example_filling.rs b/examples/basic/src/example_filling.rs index fe054506..9d725ec6 100644 --- a/examples/basic/src/example_filling.rs +++ b/examples/basic/src/example_filling.rs @@ -55,7 +55,7 @@ impl ColumnTrait for Column { fn def(&self) -> ColumnDef { match self { Self::Id => ColumnType::Integer.def(), - Self::Name => ColumnType::string(None).def(), + Self::Name => ColumnType::String(StringLen::None).def(), } } } diff --git a/examples/basic/src/example_fruit.rs b/examples/basic/src/example_fruit.rs index 03875329..d0ffebf7 100644 --- a/examples/basic/src/example_fruit.rs +++ b/examples/basic/src/example_fruit.rs @@ -62,7 +62,7 @@ impl ColumnTrait for Column { fn def(&self) -> ColumnDef { match self { Self::Id => ColumnType::Integer.def(), - Self::Name => ColumnType::string(None).def(), + Self::Name => ColumnType::String(StringLen::None).def(), Self::CakeId => ColumnType::Integer.def(), } } diff --git a/issues/1143/src/entity/sea_orm_active_enums.rs b/issues/1143/src/entity/sea_orm_active_enums.rs index 887edf62..cf7a5d44 100644 --- a/issues/1143/src/entity/sea_orm_active_enums.rs +++ b/issues/1143/src/entity/sea_orm_active_enums.rs @@ -1,7 +1,7 @@ use sea_orm::entity::prelude::*; #[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum)] -#[sea_orm(rs_type = "String", db_type = "string(Some(1))")] +#[sea_orm(rs_type = "String", db_type = "String(StringLen::N(1))")] pub enum Category { #[sea_orm(string_value = "B")] Big, diff --git a/issues/1599/entity/src/filling.rs b/issues/1599/entity/src/filling.rs index 30d1009f..afef9eeb 100644 --- a/issues/1599/entity/src/filling.rs +++ b/issues/1599/entity/src/filling.rs @@ -55,7 +55,7 @@ impl ColumnTrait for Column { fn def(&self) -> ColumnDef { match self { Self::Id => ColumnType::Integer.def(), - Self::Name => ColumnType::string(None).def(), + Self::Name => ColumnType::String(StringLen::None).def(), Self::VendorId => ColumnType::Integer.def().nullable(), } } diff --git a/sea-orm-codegen/src/entity/base_entity.rs b/sea-orm-codegen/src/entity/base_entity.rs index 72ea224e..240e94ce 100644 --- a/sea-orm-codegen/src/entity/base_entity.rs +++ b/sea-orm-codegen/src/entity/base_entity.rs @@ -267,7 +267,7 @@ impl Entity { #[cfg(test)] mod tests { use quote::{format_ident, quote}; - use sea_query::{ColumnType, ForeignKeyAction}; + use sea_query::{ColumnType, ForeignKeyAction, StringLen}; use crate::{Column, DateTimeCrate, Entity, PrimaryKey, Relation, RelationType}; @@ -284,7 +284,7 @@ mod tests { }, Column { name: "name".to_owned(), - col_type: ColumnType::string(None), + col_type: ColumnType::String(StringLen::None), auto_increment: false, not_null: false, unique: false, diff --git a/sea-orm-codegen/src/entity/column.rs b/sea-orm-codegen/src/entity/column.rs index ce421050..e8e030ef 100644 --- a/sea-orm-codegen/src/entity/column.rs +++ b/sea-orm-codegen/src/entity/column.rs @@ -115,8 +115,8 @@ impl Column { None => quote! { ColumnType::Char(None) }, }, ColumnType::String(s) => match s { - StringLen::N(s) => quote! { ColumnType::string(Some(#s)) }, - StringLen::None => quote! { ColumnType::string(None) }, + StringLen::N(s) => quote! { ColumnType::String(StringLen::N(#s)) }, + StringLen::None => quote! { ColumnType::String(StringLen::None) }, StringLen::Max => quote! { ColumnType::String(StringLen::Max) }, }, ColumnType::Text => quote! { ColumnType::Text }, @@ -302,8 +302,8 @@ mod tests { }; } vec![ - make_col!("id", ColumnType::string(Some(255))), - make_col!("id", ColumnType::string(None)), + make_col!("id", ColumnType::String(StringLen::N(255))), + make_col!("id", ColumnType::String(StringLen::None)), make_col!( "cake_id", ColumnType::Custom(SeaRc::new(Alias::new("cus_col"))) @@ -493,8 +493,8 @@ mod tests { fn test_get_def() { let columns = setup(); let col_defs = vec![ - "ColumnType::string(Some(255u32)).def()", - "ColumnType::string(None).def()", + "ColumnType::String(StringLen::N(255u32)).def()", + "ColumnType::String(StringLen::None).def()", "ColumnType::custom(\"cus_col\").def()", "ColumnType::TinyInteger.def()", "ColumnType::TinyUnsigned.def()", @@ -681,7 +681,7 @@ mod tests { assert_eq!( column.get_def().to_string(), quote! { - ColumnType::string(None).def().null() + ColumnType::String(StringLen::None).def().null() } .to_string() ); diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index f85f956d..edfe8c7c 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -841,7 +841,7 @@ mod tests { use pretty_assertions::assert_eq; use proc_macro2::TokenStream; use quote::quote; - use sea_query::{Alias, ColumnType, ForeignKeyAction, RcOrArc, SeaRc}; + use sea_query::{Alias, ColumnType, ForeignKeyAction, RcOrArc, SeaRc, StringLen}; use std::io::{self, BufRead, BufReader, Read}; fn setup() -> Vec { @@ -993,7 +993,7 @@ mod tests { }, Column { name: "name".to_owned(), - col_type: ColumnType::string(Some(255)), + col_type: ColumnType::String(StringLen::N(255)), auto_increment: false, not_null: true, unique: false, @@ -1020,7 +1020,7 @@ mod tests { }, Column { name: "name".to_owned(), - col_type: ColumnType::string(Some(255)), + col_type: ColumnType::String(StringLen::N(255)), auto_increment: false, not_null: true, unique: false, @@ -1074,7 +1074,7 @@ mod tests { }, Column { name: "_name_".to_owned(), - col_type: ColumnType::string(Some(255)), + col_type: ColumnType::String(StringLen::N(255)), auto_increment: false, not_null: true, unique: false, diff --git a/sea-orm-codegen/tests/expanded/filling.rs b/sea-orm-codegen/tests/expanded/filling.rs index c359b564..b7fc8767 100644 --- a/sea-orm-codegen/tests/expanded/filling.rs +++ b/sea-orm-codegen/tests/expanded/filling.rs @@ -44,7 +44,7 @@ impl ColumnTrait for Column { fn def(&self) -> ColumnDef { match self { Self::Id => ColumnType::Integer.def(), - Self::Name => ColumnType::string(Some(255u32)).def(), + Self::Name => ColumnType::String(StringLen::N(255u32)).def(), } } } diff --git a/sea-orm-codegen/tests/expanded/fruit.rs b/sea-orm-codegen/tests/expanded/fruit.rs index 6de130f3..ed88853e 100644 --- a/sea-orm-codegen/tests/expanded/fruit.rs +++ b/sea-orm-codegen/tests/expanded/fruit.rs @@ -49,7 +49,7 @@ impl ColumnTrait for Column { fn def(&self) -> ColumnDef { match self { Self::Id => ColumnType::Integer.def(), - Self::Name => ColumnType::string(Some(255u32)).def(), + Self::Name => ColumnType::String(StringLen::N(255u32)).def(), Self::CakeId => ColumnType::Integer.def().null(), } } diff --git a/sea-orm-codegen/tests/expanded/vendor.rs b/sea-orm-codegen/tests/expanded/vendor.rs index 793f9c1c..c2f25b14 100644 --- a/sea-orm-codegen/tests/expanded/vendor.rs +++ b/sea-orm-codegen/tests/expanded/vendor.rs @@ -50,7 +50,7 @@ impl ColumnTrait for Column { fn def(&self) -> ColumnDef { match self { Self::Id => ColumnType::Integer.def(), - Self::Name => ColumnType::string(Some(255u32)).def(), + Self::Name => ColumnType::String(StringLen::N(255u32)).def(), Self::FruitId => ColumnType::Integer.def().null(), } } diff --git a/sea-orm-codegen/tests/expanded_with_schema_name/filling.rs b/sea-orm-codegen/tests/expanded_with_schema_name/filling.rs index 606e2018..4aa76b49 100644 --- a/sea-orm-codegen/tests/expanded_with_schema_name/filling.rs +++ b/sea-orm-codegen/tests/expanded_with_schema_name/filling.rs @@ -48,7 +48,7 @@ impl ColumnTrait for Column { fn def(&self) -> ColumnDef { match self { Self::Id => ColumnType::Integer.def(), - Self::Name => ColumnType::string(Some(255u32)).def(), + Self::Name => ColumnType::String(StringLen::N(255u32)).def(), } } } diff --git a/sea-orm-codegen/tests/expanded_with_schema_name/fruit.rs b/sea-orm-codegen/tests/expanded_with_schema_name/fruit.rs index 57e2cc15..1b3b73dd 100644 --- a/sea-orm-codegen/tests/expanded_with_schema_name/fruit.rs +++ b/sea-orm-codegen/tests/expanded_with_schema_name/fruit.rs @@ -53,7 +53,7 @@ impl ColumnTrait for Column { fn def(&self) -> ColumnDef { match self { Self::Id => ColumnType::Integer.def(), - Self::Name => ColumnType::string(Some(255u32)).def(), + Self::Name => ColumnType::String(StringLen::N(255u32)).def(), Self::CakeId => ColumnType::Integer.def().null(), } } diff --git a/sea-orm-codegen/tests/expanded_with_schema_name/vendor.rs b/sea-orm-codegen/tests/expanded_with_schema_name/vendor.rs index 14e71492..9736c576 100644 --- a/sea-orm-codegen/tests/expanded_with_schema_name/vendor.rs +++ b/sea-orm-codegen/tests/expanded_with_schema_name/vendor.rs @@ -54,7 +54,7 @@ impl ColumnTrait for Column { fn def(&self) -> ColumnDef { match self { Self::Id => ColumnType::Integer.def(), - Self::Name => ColumnType::string(Some(255u32)).def(), + Self::Name => ColumnType::String(StringLen::N(255u32)).def(), Self::FruitId => ColumnType::Integer.def().null(), } } diff --git a/sea-orm-macros/src/lib.rs b/sea-orm-macros/src/lib.rs index 23aa197f..db7e84b3 100644 --- a/sea-orm-macros/src/lib.rs +++ b/sea-orm-macros/src/lib.rs @@ -60,7 +60,7 @@ mod strum; /// # fn def(&self) -> ColumnDef { /// # match self { /// # Self::Id => ColumnType::Integer.def(), -/// # Self::Name => ColumnType::string(None).def(), +/// # Self::Name => ColumnType::String(StringLen::None).def(), /// # } /// # } /// # } @@ -344,7 +344,7 @@ pub fn derive_custom_column(input: TokenStream) -> TokenStream { /// # fn def(&self) -> ColumnDef { /// # match self { /// # Self::Id => ColumnType::Integer.def(), -/// # Self::Name => ColumnType::string(None).def(), +/// # Self::Name => ColumnType::String(StringLen::None).def(), /// # } /// # } /// # } @@ -417,7 +417,7 @@ pub fn derive_model(input: TokenStream) -> TokenStream { /// # fn def(&self) -> ColumnDef { /// # match self { /// # Self::Id => ColumnType::Integer.def(), -/// # Self::Name => ColumnType::string(None).def(), +/// # Self::Name => ColumnType::String(StringLen::None).def(), /// # } /// # } /// # } @@ -503,7 +503,7 @@ pub fn derive_into_active_model(input: TokenStream) -> TokenStream { /// # fn def(&self) -> ColumnDef { /// # match self { /// # Self::Id => ColumnType::Integer.def(), -/// # Self::Name => ColumnType::string(None).def(), +/// # Self::Name => ColumnType::String(StringLen::None).def(), /// # } /// # } /// # } diff --git a/src/entity/active_enum.rs b/src/entity/active_enum.rs index 7425913b..2222aff6 100644 --- a/src/entity/active_enum.rs +++ b/src/entity/active_enum.rs @@ -20,7 +20,7 @@ use sea_query::{DynIden, Expr, Nullable, SimpleExpr, Value, ValueType}; /// #[derive(Debug, PartialEq, EnumIter, DeriveActiveEnum, DeriveDisplay)] /// #[sea_orm( /// rs_type = "String", -/// db_type = "string(Some(1))", +/// db_type = "String(StringLen::N(1))", /// enum_name = "category" /// )] /// pub enum DeriveCategory { @@ -74,7 +74,7 @@ use sea_query::{DynIden, Expr, Nullable, SimpleExpr, Value, ValueType}; /// /// fn db_type() -> ColumnDef { /// // The macro attribute `db_type` is being pasted here -/// ColumnType::string(Some(1)).def() +/// ColumnType::String(StringLen::N(1)).def() /// } /// } /// ``` @@ -86,7 +86,7 @@ use sea_query::{DynIden, Expr, Nullable, SimpleExpr, Value, ValueType}; /// /// // Define the `Category` active enum /// #[derive(Debug, Clone, PartialEq, EnumIter, DeriveActiveEnum, DeriveDisplay)] -/// #[sea_orm(rs_type = "String", db_type = "string(Some(1))")] +/// #[sea_orm(rs_type = "String", db_type = "String(StringLen::N(1))")] /// pub enum Category { /// #[sea_orm(string_value = "B")] /// Big, @@ -205,7 +205,11 @@ where #[cfg(test)] mod tests { use crate as sea_orm; - use crate::{error::*, sea_query::SeaRc, *}; + use crate::{ + error::*, + sea_query::{SeaRc, StringLen}, + *, + }; use pretty_assertions::assert_eq; #[test] @@ -246,14 +250,14 @@ mod tests { } fn db_type() -> ColumnDef { - ColumnType::string(Some(1)).def() + ColumnType::String(StringLen::N(1)).def() } } #[derive(Debug, PartialEq, Eq, EnumIter, DeriveActiveEnum, DeriveDisplay)] #[sea_orm( rs_type = "String", - db_type = "string(Some(1))", + db_type = "String(StringLen::N(1))", enum_name = "category" )] pub enum DeriveCategory { @@ -293,8 +297,14 @@ mod tests { Some(DeriveCategory::Small) ); - assert_eq!(Category::db_type(), ColumnType::string(Some(1)).def()); - assert_eq!(DeriveCategory::db_type(), ColumnType::string(Some(1)).def()); + assert_eq!( + Category::db_type(), + ColumnType::String(StringLen::N(1)).def() + ); + assert_eq!( + DeriveCategory::db_type(), + ColumnType::String(StringLen::N(1)).def() + ); assert_eq!( Category::name().to_string(), @@ -496,7 +506,7 @@ mod tests { #[derive(Clone, Debug, PartialEq, EnumIter, DeriveActiveEnum, DeriveDisplay)] #[sea_orm( rs_type = "String", - db_type = "string(None)", + db_type = "String(StringLen::None)", enum_name = "conflicting_string_values" )] pub enum ConflictingStringValues { diff --git a/src/entity/column.rs b/src/entity/column.rs index 8256557e..5a2a94c6 100644 --- a/src/entity/column.rs +++ b/src/entity/column.rs @@ -620,11 +620,15 @@ mod tests { ); assert_eq!( hello::Column::Twelve.def(), - ColumnType::string(None).def().default("twelve_value") + ColumnType::String(StringLen::None) + .def() + .default("twelve_value") ); assert_eq!( hello::Column::TwelveTwo.def(), - ColumnType::string(None).def().default("twelve_value") + ColumnType::String(StringLen::None) + .def() + .default("twelve_value") ); } diff --git a/src/entity/mod.rs b/src/entity/mod.rs index 13137822..d58d4a4b 100644 --- a/src/entity/mod.rs +++ b/src/entity/mod.rs @@ -80,7 +80,7 @@ /// fn def(&self) -> ColumnDef { /// match self { /// Self::Id => ColumnType::Integer.def(), -/// Self::Name => ColumnType::string(None).def(), +/// Self::Name => ColumnType::String(StringLen::None).def(), /// } /// } /// } diff --git a/src/entity/primary_key.rs b/src/entity/primary_key.rs index b4e69ca1..ee73f3d0 100644 --- a/src/entity/primary_key.rs +++ b/src/entity/primary_key.rs @@ -170,7 +170,7 @@ mod tests { #[derive(Clone, Debug, PartialEq, Eq, EnumIter, DeriveActiveEnum)] #[sea_orm( rs_type = "String", - db_type = "string(Some(1))", + db_type = "String(StringLen::N(1))", enum_name = "category" )] pub enum DeriveCategory { diff --git a/src/tests_cfg/cake_expanded.rs b/src/tests_cfg/cake_expanded.rs index 7675a765..8e301fab 100644 --- a/src/tests_cfg/cake_expanded.rs +++ b/src/tests_cfg/cake_expanded.rs @@ -46,7 +46,7 @@ impl ColumnTrait for Column { fn def(&self) -> ColumnDef { match self { Self::Id => ColumnType::Integer.def(), - Self::Name => ColumnType::string(None).def(), + Self::Name => ColumnType::String(StringLen::None).def(), } } } diff --git a/src/tests_cfg/filling.rs b/src/tests_cfg/filling.rs index 055e2421..9eff6475 100644 --- a/src/tests_cfg/filling.rs +++ b/src/tests_cfg/filling.rs @@ -58,7 +58,7 @@ impl ColumnTrait for Column { fn def(&self) -> ColumnDef { match self { Self::Id => ColumnType::Integer.def(), - Self::Name => ColumnType::string(None).def(), + Self::Name => ColumnType::String(StringLen::None).def(), Self::VendorId => ColumnType::Integer.def().nullable(), } } diff --git a/src/tests_cfg/lunch_set_expanded.rs b/src/tests_cfg/lunch_set_expanded.rs index 0a904dba..033d5e50 100644 --- a/src/tests_cfg/lunch_set_expanded.rs +++ b/src/tests_cfg/lunch_set_expanded.rs @@ -49,7 +49,7 @@ impl ColumnTrait for Column { fn def(&self) -> ColumnDef { match self { Self::Id => ColumnType::Integer.def(), - Self::Name => ColumnType::string(None).def(), + Self::Name => ColumnType::String(StringLen::None).def(), Self::Tea => Tea::db_type().def(), } } diff --git a/tests/common/features/dyn_table_name_lazy_static.rs b/tests/common/features/dyn_table_name_lazy_static.rs index ada3b49e..b8ef55f2 100644 --- a/tests/common/features/dyn_table_name_lazy_static.rs +++ b/tests/common/features/dyn_table_name_lazy_static.rs @@ -48,7 +48,7 @@ impl ColumnTrait for Column { fn def(&self) -> ColumnDef { match self { Self::Id => ColumnType::Integer.def(), - Self::Name => ColumnType::string(None).def(), + Self::Name => ColumnType::String(StringLen::None).def(), } } } diff --git a/tests/common/features/event_trigger.rs b/tests/common/features/event_trigger.rs index fd8e8707..eaef6f8d 100644 --- a/tests/common/features/event_trigger.rs +++ b/tests/common/features/event_trigger.rs @@ -65,6 +65,6 @@ impl ValueType for Events { } fn column_type() -> ColumnType { - ColumnType::Array(RcOrArc::new(ColumnType::string(None))) + ColumnType::Array(RcOrArc::new(ColumnType::String(StringLen::None))) } } diff --git a/tests/common/features/schema.rs b/tests/common/features/schema.rs index d51d66c1..7a8cf814 100644 --- a/tests/common/features/schema.rs +++ b/tests/common/features/schema.rs @@ -508,7 +508,7 @@ pub async fn create_event_trigger_table(db: &DbConn) -> Result Result { .not_null() .primary_key(), ) - .col(ColumnDef::new(categories::Column::Categories).array(ColumnType::string(Some(1)))) + .col( + ColumnDef::new(categories::Column::Categories) + .array(ColumnType::String(StringLen::N(1))), + ) .to_owned(); create_table(db, &create_table_stmt, Categories).await @@ -733,7 +736,7 @@ pub async fn create_value_type_postgres_table(db: &DbConn) -> Result