Move entity_to_table_create_statement

This commit is contained in:
Chris Tsang 2021-09-07 15:55:58 +08:00
parent b100e32ffd
commit 6c810673c4
3 changed files with 20 additions and 8 deletions

View File

@ -1,10 +1,19 @@
use crate::{ use crate::{
unpack_table_ref, ColumnTrait, EntityTrait, Identity, Iterable, PrimaryKeyToColumn, unpack_table_ref, ColumnTrait, EntityTrait, Identity, Iterable, PrimaryKeyToColumn,
PrimaryKeyTrait, RelationTrait, PrimaryKeyTrait, RelationTrait, Schema,
}; };
use sea_query::{ColumnDef, ForeignKeyCreateStatement, Iden, Index, TableCreateStatement}; use sea_query::{ColumnDef, ForeignKeyCreateStatement, Iden, Index, TableCreateStatement};
pub fn create_table_from_entity<E>(entity: E) -> TableCreateStatement impl Schema {
pub fn create_table_from_entity<E>(entity: E) -> TableCreateStatement
where
E: EntityTrait,
{
create_table_from_entity(entity)
}
}
pub(crate) fn create_table_from_entity<E>(entity: E) -> TableCreateStatement
where where
E: EntityTrait, E: EntityTrait,
{ {
@ -112,14 +121,13 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{create_table_from_entity, tests_cfg::*}; use crate::{sea_query::*, tests_cfg::*, Schema};
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
use sea_query::*;
#[test] #[test]
fn test_create_table_from_entity() { fn test_create_table_from_entity() {
assert_eq!( assert_eq!(
create_table_from_entity(CakeFillingPrice).to_string(MysqlQueryBuilder), Schema::create_table_from_entity(CakeFillingPrice).to_string(MysqlQueryBuilder),
Table::create() Table::create()
.table(CakeFillingPrice) .table(CakeFillingPrice)
.if_not_exists() .if_not_exists()

View File

@ -1,3 +1,4 @@
mod entity; mod entity;
pub use entity::*; #[derive(Debug)]
pub struct Schema;

View File

@ -1,6 +1,6 @@
pub use super::super::bakery_chain::*; pub use super::super::bakery_chain::*;
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
use sea_orm::{create_table_from_entity, error::*, sea_query, DbConn, EntityTrait, ExecResult}; use sea_orm::{error::*, sea_query, DbConn, EntityTrait, ExecResult, Schema};
use sea_query::{ColumnDef, ForeignKey, ForeignKeyAction, Index, Table, TableCreateStatement}; use sea_query::{ColumnDef, ForeignKey, ForeignKeyAction, Index, Table, TableCreateStatement};
async fn create_table<E>( async fn create_table<E>(
@ -13,7 +13,10 @@ where
{ {
let builder = db.get_database_backend(); let builder = db.get_database_backend();
let stmt = builder.build(stmt); let stmt = builder.build(stmt);
assert_eq!(builder.build(&create_table_from_entity(entity)), stmt); assert_eq!(
builder.build(&Schema::create_table_from_entity(entity)),
stmt
);
db.execute(stmt).await db.execute(stmt).await
} }