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

Fix issue with Unique Records not being removed when the unique fields are value fields #510

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

AlexStansfield
Copy link
Contributor

@AlexStansfield AlexStansfield commented Nov 17, 2023

See Issue #509 for all the details.

I've since taken the tests created for that issue and improved them slightly.

Changes to Model.js:

  • Prepare the properties earlier so that we can tell the unique fields have actually changed
  • Made a change to the how it determines hasUniqueProperties by also checking params.remove
  • Create new copies of properties and params to pass to prepareProperties to avoid issues due to the method mutating the objects causing conflicts if run multiple times

@AlexStansfield
Copy link
Contributor Author

I'm not really happy with the way I had to do this, but because prepareProperties mutates the properties and params in certain situations this caused issues (like adding remove twice on fields).

Personally I think the prepareProperties shouldn't mutate these objects and should return new objects with the changes, but that seems like a huge task so this seemed like the way around it.

If you know a better way I'd be happy to hear them and update accordingly

@mobsense
Copy link
Contributor

mobsense commented Dec 4, 2023

Alex, thanks for the work on this.

I agree with the comment about mutating properties.

How is this working for you in practice?

I'll wait for 2 other PRs to have some test, then I'll merge this one.

@AlexStansfield
Copy link
Contributor Author

AlexStansfield commented Dec 7, 2023

Actually I was a little worried about the code just because that prepareProperties is so complicated that I really couldn't be sure that even though the tests passed this wasn't still messing something up so for now we've taken uniqueness off the schema and doing it only with the existing validation code that checked for duplicates. Not ideal but better than broken data.

Was waiting to hear back from you guys what you thought.

@mobsense
Copy link
Contributor

mobsense commented Dec 8, 2023

We use uniqueness rarely. It does add a lot of complexity, so we use it for things that really require it like email addresses and account names.

@AlexStansfield
Copy link
Contributor Author

AlexStansfield commented Dec 14, 2023

We use uniqueness rarely. It does add a lot of complexity, so we use it for things that really require it like email addresses and account names.

Our use case is ensuring unique email on user records. The issue for us is we don't want to hard delete users, we want to soft delete them. So a simple unique on email field doesn't suffice, no user would be able to sign back up with the same email if their account is deleted.

Solution we found was to use a value function to set a uniqueEmail field based on the value of email and deletedAt and have uniqueEmail be the unique field. But then we ran into this problem.

If you think my PR is a good fix then I'm happy for you to merge it so we can turn the uniqueness back on.

@mobsense
Copy link
Contributor

mobsense commented Jan 7, 2024

Alex, sorry for the delay in looking into this. Been swamped with other stuff.

I'm concerned about the risk of change in the PR -- so that is why I've not just merged it already. I need to dig into it closely.

I'll try to get to this soon.

Michael

@AlexStansfield
Copy link
Contributor Author

Alex, sorry for the delay in looking into this. Been swamped with other stuff.

I'm concerned about the risk of change in the PR -- so that is why I've not just merged it already. I need to dig into it closely.

No problem, take your time

@mobsense
Copy link
Contributor

mobsense commented Feb 6, 2024

Just an update, digging into this now and should have feedback very soon.

@mobsense
Copy link
Contributor

mobsense commented Feb 6, 2024

Sorry for the delay.

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