-
Notifications
You must be signed in to change notification settings - Fork 223
feat: add option to enforce unique attribute #49
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
base: main
Are you sure you want to change the base?
Conversation
2492bb4
to
45e8b03
Compare
@vishalnayak almost certainly of interest to you |
This is eliminating the need of higher level application code to check that the field value being inserted is not already being used in another object that is already inserted, which seems to be nice. However, this PR is also overloading another meaning to |
I think that updates should be allowed as long as the value is not used elsewhere. Currently, "other" fields in the object cannot be updated because the uniqueness constraint on one field disallows updating the object altogether. |
@vishalnayak I expected this to come up and when I initially took a stab, updates seemed complicated to resolve. I had introduced an Giving a second look at the code, it seems that updates could be allowed and be a little more smarter, but might be a little expensive as well. An unrelated question, not sure who can answer @jefferai or @vishalnayak, Can I remove |
@hbagdi I do strongly feel that update is a very common use case for an object (at least Vault uses memdb in such a manner). I also wonder about the double iteration of Regarding merging the functionality of |
txn.go
Outdated
// On an update, there is an existing object with the given | ||
// primary ID. We do the update by deleting the current object | ||
// and inserting the new object. | ||
if update && idSchema.Unique && idSchema.EnforceUnique { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not certain at this point if this PR will get merged or not. In any case, this check should certainly go away.
The problem here is if you allow updates using Currently, Also, Now, if a user of this application would like to get an error if there is duplicate I'd happy to add an Let me know your thoughts! |
Friendly ping on this to keep moving forward. Thanks! |
@jefferai @vishalnayak Would you guys be interesting in moving this forward? |
@hbagdi We are, but please address the issue Vishal left -- we should explicitly not allow EnforceUnique on the ID. That way we can know if it's an insert or an update via the current API. This check can be done as part of the schema validation. Also probably the error should be a defined value for an enforce uniqueness violation to help the application understand what the issue is. |
For my use-case, I specifically want to throw an error if when an |
Can you describe your actual use-case then? Is it purely to have a knob to never allow updates at all? Because that probably makes more sense at a table level. |
I'm using Is it possible to release a v1 of this package and then add these APIs in the v2 version of the library, such that we keep the backwards compat by default (and give users to change the behavior as an opt-in, with |
But how do you propose to actually do this? Those seem fundamentally at odds with the current proposal. If you want to update you need to call Insert with an ID so that we know what to update. If EnforceUnique is true then this will error, meaning you can't ever actually update. If there was an Update call coupled with a table option to disallow updates via insert, then that would probably do what you want. But it doesn't really make sense to disallow updates per index -- objects are put into tables, indices are just built on top of the objects. |
Indeed. In a very early incarnation of this PR, I had introduced an
Correct. We will need to use the index to check for duplicates though. |
Friendly ping. |
Hi there! What do you need on our end? It sounded like we were in agreement on the way forward but there have been no updates to the code. |
Apologies for dropping the balls on this one, it has been a busy year.
Is this an acceptable solution to this library? I think to solve this problem, it is necessary to distinguish between an |
This has stalled and is out of date. |
Hi @hbagdi , Just to be clear, is it that you don't have time to get this across the finish line? I think we were just waiting on updates from you. It seems like we'd all agreed on the need for Update instead of changing Insert's behavior. |
Reopening for tracking. |
Okay, backwards compatibility remains a challenge but I'll work on it. |
Is backwards compat a challenge with a separate Update method and non-default option to disallow updates via insert? |
No, then that shouldn't be a problem. I now know what to do. I'll ping back if I run into another blocker. |
Enforcing Uniqueness on an Index can go a long way since higher-level application code doesn't need to add explicit checks for it. To keep the change backwards compatible, changes have been made to `Insert()` method to respect Unique constraints. Insert will not allow update if `EnforceUniqueness` is set to true on the id schema. If any other schema of the table has EnforceUnique, updates using `Insert()` will error out if the same value exists in the schema. A new `Update()` has been added to allow for updates when `EnforceUniqueness` is set.
45e8b03
to
44c2c01
Compare
@jefferai I updated the PR with what we are talking above. |
@@ -213,6 +216,15 @@ func (txn *Txn) Insert(table string, obj interface{}) error { | |||
} | |||
} | |||
|
|||
if indexSchema.EnforceUniqueness && indexSchema.Unique { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this redundant with the check above?
@@ -93,7 +93,9 @@ type IndexSchema struct { | |||
// exist from a structure. | |||
AllowMissing bool | |||
|
|||
Unique bool | |||
Unique bool | |||
EnforceUniqueness bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the naming of this is a bit confusing, like, why wouldn't the Unique val already require this? Maybe something like DisallowUpdatesViaInsert?
Enforcing Uniqueness on an Index can go a long way since higher-level
application code doesn't need to add explicit checks for it.
To keep the change backwards compatible, changes have been made to
Insert()
method to respect Unique constraints.Insert will not allow update if EnforceUnique is set to true on the id
schema.
If any other schema of the table has EnforceUnique, updates using
Insert()
will error out if the same value exists in the schema.