Tests all DB
This commit is contained in:
parent
8858d64dd0
commit
f20c64988d
@ -34,7 +34,7 @@ pub enum ColumnType {
|
|||||||
JsonBinary,
|
JsonBinary,
|
||||||
Custom(String),
|
Custom(String),
|
||||||
Uuid,
|
Uuid,
|
||||||
Enum(String),
|
Enum(String, Vec<String>),
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! bind_oper {
|
macro_rules! bind_oper {
|
||||||
@ -245,7 +245,8 @@ impl ColumnType {
|
|||||||
|
|
||||||
pub(crate) fn get_enum_name(&self) -> Option<&String> {
|
pub(crate) fn get_enum_name(&self) -> Option<&String> {
|
||||||
match self {
|
match self {
|
||||||
ColumnType::Enum(s) => Some(s),
|
// FIXME: How to get rid of this feature gate?
|
||||||
|
ColumnType::Enum(s, _) if cfg!(feature = "sqlx-postgres") => Some(s),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -303,7 +304,7 @@ impl From<ColumnType> for sea_query::ColumnType {
|
|||||||
sea_query::ColumnType::Custom(sea_query::SeaRc::new(sea_query::Alias::new(&s)))
|
sea_query::ColumnType::Custom(sea_query::SeaRc::new(sea_query::Alias::new(&s)))
|
||||||
}
|
}
|
||||||
ColumnType::Uuid => sea_query::ColumnType::Uuid,
|
ColumnType::Uuid => sea_query::ColumnType::Uuid,
|
||||||
ColumnType::Enum(s) => {
|
ColumnType::Enum(s, _) => {
|
||||||
sea_query::ColumnType::Custom(sea_query::SeaRc::new(sea_query::Alias::new(&s)))
|
sea_query::ColumnType::Custom(sea_query::SeaRc::new(sea_query::Alias::new(&s)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
unpack_table_ref, ColumnTrait, DbBackend, EntityTrait, Identity, Iterable, PrimaryKeyToColumn,
|
unpack_table_ref, ColumnTrait, ColumnType, DbBackend, EntityTrait, Identity, Iterable,
|
||||||
PrimaryKeyTrait, RelationTrait, Schema,
|
PrimaryKeyToColumn, PrimaryKeyTrait, RelationTrait, Schema,
|
||||||
};
|
};
|
||||||
use sea_query::{ColumnDef, ForeignKeyCreateStatement, Iden, Index, TableCreateStatement};
|
use sea_query::{ColumnDef, ForeignKeyCreateStatement, Iden, Index, TableCreateStatement};
|
||||||
|
|
||||||
@ -21,7 +21,15 @@ where
|
|||||||
|
|
||||||
for column in E::Column::iter() {
|
for column in E::Column::iter() {
|
||||||
let orm_column_def = column.def();
|
let orm_column_def = column.def();
|
||||||
let types = match db_backend {
|
let types = match orm_column_def.col_type {
|
||||||
|
ColumnType::Enum(s, variants) => match db_backend {
|
||||||
|
DbBackend::MySql => {
|
||||||
|
ColumnType::Custom(format!("ENUM('{}')", variants.join("', '")))
|
||||||
|
}
|
||||||
|
DbBackend::Postgres => ColumnType::Custom(s),
|
||||||
|
DbBackend::Sqlite => ColumnType::Text,
|
||||||
|
}
|
||||||
|
.into(),
|
||||||
_ => orm_column_def.col_type.into(),
|
_ => orm_column_def.col_type.into(),
|
||||||
};
|
};
|
||||||
let mut column_def = ColumnDef::new_with_type(column, types);
|
let mut column_def = ColumnDef::new_with_type(column, types);
|
||||||
|
@ -34,7 +34,10 @@ pub enum Color {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, EnumIter, DeriveActiveEnum)]
|
#[derive(Debug, Clone, PartialEq, EnumIter, DeriveActiveEnum)]
|
||||||
#[sea_orm(rs_type = "String", db_type = r#"Enum("tea".to_owned())"#)]
|
#[sea_orm(
|
||||||
|
rs_type = "String",
|
||||||
|
db_type = r#"Enum("tea".to_owned(), vec!["EverydayTea".to_owned(), "BreakfastTea".to_owned()])"#
|
||||||
|
)]
|
||||||
pub enum Tea {
|
pub enum Tea {
|
||||||
#[sea_orm(string_value = "EverydayTea")]
|
#[sea_orm(string_value = "EverydayTea")]
|
||||||
EverydayTea,
|
EverydayTea,
|
||||||
|
@ -2,8 +2,13 @@ pub use super::super::bakery_chain::*;
|
|||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::common::setup::create_table;
|
use crate::common::setup::create_table;
|
||||||
use sea_orm::{error::*, sea_query, DatabaseConnection, DbConn, ExecResult};
|
use sea_orm::{
|
||||||
use sea_query::{Alias, ColumnDef, ForeignKeyCreateStatement};
|
error::*, sea_query, ConnectionTrait, DatabaseConnection, DbBackend, DbConn, ExecResult,
|
||||||
|
Statement,
|
||||||
|
};
|
||||||
|
use sea_query::{
|
||||||
|
extension::postgres::Type, Alias, ColumnDef, ForeignKeyCreateStatement, PostgresQueryBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {
|
pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {
|
||||||
create_log_table(db).await?;
|
create_log_table(db).await?;
|
||||||
@ -103,8 +108,16 @@ pub async fn create_self_join_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create_active_enum_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
pub async fn create_active_enum_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
||||||
|
let db_backend = db.get_database_backend();
|
||||||
let tea_enum = Alias::new("tea");
|
let tea_enum = Alias::new("tea");
|
||||||
|
|
||||||
|
let mut tea_col = ColumnDef::new(active_enum::Column::Tea);
|
||||||
|
match db_backend {
|
||||||
|
DbBackend::MySql => tea_col.custom(Alias::new("ENUM('EverydayTea', 'BreakfastTea')")),
|
||||||
|
DbBackend::Postgres => tea_col.custom(tea_enum.clone()),
|
||||||
|
DbBackend::Sqlite => tea_col.text(),
|
||||||
|
};
|
||||||
|
|
||||||
let stmt = sea_query::Table::create()
|
let stmt = sea_query::Table::create()
|
||||||
.table(active_enum::Entity)
|
.table(active_enum::Entity)
|
||||||
.col(
|
.col(
|
||||||
@ -116,15 +129,11 @@ pub async fn create_active_enum_table(db: &DbConn) -> Result<ExecResult, DbErr>
|
|||||||
)
|
)
|
||||||
.col(ColumnDef::new(active_enum::Column::Category).string_len(1))
|
.col(ColumnDef::new(active_enum::Column::Category).string_len(1))
|
||||||
.col(ColumnDef::new(active_enum::Column::Color).integer())
|
.col(ColumnDef::new(active_enum::Column::Color).integer())
|
||||||
.col(ColumnDef::new(active_enum::Column::Tea).custom(tea_enum.clone()))
|
.col(&mut tea_col)
|
||||||
.to_owned();
|
.to_owned();
|
||||||
|
|
||||||
match db {
|
match db_backend {
|
||||||
#[cfg(feature = "sqlx-postgres")]
|
DbBackend::Postgres => {
|
||||||
DatabaseConnection::SqlxPostgresPoolConnection(_) => {
|
|
||||||
use sea_orm::{ConnectionTrait, Statement};
|
|
||||||
use sea_query::{extension::postgres::Type, PostgresQueryBuilder};
|
|
||||||
|
|
||||||
let drop_type_stmt = Type::drop()
|
let drop_type_stmt = Type::drop()
|
||||||
.name(tea_enum.clone())
|
.name(tea_enum.clone())
|
||||||
.cascade()
|
.cascade()
|
||||||
@ -138,19 +147,16 @@ pub async fn create_active_enum_table(db: &DbConn) -> Result<ExecResult, DbErr>
|
|||||||
.as_enum(tea_enum)
|
.as_enum(tea_enum)
|
||||||
.values(vec![Alias::new("EverydayTea"), Alias::new("BreakfastTea")])
|
.values(vec![Alias::new("EverydayTea"), Alias::new("BreakfastTea")])
|
||||||
.to_owned();
|
.to_owned();
|
||||||
|
|
||||||
// FIXME: This is not working
|
// FIXME: This is not working
|
||||||
{
|
{
|
||||||
let (sql, values) = create_type_stmt.build(PostgresQueryBuilder);
|
let (sql, values) = create_type_stmt.build(PostgresQueryBuilder);
|
||||||
let _stmt = Statement::from_sql_and_values(db.get_database_backend(), &sql, values);
|
let _stmt = Statement::from_sql_and_values(db.get_database_backend(), &sql, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
// But this is working...
|
// But this is working...
|
||||||
let stmt = Statement::from_string(
|
let stmt = Statement::from_string(
|
||||||
db.get_database_backend(),
|
db.get_database_backend(),
|
||||||
create_type_stmt.to_string(PostgresQueryBuilder),
|
create_type_stmt.to_string(PostgresQueryBuilder),
|
||||||
);
|
);
|
||||||
|
|
||||||
db.execute(stmt).await?;
|
db.execute(stmt).await?;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user