Skip to content

Commit

Permalink
support extra in entity macro field
Browse files Browse the repository at this point in the history
  • Loading branch information
Liber Wang committed Jan 30, 2023
1 parent d6afa09 commit fa4b65d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
6 changes: 6 additions & 0 deletions sea-orm-macros/src/derives/entity_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
let mut default_expr = None;
let mut select_as = None;
let mut save_as = None;
let mut extra = None;
let mut created_at = false;
let mut updated_at = false;
let mut indexed = false;
Expand Down Expand Up @@ -193,6 +194,8 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
format!("Invalid save_as {:?}", nv.lit),
));
}
} else if name == "extra" {
extra = Some(nv.lit.to_owned());
}
}
}
Expand Down Expand Up @@ -355,6 +358,9 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
if let Some(default_expr) = default_expr {
match_row = quote! { #match_row.default_expr(#default_expr) };
}
if let Some(extra) = extra {
match_row = quote! { #match_row.extra(#extra.into()) };
}
columns_trait.push(match_row);
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/entity/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct ColumnDef {
pub(crate) created_at: bool,
pub(crate) updated_at: bool,
pub(crate) default_value: Option<Value>,
pub(crate) extra: Option<String>,
}

macro_rules! bind_oper {
Expand Down Expand Up @@ -300,6 +301,7 @@ impl ColumnTypeTrait for ColumnType {
created_at: false,
updated_at: false,
default_value: None,
extra: None,
}
}

Expand Down Expand Up @@ -360,6 +362,12 @@ impl ColumnDef {
self
}

/// Set the extra
pub fn extra(mut self, value: String) -> Self {
self.extra = Some(value);
self
}

/// Get [ColumnType] as reference
pub fn get_column_type(&self) -> &ColumnType {
&self.col_type
Expand Down
3 changes: 3 additions & 0 deletions src/schema/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ where
if let Some(value) = orm_column_def.default_value {
column_def.default(value);
}
if let Some(value) = orm_column_def.extra {
column_def.default(value);
}
for primary_key in E::PrimaryKey::iter() {
if column.to_string() == primary_key.into_column().to_string() {
if E::PrimaryKey::auto_increment() {
Expand Down
4 changes: 2 additions & 2 deletions tests/common/features/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ pub struct Model {
pub id: i32,
pub pay: String,
pub amount: f64,
#[sea_orm(updated_at, nullable)]
#[sea_orm(updated_at, nullable, extra = "DEFAULT CURRENT_TIMESTAMP")]
pub updated_at: DateTimeWithTimeZone,
#[sea_orm(created_at, nullable)]
#[sea_orm(created_at, nullable, extra = "DEFAULT CURRENT_TIMESTAMP")]
pub created_at: DateTimeWithTimeZone,
}

Expand Down

0 comments on commit fa4b65d

Please sign in to comment.