Merge remote-tracking branch 'origin/master' into active-enum
This commit is contained in:
commit
0bbb50b7f6
@ -107,6 +107,7 @@ impl ConnectOptions {
|
||||
}
|
||||
|
||||
#[cfg(feature = "sqlx-dep")]
|
||||
/// Convert [ConnectOptions] into [sqlx::pool::PoolOptions]
|
||||
pub fn pool_options<DB>(self) -> sqlx::pool::PoolOptions<DB>
|
||||
where
|
||||
DB: sqlx::Database,
|
||||
|
@ -16,16 +16,15 @@
|
||||
/// #### Example for creating an Entity, Model and ActiveModel
|
||||
/// ```
|
||||
/// #[cfg(feature = "macros")]
|
||||
///
|
||||
/// # use sea_orm::entity::prelude::*;
|
||||
/// use sea_orm::ActiveModelBehavior;
|
||||
/// use sea_orm::RelationDef;
|
||||
/// use sea_orm::RelationTrait;
|
||||
/// use sea_orm::ColumnType;
|
||||
/// use sea_orm::ColumnDef;
|
||||
/// use sea_orm::ColumnTrait;
|
||||
/// use sea_orm::PrimaryKeyTrait;
|
||||
/// use sea_orm::ColumnType;
|
||||
/// use sea_orm::EntityName;
|
||||
/// use sea_orm::PrimaryKeyTrait;
|
||||
/// use sea_orm::RelationDef;
|
||||
/// use sea_orm::RelationTrait;
|
||||
///
|
||||
/// // Use [DeriveEntity] to derive the EntityTrait automatically
|
||||
/// #[derive(Copy, Clone, Default, Debug, DeriveEntity)]
|
||||
@ -72,7 +71,6 @@
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
///
|
||||
/// #[derive(Copy, Clone, Debug, EnumIter)]
|
||||
/// pub enum Relation {}
|
||||
///
|
||||
@ -96,7 +94,6 @@
|
||||
/// // Implement user defined operations for CREATE, UPDATE and DELETE operations
|
||||
/// // to create an ActiveModel using the [ActiveModelBehavior]
|
||||
/// impl ActiveModelBehavior for ActiveModel {}
|
||||
///
|
||||
/// ```
|
||||
mod active_enum;
|
||||
mod active_model;
|
||||
|
@ -539,5 +539,7 @@ macro_rules! try_from_u64_string {
|
||||
|
||||
try_from_u64_string!(String);
|
||||
|
||||
try_from_u64_err!(Vec<u8>);
|
||||
|
||||
#[cfg(feature = "with-uuid")]
|
||||
try_from_u64_err!(uuid::Uuid);
|
||||
|
@ -1,4 +1,5 @@
|
||||
#![cfg_attr(docsrs, feature(doc_cfg))]
|
||||
#![warn(missing_docs)]
|
||||
#![deny(
|
||||
missing_debug_implementations,
|
||||
clippy::print_stderr,
|
||||
|
74
tests/byte_primary_key_tests.rs
Normal file
74
tests/byte_primary_key_tests.rs
Normal file
@ -0,0 +1,74 @@
|
||||
pub mod common;
|
||||
|
||||
pub use common::{features::*, setup::*, TestContext};
|
||||
use pretty_assertions::assert_eq;
|
||||
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection};
|
||||
|
||||
#[sea_orm_macros::test]
|
||||
#[cfg(any(
|
||||
feature = "sqlx-mysql",
|
||||
feature = "sqlx-sqlite",
|
||||
feature = "sqlx-postgres"
|
||||
))]
|
||||
async fn main() -> Result<(), DbErr> {
|
||||
let ctx = TestContext::new("byte_primary_key_tests").await;
|
||||
create_tables(&ctx.db).await?;
|
||||
create_and_update(&ctx.db).await?;
|
||||
ctx.delete().await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn create_and_update(db: &DatabaseConnection) -> Result<(), DbErr> {
|
||||
use common::features::byte_primary_key::*;
|
||||
|
||||
let model = Model {
|
||||
id: vec![1, 2, 3],
|
||||
value: "First Row".to_owned(),
|
||||
};
|
||||
|
||||
let res = Entity::insert(model.clone().into_active_model())
|
||||
.exec(db)
|
||||
.await?;
|
||||
|
||||
assert_eq!(Entity::find().one(db).await?, Some(model.clone()));
|
||||
|
||||
assert_eq!(res.last_insert_id, model.id);
|
||||
|
||||
let updated_active_model = ActiveModel {
|
||||
value: Set("First Row (Updated)".to_owned()),
|
||||
..model.clone().into_active_model()
|
||||
};
|
||||
|
||||
let update_res = Entity::update(updated_active_model.clone())
|
||||
.filter(Column::Id.eq(vec![1, 2, 4]))
|
||||
.exec(db)
|
||||
.await;
|
||||
|
||||
assert_eq!(
|
||||
update_res,
|
||||
Err(DbErr::RecordNotFound(
|
||||
"None of the database rows are affected".to_owned()
|
||||
))
|
||||
);
|
||||
|
||||
let update_res = Entity::update(updated_active_model.clone())
|
||||
.filter(Column::Id.eq(vec![1, 2, 3]))
|
||||
.exec(db)
|
||||
.await?;
|
||||
|
||||
assert_eq!(update_res, updated_active_model);
|
||||
|
||||
assert_eq!(
|
||||
Entity::find()
|
||||
.filter(Column::Id.eq(vec![1, 2, 3]))
|
||||
.one(db)
|
||||
.await?,
|
||||
Some(Model {
|
||||
id: vec![1, 2, 3],
|
||||
value: "First Row (Updated)".to_owned(),
|
||||
})
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
14
tests/common/features/byte_primary_key.rs
Normal file
14
tests/common/features/byte_primary_key.rs
Normal file
@ -0,0 +1,14 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
|
||||
#[sea_orm(table_name = "byte_primary_key")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
pub id: Vec<u8>,
|
||||
pub value: String,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
@ -1,5 +1,6 @@
|
||||
pub mod active_enum;
|
||||
pub mod applog;
|
||||
pub mod byte_primary_key;
|
||||
pub mod metadata;
|
||||
pub mod repository;
|
||||
pub mod schema;
|
||||
@ -7,6 +8,7 @@ pub mod self_join;
|
||||
|
||||
pub use active_enum::Entity as ActiveEnum;
|
||||
pub use applog::Entity as Applog;
|
||||
pub use byte_primary_key::Entity as BytePrimaryKey;
|
||||
pub use metadata::Entity as Metadata;
|
||||
pub use repository::Entity as Repository;
|
||||
pub use schema::*;
|
||||
|
@ -1,7 +1,7 @@
|
||||
pub use super::super::bakery_chain::*;
|
||||
|
||||
use super::*;
|
||||
use crate::common::setup::{create_table, create_enum};
|
||||
use crate::common::setup::{create_table, create_enum, create_table_without_asserts};
|
||||
use sea_orm::{
|
||||
error::*, sea_query, ConnectionTrait, DatabaseConnection, DbBackend, DbConn, ExecResult,
|
||||
};
|
||||
@ -12,6 +12,7 @@ pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {
|
||||
create_metadata_table(db).await?;
|
||||
create_repository_table(db).await?;
|
||||
create_self_join_table(db).await?;
|
||||
create_byte_primary_key_table(db).await?;
|
||||
create_active_enum_table(db).await?;
|
||||
|
||||
Ok(())
|
||||
@ -104,6 +105,26 @@ pub async fn create_self_join_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
||||
create_table(db, &stmt, SelfJoin).await
|
||||
}
|
||||
|
||||
pub async fn create_byte_primary_key_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
||||
let mut primary_key_col = ColumnDef::new(byte_primary_key::Column::Id);
|
||||
match db.get_database_backend() {
|
||||
DbBackend::MySql => primary_key_col.binary_len(3),
|
||||
DbBackend::Sqlite | DbBackend::Postgres => primary_key_col.binary(),
|
||||
};
|
||||
|
||||
let stmt = sea_query::Table::create()
|
||||
.table(byte_primary_key::Entity)
|
||||
.col(primary_key_col.not_null().primary_key())
|
||||
.col(
|
||||
ColumnDef::new(byte_primary_key::Column::Value)
|
||||
.string()
|
||||
.not_null(),
|
||||
)
|
||||
.to_owned();
|
||||
|
||||
create_table_without_asserts(db, &stmt).await
|
||||
}
|
||||
|
||||
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");
|
||||
|
@ -131,6 +131,19 @@ pub async fn create_table<E>(
|
||||
where
|
||||
E: EntityTrait,
|
||||
{
|
||||
let builder = db.get_database_backend();
|
||||
assert_eq!(
|
||||
builder.build(&Schema::create_table_from_entity(entity)),
|
||||
builder.build(create)
|
||||
);
|
||||
|
||||
create_table_without_asserts(db, create).await
|
||||
}
|
||||
|
||||
pub async fn create_table_without_asserts(
|
||||
db: &DbConn,
|
||||
create: &TableCreateStatement,
|
||||
) -> Result<ExecResult, DbErr> {
|
||||
let builder = db.get_database_backend();
|
||||
if builder != DbBackend::Sqlite {
|
||||
let stmt = builder.build(
|
||||
@ -141,14 +154,5 @@ where
|
||||
);
|
||||
db.execute(stmt).await?;
|
||||
}
|
||||
|
||||
let stmt = builder.build(create);
|
||||
assert_eq!(
|
||||
builder.build(&Schema::create_table_from_entity(
|
||||
entity,
|
||||
db.get_database_backend()
|
||||
)),
|
||||
stmt
|
||||
);
|
||||
db.execute(stmt).await
|
||||
db.execute(builder.build(create)).await
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user