-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Allow models to specifiy conflict keys #12597
Conversation
fixes Kong#12288 The upsert DAO function generated uses the primary key as an "ON CONFLICT" match. This might not be the case if a field uses a unique flag or if that field is also workgroupable. Postgresql requires an index to be specified. I tried autodetecting the correct behavior but it failed on other models. Changing the primary key also did not work. I added a way to specify the conflict index. It defaults to the primary key.
This feels like a generic issue with workspaces, if this is true? |
Yes. And it kind of depends how the model is used. I tried to autodetect this by setting the unique_key from the first unique index encountered that has a workspace setting. This unfortunately failed on other plugins but for the key_auth it is the correct way to handle it. |
Thanks for proposal. I did spend some time to figure out a generic fix, but didn't yet find anything. Did you try already the (or something similar to it):
I feel we need to have the |
I think the problem is here: local conflict_key = unique_escaped
if has_composite_cache_key and not unique_field.is_endpoint_key then
conflict_key = escape_identifier(connector, "cache_key")
end In the key-auth plugin, "key" is the cache_key and also the endpoint_key. |
This PR will prevent upserts from working when trying to insert an entity with an existing ID, but changes in the columns the are in the secondary unique index. The conflict on the ID will no longer be detected and the insert will then fail. Given that It may still be possible to handle some problem using triggers, but the general recommendation to solve the specific problem at hand (failing imports using |
I tried to understand your reasoning and looked at the code again but I can't. The current implementation is broken at least since 3.4 or 3.2 on a workflow that worked before. This is a example config: _format_version: "1.1"
services:
- name: admin-api
url: http://localhost:8002
plugins:
- name: acl
config:
allow:
- admin-api-group
hide_groups_header: true
tags:
- admin-api
tags:
- admin-api
routes:
- name: admin-api
hosts:
- admin-api
paths:
- /admin-api
plugins:
- name: key-auth
config:
hide_credentials: true
tags:
- admin-api
tags:
- admin-api
consumers:
- username: admin-api-user
acls:
- group: admin-api-group
keyauth_credentials:
- key: very-secret-key Now, second run of the migration yields:
3rd run:
The ID is clearly autogenerated and the important unique key is |
ping @hanshuebner |
Why can't you pre-generate the IDs for all entities in your configuration file? |
Summary
The upsert DAO function generated uses the primary key as an "ON CONFLICT" match. This might not be the case if a field uses a unique flag or if that field is also workgroupable. Postgresql requires an index to be specified.
I tried autodetecting the correct behavior but it failed on other models. Changing the primary key also did not work. I added a way to specify the conflict index.
It defaults to the primary key.
Checklist
changelog/unreleased/kong
orskip-changelog
label added on PR if changelog is unnecessary. README.mdIssue reference
Fix #12288