Skip to content

Commit

Permalink
Add default_expr support for ColumnDef (#1474)
Browse files Browse the repository at this point in the history
* Add default_expr support for ColumnDef

* added test for attribute `default_value`

* Implements `PartialEq` for `sea_query::SimpleExpr`

* RcOrArc

* Re-export `sea_query::Expr` in `sea_orm::entity::prelude`

* Add default_expr support for ColumnDef

* added test for attribute `default_value`

* Implements `PartialEq` for `sea_query::SimpleExpr`

* RcOrArc

* Re-export `sea_query::Expr` in `sea_orm::entity::prelude`

* Deprecate `ColumnDef::default_value()`

* Add `ColumnDef::default()`

---------

Co-authored-by: Billy Chan <[email protected]>
  • Loading branch information
Diwakar-Gupta and billy1624 authored Jul 13, 2023
1 parent 4953e37 commit 1ba37b6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
14 changes: 11 additions & 3 deletions sea-orm-macros/src/derives/entity_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,15 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
} else if meta.path.is_ident("default_value") {
default_value = Some(meta.value()?.parse::<Lit>()?);
} else if meta.path.is_ident("default_expr") {
default_expr = Some(meta.value()?.parse::<Lit>()?);
let lit = meta.value()?.parse()?;
if let Lit::Str(litstr) = lit {
let value_expr: TokenStream = syn::parse_str(&litstr.value())?;
default_expr = Some(value_expr);
} else {
return Err(
meta.error(format!("Invalid column_type {:?}", lit))
);
}
} else if meta.path.is_ident("column_name") {
let lit = meta.value()?.parse()?;
if let Lit::Str(litstr) = lit {
Expand Down Expand Up @@ -264,10 +272,10 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
match_row = quote! { #match_row.unique() };
}
if let Some(default_value) = default_value {
match_row = quote! { #match_row.default_value(#default_value) };
match_row = quote! { #match_row.default(#default_value) };
}
if let Some(default_expr) = default_expr {
match_row = quote! { #match_row.default_expr(#default_expr) };
match_row = quote! { #match_row.default(#default_expr) };
}
columns_trait.push(match_row);
}
Expand Down
38 changes: 34 additions & 4 deletions src/entity/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct ColumnDef {
pub(crate) null: bool,
pub(crate) unique: bool,
pub(crate) indexed: bool,
pub(crate) default_value: Option<Value>,
pub(crate) default: Option<SimpleExpr>,
}

macro_rules! bind_oper {
Expand Down Expand Up @@ -310,7 +310,7 @@ impl ColumnTypeTrait for ColumnType {
null: false,
unique: false,
indexed: false,
default_value: None,
default: None,
}
}

Expand Down Expand Up @@ -362,11 +362,21 @@ impl ColumnDef {
}

/// Set the default value
#[deprecated(since = "0.12.0", note = "Please use [`ColumnDef::default`]")]
pub fn default_value<T>(mut self, value: T) -> Self
where
T: Into<Value>,
{
self.default_value = Some(value.into());
self.default = Some(value.into().into());
self
}

/// Set the default value or expression of a column
pub fn default<T>(mut self, default: T) -> Self
where
T: Into<SimpleExpr>,
{
self.default = Some(default.into());
self
}

Expand Down Expand Up @@ -493,7 +503,7 @@ mod tests {
#[test]
#[cfg(feature = "macros")]
fn entity_model_column_1() {
use crate::entity::*;
use crate::prelude::*;

mod hello {
use crate as sea_orm;
Expand Down Expand Up @@ -521,6 +531,12 @@ mod tests {
pub eight: u32,
#[sea_orm(unique, indexed, nullable)]
pub nine: u64,
#[sea_orm(default_expr = "Expr::current_timestamp()")]
pub ten: DateTimeUtc,
#[sea_orm(default_value = 7)]
pub eleven: u8,
#[sea_orm(default_value = "twelve_value")]
pub twelve: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
Expand Down Expand Up @@ -562,6 +578,20 @@ mod tests {
hello::Column::Nine.def(),
ColumnType::BigUnsigned.def().unique().indexed().nullable()
);
assert_eq!(
hello::Column::Ten.def(),
ColumnType::TimestampWithTimeZone
.def()
.default(Expr::current_timestamp())
);
assert_eq!(
hello::Column::Eleven.def(),
ColumnType::TinyUnsigned.def().default(7)
);
assert_eq!(
hello::Column::Twelve.def(),
ColumnType::String(None).def().default("twelve_value")
);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/entity/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub use crate::{
error::*,
sea_query::{BlobSize, DynIden, RcOrArc, SeaRc},
sea_query::{BlobSize, DynIden, Expr, RcOrArc, SeaRc},
ActiveEnum, ActiveModelBehavior, ActiveModelTrait, ColumnDef, ColumnTrait, ColumnType,
ColumnTypeTrait, ConnectionTrait, CursorTrait, DatabaseConnection, DbConn, EntityName,
EntityTrait, EnumIter, ForeignKeyAction, Iden, IdenStatic, Linked, LoaderTrait, ModelTrait,
Expand Down
4 changes: 2 additions & 2 deletions src/schema/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ where
if orm_column_def.unique {
column_def.unique_key();
}
if let Some(value) = orm_column_def.default_value {
column_def.default(value);
if let Some(default) = orm_column_def.default {
column_def.default(default);
}
for primary_key in E::PrimaryKey::iter() {
if column.to_string() == primary_key.into_column().to_string() {
Expand Down

0 comments on commit 1ba37b6

Please sign in to comment.