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 Aug 20, 2022
1 parent d9d77dc commit 37de813
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 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 @@ -90,6 +90,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
let mut nullable = false;
let mut default_value = None;
let mut default_expr = None;
let mut extra = None;
let mut created_at = false;
let mut updated_at = false;
let mut indexed = false;
Expand Down Expand Up @@ -171,6 +172,8 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
format!("Invalid enum_name {:?}", nv.lit),
));
}
} else if name == "extra" {
extra = Some(nv.lit.to_owned());
}
}
}
Expand Down Expand Up @@ -312,6 +315,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 @@ -12,6 +12,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>,
}

/// The type of column as defined in the SQL format
Expand Down Expand Up @@ -314,6 +315,7 @@ impl ColumnType {
created_at: false,
updated_at: false,
default_value: None,
extra: None,
}
}

Expand Down Expand Up @@ -370,6 +372,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 @@ -143,6 +143,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.extra(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
12 changes: 10 additions & 2 deletions tests/common/features/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,16 @@ pub async fn create_check_table(db: &DbConn) -> Result<ExecResult, DbErr> {
)
.col(ColumnDef::new(check::Column::Pay).string().not_null())
.col(ColumnDef::new(check::Column::Amount).double().not_null())
.col(ColumnDef::new(check::Column::UpdatedAt).timestamp_with_time_zone())
.col(ColumnDef::new(check::Column::CreatedAt).timestamp_with_time_zone())
.col(
ColumnDef::new(check::Column::UpdatedAt)
.timestamp_with_time_zone()
.extra("DEFAULT CURRENT_TIMESTAMP".into()),
)
.col(
ColumnDef::new(check::Column::CreatedAt)
.timestamp_with_time_zone()
.extra("DEFAULT CURRENT_TIMESTAMP".into()),
)
.to_owned();

create_table(db, &stmt, Check).await
Expand Down

0 comments on commit 37de813

Please sign in to comment.