Skip to content

Commit

Permalink
Explicit StringLen on ColumnType::String (#2123)
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 authored Mar 5, 2024
1 parent 9318a54 commit b775027
Show file tree
Hide file tree
Showing 26 changed files with 67 additions and 50 deletions.
2 changes: 1 addition & 1 deletion examples/basic/src/example_filling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/src/example_fruit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion issues/1143/src/entity/sea_orm_active_enums.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion issues/1599/entity/src/filling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
Expand Down
4 changes: 2 additions & 2 deletions sea-orm-codegen/src/entity/base_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions sea-orm-codegen/src/entity/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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")))
Expand Down Expand Up @@ -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()",
Expand Down Expand Up @@ -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()
);
Expand Down
8 changes: 4 additions & 4 deletions sea-orm-codegen/src/entity/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entity> {
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-codegen/tests/expanded/filling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-codegen/tests/expanded/fruit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-codegen/tests/expanded/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-codegen/tests/expanded_with_schema_name/filling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-codegen/tests/expanded_with_schema_name/fruit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-codegen/tests/expanded_with_schema_name/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
Expand Down
8 changes: 4 additions & 4 deletions sea-orm-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
/// # }
/// # }
/// # }
Expand Down Expand Up @@ -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(),
/// # }
/// # }
/// # }
Expand Down Expand Up @@ -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(),
/// # }
/// # }
/// # }
Expand Down Expand Up @@ -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(),
/// # }
/// # }
/// # }
Expand Down
28 changes: 19 additions & 9 deletions src/entity/active_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
/// }
/// }
/// ```
Expand All @@ -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,
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 6 additions & 2 deletions src/entity/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
/// }
/// }
/// }
Expand Down
2 changes: 1 addition & 1 deletion src/entity/primary_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/tests_cfg/cake_expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/tests_cfg/filling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/tests_cfg/lunch_set_expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/common/features/dyn_table_name_lazy_static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/common/features/event_trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
}
Loading

0 comments on commit b775027

Please sign in to comment.