Skip to content

Commit

Permalink
Merge pull request #1403 from /issues/1357
Browse files Browse the repository at this point in the history
Don't call last_insert_id if not needed
  • Loading branch information
tyt2y3 committed Jan 19, 2023
2 parents 475bd63 + 5cdcdbf commit 0557e7b
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 28 deletions.
18 changes: 18 additions & 0 deletions issues/1357/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[workspace]
# A separate workspace

[package]
name = "sea-orm-issues-1357"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
anyhow = "1"
serde = "1"
tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros"] }

[dependencies.sea-orm]
path = "../../"
default-features = false
features = ["macros", "runtime-tokio-native-tls", "sqlx-sqlite"]
14 changes: 14 additions & 0 deletions issues/1357/src/entity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "pool")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: i64,
pub name: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}
42 changes: 42 additions & 0 deletions issues/1357/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use anyhow::Result;
use sea_orm::{ConnectionTrait, Database, EntityTrait, IntoActiveModel, Schema};

mod entity;

use entity::*;

#[tokio::main]
async fn main() -> Result<()> {
let db = Database::connect("sqlite::memory:").await.unwrap();

let builder = db.get_database_backend();
let schema = Schema::new(builder);
let stmt = schema.create_table_from_entity(Entity);
db.execute(builder.build(&stmt)).await?;

let model = Model {
id: 100,
name: "Hello".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 model = Model {
id: -10,
name: "World".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);

Ok(())
}
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ pub enum DbErr {
/// None of the records are being inserted into the database,
/// if you insert with upsert expression that means
/// all of them conflict with existing records in the database
#[error("RecordNotInserted Error: {0}")]
RecordNotInserted(String),
#[error("None of the records are being inserted")]
RecordNotInserted,
}

/// Runtime error
Expand Down
2 changes: 1 addition & 1 deletion src/executor/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl ExecResult {
}
}

/// Get the number of rows affedted by the operation
/// Get the number of rows affected by the operation
pub fn rows_affected(&self) -> u64 {
match &self.result {
#[cfg(feature = "sqlx-mysql")]
Expand Down
42 changes: 25 additions & 17 deletions src/executor/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,29 +137,37 @@ where
{
type PrimaryKey<A> = <<A as ActiveModelTrait>::Entity as EntityTrait>::PrimaryKey;
type ValueTypeOf<A> = <PrimaryKey<A> as PrimaryKeyTrait>::ValueType;
let last_insert_id_opt = match db.support_returning() {
true => {

let last_insert_id = match (primary_key, db.support_returning()) {
(Some(value_tuple), _) => {
let res = db.execute(statement).await?;
if res.rows_affected() == 0 {
return Err(DbErr::RecordNotInserted);
}
FromValueTuple::from_value_tuple(value_tuple)
}
(None, true) => {
let mut rows = db.query_all(statement).await?;
let row = match rows.pop() {
Some(row) => row,
None => return Err(DbErr::RecordNotInserted),
};
let cols = PrimaryKey::<A>::iter()
.map(|col| col.to_string())
.collect::<Vec<_>>();
let rows = db.query_all(statement).await?;
let res = rows.last().ok_or_else(|| {
DbErr::RecordNotInserted("None of the records are being inserted".to_owned())
})?;
res.try_get_many("", cols.as_ref()).ok()
row.try_get_many("", cols.as_ref())
.map_err(|_| DbErr::UnpackInsertId)?
}
false => {
let last_insert_id = db.execute(statement).await?.last_insert_id();
ValueTypeOf::<A>::try_from_u64(last_insert_id).ok()
(None, false) => {
let res = db.execute(statement).await?;
if res.rows_affected() == 0 {
return Err(DbErr::RecordNotInserted);
}
let last_insert_id = res.last_insert_id();
ValueTypeOf::<A>::try_from_u64(last_insert_id).map_err(|_| DbErr::UnpackInsertId)?
}
};
let last_insert_id = match primary_key {
Some(value_tuple) => FromValueTuple::from_value_tuple(value_tuple),
None => match last_insert_id_opt {
Some(last_insert_id) => last_insert_id,
None => return Err(DbErr::UnpackInsertId),
},
};

Ok(InsertResult { last_insert_id })
}

Expand Down
15 changes: 13 additions & 2 deletions tests/string_primary_key_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod common;

pub use common::{features::*, setup::*, TestContext};
use pretty_assertions::assert_eq;
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection};
use sea_orm::{entity::prelude::*, entity::*, sea_query::OnConflict, DatabaseConnection};
use serde_json::json;

#[sea_orm_macros::test]
Expand Down Expand Up @@ -30,7 +30,7 @@ pub async fn insert_and_delete_repository(db: &DatabaseConnection) -> Result<(),
}
.into_active_model();

let result = repository.insert(db).await?;
let result = repository.clone().insert(db).await?;

assert_eq!(
result,
Expand All @@ -42,6 +42,17 @@ pub async fn insert_and_delete_repository(db: &DatabaseConnection) -> Result<(),
}
);

#[cfg(any(feature = "sqlx-sqlite", feature = "sqlx-postgres"))]
{
let err = Repository::insert(repository)
// MySQL does not support DO NOTHING, we might workaround that later
.on_conflict(OnConflict::new().do_nothing().to_owned())
.exec(db)
.await;

assert_eq!(err.err(), Some(DbErr::RecordNotInserted));
}

result.delete(db).await?;

assert_eq!(
Expand Down
7 changes: 1 addition & 6 deletions tests/upsert_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,7 @@ pub async fn create_insert_default(db: &DatabaseConnection) -> Result<(), DbErr>
.exec(db)
.await;

assert_eq!(
res.err(),
Some(DbErr::RecordNotInserted(
"None of the records are being inserted".to_owned()
))
);
assert_eq!(res.err(), Some(DbErr::RecordNotInserted));

Ok(())
}

0 comments on commit 0557e7b

Please sign in to comment.