Tests all DB
This commit is contained in:
parent
8858d64dd0
commit
f20c64988d
@ -34,7 +34,7 @@ pub enum ColumnType {
|
||||
JsonBinary,
|
||||
Custom(String),
|
||||
Uuid,
|
||||
Enum(String),
|
||||
Enum(String, Vec<String>),
|
||||
}
|
||||
|
||||
macro_rules! bind_oper {
|
||||
@ -245,7 +245,8 @@ impl ColumnType {
|
||||
|
||||
pub(crate) fn get_enum_name(&self) -> Option<&String> {
|
||||
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,
|
||||
}
|
||||
}
|
||||
@ -303,7 +304,7 @@ impl From<ColumnType> for sea_query::ColumnType {
|
||||
sea_query::ColumnType::Custom(sea_query::SeaRc::new(sea_query::Alias::new(&s)))
|
||||
}
|
||||
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)))
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
unpack_table_ref, ColumnTrait, DbBackend, EntityTrait, Identity, Iterable, PrimaryKeyToColumn,
|
||||
PrimaryKeyTrait, RelationTrait, Schema,
|
||||
unpack_table_ref, ColumnTrait, ColumnType, DbBackend, EntityTrait, Identity, Iterable,
|
||||
PrimaryKeyToColumn, PrimaryKeyTrait, RelationTrait, Schema,
|
||||
};
|
||||
use sea_query::{ColumnDef, ForeignKeyCreateStatement, Iden, Index, TableCreateStatement};
|
||||
|
||||
@ -21,7 +21,15 @@ where
|
||||
|
||||
for column in E::Column::iter() {
|
||||
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(),
|
||||
};
|
||||
let mut column_def = ColumnDef::new_with_type(column, types);
|
||||
|
@ -34,7 +34,10 @@ pub enum Color {
|
||||
}
|
||||
|
||||
#[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 {
|
||||
#[sea_orm(string_value = "EverydayTea")]
|
||||
EverydayTea,
|
||||
|
@ -2,8 +2,13 @@ pub use super::super::bakery_chain::*;
|
||||
|
||||
use super::*;
|
||||
use crate::common::setup::create_table;
|
||||
use sea_orm::{error::*, sea_query, DatabaseConnection, DbConn, ExecResult};
|
||||
use sea_query::{Alias, ColumnDef, ForeignKeyCreateStatement};
|
||||
use sea_orm::{
|
||||
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> {
|
||||
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> {
|
||||
let db_backend = db.get_database_backend();
|
||||
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()
|
||||
.table(active_enum::Entity)
|
||||
.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::Color).integer())
|
||||
.col(ColumnDef::new(active_enum::Column::Tea).custom(tea_enum.clone()))
|
||||
.col(&mut tea_col)
|
||||
.to_owned();
|
||||
|
||||
match db {
|
||||
#[cfg(feature = "sqlx-postgres")]
|
||||
DatabaseConnection::SqlxPostgresPoolConnection(_) => {
|
||||
use sea_orm::{ConnectionTrait, Statement};
|
||||
use sea_query::{extension::postgres::Type, PostgresQueryBuilder};
|
||||
|
||||
match db_backend {
|
||||
DbBackend::Postgres => {
|
||||
let drop_type_stmt = Type::drop()
|
||||
.name(tea_enum.clone())
|
||||
.cascade()
|
||||
@ -138,19 +147,16 @@ pub async fn create_active_enum_table(db: &DbConn) -> Result<ExecResult, DbErr>
|
||||
.as_enum(tea_enum)
|
||||
.values(vec![Alias::new("EverydayTea"), Alias::new("BreakfastTea")])
|
||||
.to_owned();
|
||||
|
||||
// FIXME: This is not working
|
||||
{
|
||||
let (sql, values) = create_type_stmt.build(PostgresQueryBuilder);
|
||||
let _stmt = Statement::from_sql_and_values(db.get_database_backend(), &sql, values);
|
||||
}
|
||||
|
||||
// But this is working...
|
||||
let stmt = Statement::from_string(
|
||||
db.get_database_backend(),
|
||||
create_type_stmt.to_string(PostgresQueryBuilder),
|
||||
);
|
||||
|
||||
db.execute(stmt).await?;
|
||||
}
|
||||
_ => {}
|
||||
|
Loading…
x
Reference in New Issue
Block a user