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

Refactor Schema #309

Merged
merged 1 commit into from
Nov 13, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 11 additions & 9 deletions src/schema/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ use sea_query::{

impl Schema {
/// Creates Postgres enums from an Entity. See [TypeCreateStatement] for more details
pub fn create_enum_from_entity<E>(entity: E, db_backend: DbBackend) -> Vec<TypeCreateStatement>
pub fn create_enum_from_entity<E>(&self, entity: E) -> Vec<TypeCreateStatement>
where
E: EntityTrait,
{
create_enum_from_entity(entity, db_backend)
create_enum_from_entity(entity, self.backend)
}

/// Creates a table from an Entity. See [TableCreateStatement] for more details
pub fn create_table_from_entity<E>(entity: E, db_backend: DbBackend) -> TableCreateStatement
pub fn create_table_from_entity<E>(&self, entity: E) -> TableCreateStatement
where
E: EntityTrait,
{
create_table_from_entity(entity, db_backend)
create_table_from_entity(entity, self.backend)
}
}

pub(crate) fn create_enum_from_entity<E>(_: E, db_backend: DbBackend) -> Vec<TypeCreateStatement>
pub(crate) fn create_enum_from_entity<E>(_: E, backend: DbBackend) -> Vec<TypeCreateStatement>
where
E: EntityTrait,
{
if matches!(db_backend, DbBackend::MySql | DbBackend::Sqlite) {
if matches!(backend, DbBackend::MySql | DbBackend::Sqlite) {
return Vec::new();
}
let mut vec = Vec::new();
Expand All @@ -52,7 +52,7 @@ where
vec
}

pub(crate) fn create_table_from_entity<E>(entity: E, db_backend: DbBackend) -> TableCreateStatement
pub(crate) fn create_table_from_entity<E>(entity: E, backend: DbBackend) -> TableCreateStatement
where
E: EntityTrait,
{
Expand All @@ -61,7 +61,7 @@ where
for column in E::Column::iter() {
let orm_column_def = column.def();
let types = match orm_column_def.col_type {
ColumnType::Enum(s, variants) => match db_backend {
ColumnType::Enum(s, variants) => match backend {
DbBackend::MySql => {
ColumnType::Custom(format!("ENUM('{}')", variants.join("', '")))
}
Expand Down Expand Up @@ -175,8 +175,10 @@ mod tests {

#[test]
fn test_create_table_from_entity() {
let schema = Schema::new(DbBackend::MySql);
assert_eq!(
Schema::create_table_from_entity(CakeFillingPrice, DbBackend::MySql)
schema
.create_table_from_entity(CakeFillingPrice)
.to_string(MysqlQueryBuilder),
Table::create()
.table(CakeFillingPrice)
Expand Down
16 changes: 14 additions & 2 deletions src/schema/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
use crate::DbBackend;

mod entity;

/// This structure defines a schema for a table
/// This is a helper struct to convert [`EntityTrait`](crate::EntityTrait)
/// into different [`sea_query`](crate::sea_query) statements.
#[derive(Debug)]
pub struct Schema;
pub struct Schema {
backend: DbBackend,
}

impl Schema {
/// Create a helper for a specific database backend
pub fn new(backend: DbBackend) -> Self {
Self { backend }
}
}
7 changes: 5 additions & 2 deletions tests/common/setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ where
}

let expect_stmts: Vec<Statement> = creates.iter().map(|stmt| builder.build(stmt)).collect();
let create_from_entity_stmts: Vec<Statement> = Schema::create_enum_from_entity(entity, builder)
let schema = Schema::new(builder);
let create_from_entity_stmts: Vec<Statement> = schema
.create_enum_from_entity(entity)
.iter()
.map(|stmt| builder.build(stmt))
.collect();
Expand All @@ -131,8 +133,9 @@ where
E: EntityTrait,
{
let builder = db.get_database_backend();
let schema = Schema::new(builder);
assert_eq!(
builder.build(&Schema::create_table_from_entity(entity, builder)),
builder.build(&schema.create_table_from_entity(entity)),
builder.build(create)
);

Expand Down