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

feat: restrict instance methods if no primary key #15108

Merged

Conversation

frostzt
Copy link
Contributor

@frostzt frostzt commented Oct 8, 2022

Pull Request Checklist

  • Have you added new tests to prevent regressions?
  • If a documentation update is necessary, have you opened a PR to the documentation repository?
  • Did you update the typescript typings accordingly (if applicable)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Does the name of your PR follow our conventions?

Description Of Change

Instance methods such as save, update, and others will now throw an error if no primaryKey was passed by the user.

Closes: #14402
Closes #15113

Todo

  • Integration tests for save and update are failing.

Copy link
Member

@ephys ephys left a comment

Choose a reason for hiding this comment

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

You actually discovered another issues related to PKs. They're closely related but not fully the same. Ok with fixing both in this PR? :)

src/model.js Outdated Show resolved Hide resolved
test/unit/instance/decrement.test.js Outdated Show resolved Hide resolved
@frostzt
Copy link
Contributor Author

frostzt commented Oct 9, 2022

@ephys I would love to do it! Thanks for the review lemme get this resolved!

src/model.js Outdated Show resolved Hide resolved
test/unit/instance/reload.test.js Outdated Show resolved Hide resolved
test/unit/instance/restore.test.js Outdated Show resolved Hide resolved
test/unit/instance/save.test.js Outdated Show resolved Hide resolved
test/unit/model/upsert.test.js Outdated Show resolved Hide resolved
src/model.js Outdated Show resolved Hide resolved
@frostzt frostzt requested a review from ephys October 10, 2022 17:48
Copy link
Member

@WikiRik WikiRik left a comment

Choose a reason for hiding this comment

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

Few comments about the tests. There might be valid answers but this is what I noticed

test/unit/instance/save.test.js Outdated Show resolved Hide resolved
test/unit/instance/save.test.js Outdated Show resolved Hide resolved
test/integration/instance/update.test.js Outdated Show resolved Hide resolved
test/unit/instance/restore.test.js Show resolved Hide resolved
test/integration/instance/decrement.test.js Outdated Show resolved Hide resolved
@frostzt frostzt requested review from WikiRik and removed request for ephys October 11, 2022 05:03
ephys
ephys previously requested changes Oct 11, 2022
Copy link
Member

@ephys ephys left a comment

Choose a reason for hiding this comment

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

There are failing tests related to upsert in the suite, I'll take a look when I can to see what it is about

test/integration/instance/restore.test.js Outdated Show resolved Hide resolved
test/integration/instance/restore.test.js Outdated Show resolved Hide resolved
test/integration/instance/restore.test.js Outdated Show resolved Hide resolved
test/integration/instance/save.test.js Outdated Show resolved Hide resolved
test/unit/instance/save.test.js Outdated Show resolved Hide resolved
test/unit/instance/update.test.js Outdated Show resolved Hide resolved
@frostzt frostzt requested review from ephys and removed request for WikiRik October 11, 2022 12:06
@frostzt
Copy link
Contributor Author

frostzt commented Oct 11, 2022

Alright for some reason the save, update, and restore in the integration tests are being fulfilled when I expect them to throw!?

@ephys
Copy link
Member

ephys commented Oct 13, 2022

Regarding the failing tests:

Unit Tests

Restore Test

The restore test is failing because it's calling save. Because it's a new model, save uses INSERT instead of UPDATE. Which means it doesn't call .where().

How to fix it:

  • restore should call update instead of save
  • update should throw if isNewRecord is true (can you add a unit test for that too?)
  • The unit test should have isNewRecord set to false

The Upsert tests

I think upsert should be investigated deeper. Its current implementation sounds very strange to me. But let's keep that for its own PR.

In the meantime, instead of calling instance.where(), it could create the "where" object full of primary keys itself. Like what where() does but without throwing. It's a temporary measure until we can investigate upsert further.

Integration Tests

restore, update, save

None of these methods changes any value on the model (model is not deleted, restoring doesn't change the value of deletedAt), update and save don't change anything. The method aborts before it gets to .where().

How to solve this: we should call where() sooner in save() (remember: only in update mode, not in insert mode). I prefer the consistency of it throwing even if there is nothing to save.

@frostzt
Copy link
Contributor Author

frostzt commented Oct 14, 2022

Thanks for the review @ephys I have updated the PR, I don't know if that's exactly what you mentioned, seems like its gonna take me some time understanding the code and the way the repo works haha!

@WikiRik
Copy link
Member

WikiRik commented Nov 10, 2022

@ephys ping

@ephys
Copy link
Member

ephys commented Nov 15, 2022

I've pushed a commit that should fix our unit tests. Let's see whether this passes now

@frostzt don't hesitate to review commit c5eb5e4

I added a bunch of TODOs because the way Model.upsert currently works is a bit unclean, but it's unrelated to your work

@frostzt
Copy link
Contributor Author

frostzt commented Nov 16, 2022

Thank you @ephys

@ephys
Copy link
Member

ephys commented Nov 16, 2022

The failing test in postgres/mysql/mariadb/sqlite is an easy fix: this line must use create instead of build

The failing DB2/mssql should be fixed by changing this line

instance.where(false, true),
to instance.where(false, true) ?? {},

@frostzt
Copy link
Contributor Author

frostzt commented Nov 16, 2022

@ephys I'll get onto this post this week. Actually just got a lot work.

@ephys
Copy link
Member

ephys commented Nov 16, 2022

Take your time, I took a month to get back to this 😅

@WikiRik WikiRik marked this pull request as draft November 16, 2022 12:17
@ephys ephys marked this pull request as ready for review November 16, 2022 18:21
@ephys ephys requested a review from WikiRik November 16, 2022 18:21
Copy link
Member

@WikiRik WikiRik left a comment

Choose a reason for hiding this comment

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

Looks good to me. Not the biggest fan of how rejectedWith does not check if you've included the entire error message but that's not a big issue. The error messages we're testing for in this PR are specific enough

@ephys
Copy link
Member

ephys commented Nov 16, 2022

I don't mind personally, my goal using rejectedWith is just to check that the right error is being thrown, not an unrelated one.

@ephys ephys dismissed their stale review November 16, 2022 18:43

made changes

@WikiRik
Copy link
Member

WikiRik commented Nov 16, 2022

Yeah, that goal is being fulfilled so we're all good

@WikiRik
Copy link
Member

WikiRik commented Nov 16, 2022

@ephys I'll leave merging this up to you in case you want to wait for the review of @frostzt

@frostzt
Copy link
Contributor Author

frostzt commented Nov 17, 2022

Thank you @ephys and @WikiRik I'll check the commits since I wanna know why this was breaking. Just give me this week post that I'll be back!

@frostzt
Copy link
Contributor Author

frostzt commented Nov 20, 2022

Hey @ephys I got it now, do you think it is good to go?

@ephys
Copy link
Member

ephys commented Nov 20, 2022

unless you see anything else, I think it's ready

@frostzt
Copy link
Contributor Author

frostzt commented Nov 20, 2022

Awesome @ephys I don't really notice anything aside the TODOs which I think I can take on in other PRs!

@ephys
Copy link
Member

ephys commented Nov 20, 2022

@WikiRik I'm doing the merge (for the breaking changes commit message)

@ephys ephys merged commit 3095d99 into sequelize:main Nov 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants