Repository navigation
Correct unique constraints for UserConsent entity #14793
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
6
10000
7 changes: 67 additions & 0 deletions
67
...keycloak/connections/jpa/updater/liquibase/custom/JpaUpdate25_0_0_ConsentConstraints.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * Copyright 2024 Red Hat, Inc. and/or its affiliates | ||
| * and other contributors as indicated by the @author tags. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.keycloak.connections.jpa.updater.liquibase.custom; | ||
|
|
||
| import liquibase.exception.CustomChangeException; | ||
| import liquibase.statement.core.RawSqlStatement; | ||
|
|
||
| public class JpaUpdate25_0_0_ConsentConstraints extends CustomKeycloakTask { | ||
|
|
||
| @Override | ||
| protected void generateStatementsImpl() throws CustomChangeException { | ||
| final String userConsentClientScopeTable = getTableName("USER_CONSENT_CLIENT_SCOPE"); | ||
| final String userConsentTable = getTableName("USER_CONSENT"); | ||
| statements.add(new RawSqlStatement( | ||
| "DELETE FROM "+ userConsentClientScopeTable + " WHERE USER_CONSENT_ID IN (" + | ||
| " SELECT uc.ID FROM "+userConsentTable+" uc INNER JOIN (" + | ||
| " SELECT CLIENT_ID, USER_ID, MAX(LAST_UPDATED_DATE) AS MAX_UPDATED_DATE FROM " + userConsentTable + | ||
| " GROUP BY CLIENT_ID, USER_ID HAVING COUNT(*) > 1 ) max_dates ON uc.CLIENT_ID = max_dates.CLIENT_ID" + | ||
| " AND uc.USER_ID = max_dates.USER_ID AND uc.LAST_UPDATED_DATE = max_dates.MAX_UPDATED_DATE)" | ||
| )); | ||
| statements.add(new RawSqlStatement( | ||
| " DELETE FROM "+userConsentTable+" WHERE ID IN (" + | ||
| " SELECT uc.ID FROM "+userConsentTable+" uc INNER JOIN (" + | ||
| " SELECT CLIENT_ID, USER_ID, MAX(LAST_UPDATED_DATE) AS MAX_UPDATED_DATE" + | ||
| " FROM "+userConsentTable+" GROUP BY CLIENT_ID, USER_ID HAVING COUNT(*) > 1 )" + | ||
| " max_dates ON uc.CLIENT_ID = max_dates.CLIENT_ID" + | ||
| " AND uc.USER_ID = max_dates.USER_ID AND uc.LAST_UPDATED_DATE = max_dates.MAX_UPDATED_DATE )" | ||
| )); | ||
| statements.add(new RawSqlStatement( | ||
| " DELETE FROM "+ userConsentClientScopeTable + " WHERE USER_CONSENT_ID IN (" + | ||
| " SELECT uc.ID FROM "+userConsentTable+" uc INNER JOIN (" + | ||
| " SELECT CLIENT_STORAGE_PROVIDER, EXTERNAL_CLIENT_ID, USER_ID, MAX(LAST_UPDATED_DATE) AS MAX_UPDATED_DATE" + | ||
| " FROM "+userConsentTable+" GROUP BY CLIENT_STORAGE_PROVIDER, EXTERNAL_CLIENT_ID, USER_ID HAVING COUNT(*) > 1 )" + | ||
| " max_dates ON uc.CLIENT_STORAGE_PROVIDER = max_dates.CLIENT_STORAGE_PROVIDER" + | ||
| " AND uc.EXTERNAL_CLIENT_ID = max_dates.EXTERNAL_CLIENT_ID AND uc.USER_ID = max_dates.USER_ID AND uc.LAST_UPDATED_DATE = max_dates.MAX_UPDATED_DATE )" | ||
| )); | ||
| statements.add(new RawSqlStatement( | ||
| " DELETE FROM "+userConsentTable+" WHERE ID IN (" + | ||
| " SELECT uc.ID FROM "+userConsentTable+" uc INNER JOIN (" + | ||
| " SELECT CLIENT_STORAGE_PROVIDER, EXTERNAL_CLIENT_ID, USER_ID, MAX(LAST_UPDATED_DATE) AS MAX_UPDATED_DATE" + | ||
| " FROM "+userConsentTable+" GROUP BY CLIENT_STORAGE_PROVIDER, EXTERNAL_CLIENT_ID, USER_ID HAVING COUNT(*) > 1 )" + | ||
| " max_dates ON uc.CLIENT_STORAGE_PROVIDER = max_dates.CLIENT_STORAGE_PROVIDER" + | ||
| " AND uc.EXTERNAL_CLIENT_ID = max_dates.EXTERNAL_CLIENT_ID AND uc.USER_ID = max_dates.USER_ID AND uc.LAST_UPDATED_DATE = max_dates.MAX_UPDATED_DATE )" | ||
| )); | ||
| } | ||
|
|
||
| @Override | ||
| protected String getTaskId() { | ||
| return "Correct User Consent Unique Constraints for PostgreSQL and MariaDB"; | ||
| } | ||
|
|
||
| } | ||
|
|
77 changes: 77 additions & 0 deletions
77
...ak/connections/jpa/updater/liquibase/custom/JpaUpdate25_0_0_MySQL_ConsentConstraints.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * Copyright 2024 Red Hat, Inc. and/or its affiliates | ||
| * and other contributors as indicated by the @author tags. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.keycloak.connections.jpa.updater.liquibase.custom; | ||
|
|
||
| import liquibase.exception.CustomChangeException; | ||
| import liquibase.statement.core.DeleteStatement; | ||
| import liquibase.statement.core.RawSqlStatement; | ||
| import liquibase.structure.core.Table; | ||
|
|
||
| public class JpaUpdate25_0_0_MySQL_ConsentConstraints extends CustomKeycloakTask { | ||
|
|
||
| @Override | ||
| protected void generateStatementsImpl() throws CustomChangeException { | ||
| final String userConsentClientScopeTable = getTableName("USER_CONSENT_CLIENT_SCOPE"); | ||
| final String userConsentTable = getTableName("USER_CONSENT"); | ||
| statements.add(new RawSqlStatement( | ||
| "DELETE FROM "+ userConsentClientScopeTable + " WHERE USER_CONSENT_ID IN (" + | ||
| " SELECT uc.ID FROM "+userConsentTable+" uc INNER JOIN (" + | ||
| " SELECT CLIENT_ID, USER_ID, MAX(LAST_UPDATED_DATE) AS MAX_UPDATED_DATE FROM " + userConsentTable + | ||
| " GROUP BY CLIENT_ID, USER_ID HAVING COUNT(*) > 1 ) max_dates ON uc.CLIENT_ID = max_dates.CLIENT_ID" + | ||
| " AND uc.USER_ID = max_dates.USER_ID AND uc.LAST_UPDATED_DATE = max_dates.MAX_UPDATED_DATE)" | ||
| )); | ||
| statements.add(new RawSqlStatement( | ||
| " CREATE TEMPORARY TABLE TEMP_USER_CONSENT_IDS" + | ||
| " AS SELECT uc.ID FROM "+userConsentTable+" uc INNER JOIN (" + | ||
| " SELECT CLIENT_ID, USER_ID, MAX(LAST_UPDATED_DATE) AS MAX_UPDATED_DATE" + | ||
| " FROM "+userConsentTable+" GROUP BY CLIENT_ID, USER_ID HAVING COUNT(*) > 1 )" + | ||
| " max_dates ON uc.CLIENT_ID = max_dates.CLIENT_ID" + | ||
| " AND uc.USER_ID = max_dates.USER_ID AND uc.LAST_UPDATED_DATE = max_dates.MAX_UPDATED_DATE" | ||
| )); | ||
| statements.add(new DeleteStatement(null, null, database.correctObjectName("USER_CONSENT", Table.class)) | ||
| .setWhere("ID IN (SELECT ID FROM TEMP_USER_CONSENT_IDS)")); | ||
| statements.add(new RawSqlStatement("DROP TEMPORARY TABLE IF EXISTS TEMP_USER_CONSENT_IDS")); | ||
|
|
||
| statements.add(new RawSqlStatement( | ||
| " DELETE FROM "+ userConsentClientScopeTable + " WHERE USER_CONSENT_ID IN (" + | ||
| " SELECT uc.ID FROM "+userConsentTable+" uc INNER JOIN (" + | ||
| " SELECT CLIENT_STORAGE_PROVIDER, EXTERNAL_CLIENT_ID, USER_ID, MAX(LAST_UPDATED_DATE) AS MAX_UPDATED_DATE" + | ||
| " FROM "+userConsentTable+" GROUP BY CLIENT_STORAGE_PROVIDER, EXTERNAL_CLIENT_ID, USER_ID HAVING COUNT(*) > 1 )" + | ||
| " max_dates ON uc.CLIENT_STORAGE_PROVIDER = max_dates.CLIENT_STORAGE_PROVIDER" + | ||
| " AND uc.EXTERNAL_CLIENT_ID = max_dates.EXTERNAL_CLIENT_ID AND uc.USER_ID = max_dates.USER_ID AND uc.LAST_UPDATED_DATE = max_dates.MAX_UPDATED_DATE )" | ||
| )); | ||
| statements.add(new RawSqlStatement( | ||
| "CREATE TEMPORARY TABLE TEMP_USER_CONSENT_IDS2" + | ||
| " AS SELECT uc.ID FROM "+userConsentTable+" uc INNER JOIN (" + | ||
| " SELECT CLIENT_STORAGE_PROVIDER, EXTERNAL_CLIENT_ID, USER_ID, MAX(LAST_UPDATED_DATE) AS MAX_UPDATED_DATE" + | ||
| " FROM "+userConsentTable+" GROUP BY CLIENT_STORAGE_PROVIDER, EXTERNAL_CLIENT_ID, USER_ID HAVING COUNT(*) > 1 )" + | ||
| " max_dates ON uc.CLIENT_STORAGE_PROVIDER = max_dates.CLIENT_STORAGE_PROVIDER" + | ||
| " AND uc.EXTERNAL_CLIENT_ID = max_dates.EXTERNAL_CLIENT_ID AND uc.USER_ID = max_dates.USER_ID AND uc.LAST_UPDATED_DATE = max_dates.MAX_UPDATED_DATE;" | ||
| )); | ||
| statements.add(new DeleteStatement(null, null, database.correctObjectName("USER_CONSENT", Table.class)) | ||
| .setWhere("ID IN (SELECT ID FROM TEMP_USER_CONSENT_IDS2)")); | ||
| statements.add(new RawSqlStatement("DROP TEMPORARY TABLE IF EXISTS TEMP_USER_CONSENT_IDS2")); | ||
| } | ||
|
|
||
| @Override | ||
| protected String getTaskId() { | ||
| return "Correct User Consent Unique Constraints for MySQL"; | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 am personally not in favor to remove this check, and the reason is that we have similar checks in many other providers. If we decide to go down this route of relying solely on the DB constraints it would call for a broader change in all the providers.
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 do not like double checks for the same thing. If you think that it is not a performance problem, I will add it back.