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

Make insert statement not panic when inserting nothing #1708

Merged
merged 7 commits into from
Jun 20, 2023

Conversation

darkmmon
Copy link
Contributor

PR Info

New Features

  • A new wrapper struct for insert statement, allowing safe return when attempt to insert nothing

Bug Fixes

Changes

@darkmmon darkmmon marked this pull request as ready for review June 19, 2023 04:39
Copy link
Member

@tyt2y3 tyt2y3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a naming thing, may be we can rename InsertAttempt ->TryInsert, and InsertAttemptResultType -> TryInsertResult?

@@ -173,6 +222,7 @@ where
/// };
/// assert_eq!(
/// cake::Entity::insert(orange)
/// .on_empty_do_nothing()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

umm, why do we need to change the examples here? I think this PR should have no changes to exsiting features

@@ -88,3 +91,95 @@ where
QuerySelect::column_as(self, col, alias)
}
}

/// Insert query Trait
pub trait InsertTrait<A>: Sized
Copy link
Member

@tyt2y3 tyt2y3 Jun 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need a new Trait here? it is not strictly necessary? I am cautious about new traits, mostly because of the potential impact to compile time. And introducing a new type to the public API require a lot of thought.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a must, we just want to share the common methods between Insert and TryInsert. Methods such as one, many, add...etc.

Copy link
Member

@tyt2y3 tyt2y3 Jun 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They can all implement the same methods. I don't see InsertTrait being used in a type signature anywhere so I think it is not needed. Unless someone is accepting a generic I: InsertTrait.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My concern is code duplication. Implement the same method twice, for two struct, Insert and TryInsert.

We can use macro, but I think InsertTrait to define shared methods might be better. Both are okay for me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the idea is to avoid unused trait?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. technically TryInsert contains an Insert so any implementation can simply be delegated. I am not seeing where's the potential saving in code duplication.

src/query/insert.rs Outdated Show resolved Hide resolved
@tyt2y3
Copy link
Member

tyt2y3 commented Jun 19, 2023

Looks great so far! Please finish up this PR, and then make a follow up PR:

So, in https://github.com/SeaQL/sea-orm/blob/master/tests/upsert_tests.rs we have:

    let res = Entity::insert_many([..])
    .on_conflict(on_conflict)
    .exec(db)
    .await;

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

Can we add a do_nothing method that converts Insert into TryInsert, which would then 'absorbs' the DbErr::RecordNotInserted?

Then, we'd have (in addition to the above):

    let res = Entity::insert_many([..])
    .on_conflict(on_conflict)
    .do_nothing()
    .exec(db)
    .await;

    assert_eq!(res, Ok(TryInsertResult::Conflicted));

I am aware that we'd have to write do_nothing twice, which is not DRY, but I guess it's still a good step forward.

@billy1624 billy1624 linked an issue Jun 19, 2023 that may be closed by this pull request
Copy link
Member

@tyt2y3 tyt2y3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still clean, isn't it

@darkmmon darkmmon mentioned this pull request Jun 20, 2023
2 tasks
.into_iter()
.filter(|_| false);

let empty_insert = Bakery::insert_many(empty_iterator)
Copy link
Member

@tyt2y3 tyt2y3 Jun 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tyt2y3 tyt2y3 merged commit 92ea837 into SeaQL:master Jun 20, 2023
darkmmon added a commit to darkmmon/seaql.github.io that referenced this pull request Jul 12, 2023
billy1624 added a commit to SeaQL/seaql.github.io that referenced this pull request Jul 19, 2023
* Optional Field SeaQL/sea-orm#1513

* .gitignore SeaQL/sea-orm#1334

* migration table custom name SeaQL/sea-orm#1511

* OR condition relation SeaQL/sea-orm#1433

* DerivePartialModel SeaQL/sea-orm#1597

* space for migration file naming SeaQL/sea-orm#1570

* composite key up to 12 SeaQL/sea-orm#1508

* seaography integration SeaQL/sea-orm#1599

* QuerySelect SimpleExpr SeaQL/sea-orm#1702

* sqlErr SeaQL/sea-orm#1707

* migration check SeaQL/sea-orm#1519

* postgres array SeaQL/sea-orm#1565

* param intoString SeaQL/sea-orm#1439

* **skipped** re-export SeaQL/sea-orm#1661

* ping SeaQL/sea-orm#1627

* on empty do nothing SeaQL/sea-orm#1708

* on conflict do nothing SeaQL/sea-orm#1712

* **skipped** upgrade versions

* active enum fail safe SeaQL/sea-orm#1374

* relation generation check SeaQL/sea-orm#1435

* entity generation bug SeaQL/sea-schema#105

* **skipped** bug fix that does not require edits

* EnumIter change SeaQL/sea-orm#1535

* completed and fixed a previous todo SeaQL/sea-orm#1570

* amended wordings and structures

* Edit

* Remove temp file

---------

Co-authored-by: Billy Chan <ccw.billy.123@gmail.com>
@github-actions
Copy link

github-actions bot commented Aug 2, 2023

🎉 Released In 0.12.1 🎉

Thank you everyone for the contribution!
This feature is now available in the latest release. Now is a good time to upgrade!
Your participation is what makes us unique; your adoption is what drives us forward.
You can support SeaQL 🌊 by starring our repos, sharing our libraries and becoming a sponsor ⭐.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

insert_many failing if the models iterator is empty
3 participants