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

Add API for checking uniqueness constraints for updates #1140

Merged
merged 5 commits into from
Oct 29, 2020

Conversation

ivanbakel
Copy link
Contributor

@ivanbakel ivanbakel commented Oct 26, 2020

Entities which are already in the DB will conflict with themselves when using checkUnique, unless the value of each unique key is changed. This adds an alternative checkUniqueUpdateable which ignores conflicts from an entity with itself, so that uniqueness constraints can be checked without an update.

Before submitting your PR, check that you've:

After submitting your PR:

  • Update the Changelog.md file with a link to your PR
  • Bumped the version number if there isn't an (unreleased) on the Changelog
  • Check that CI passes (or if it fails, for reasons unrelated to your change, like CI timeouts)

Entities which are already in the DB will conflict with themselves when
using `checkUnique`, unless the value of each unique key is changed.
This adds an alternative `checkUniqueUpdateable` which ignores conflicts
from an entity with itself, so that uniqueness constraints can be
checked without an update.
Copy link
Collaborator

@parsonsmatt parsonsmatt left a comment

Choose a reason for hiding this comment

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

Would you mind writing a quick test for this?

Nothing -> checkUniqueKeysUpdateable key xs
Just (Entity k _)
| key == k -> checkUniqueKeysUpdateable key xs
Just _ -> return (Just x)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is fantastic, thanks!

--
-- While this would be 'Just' because SPJ already exists:
--
-- > mSpjConst <- checkUnique $ User "SPJ" 60
Copy link
Collaborator

Choose a reason for hiding this comment

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

Awesome docs, thanks 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can't take the credit - these are largely copied from checkUnique, plus a paragraph on the difference in behaviour.

@ivanbakel
Copy link
Contributor Author

Alright, I've added a test for the basic behaviour - an entity not clashing with itself, but still clashing with other entities.

@parsonsmatt parsonsmatt merged commit cbefd4d into yesodweb:master Oct 29, 2020
@parsonsmatt
Copy link
Collaborator

Looks great, thanks so much!

@parsonsmatt parsonsmatt added this to the 2.11 milestone Oct 29, 2020
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.

None yet

2 participants