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

Add default_expr support for ColumnDef #1474

Merged
merged 17 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from 16 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
16 changes: 13 additions & 3 deletions sea-orm-macros/src/derives/entity_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,17 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
} else if name == "default_value" {
default_value = Some(nv.lit.to_owned());
} else if name == "default_expr" {
default_expr = Some(nv.lit.to_owned());
if let Lit::Str(litstr) = &nv.lit {
let value_expr: TokenStream =
syn::parse_str(&litstr.value())?;

default_expr = Some(value_expr);
} else {
return Err(Error::new(
field.span(),
"`default_expr` should be str",
));
}
} else if name == "column_name" {
if let Lit::Str(litstr) = &nv.lit {
column_name = Some(litstr.value());
Expand Down Expand Up @@ -339,10 +349,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 @@ -485,7 +495,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 @@ -513,6 +523,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 @@ -554,6 +570,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