Ensure entity version is indexed

Closes #14161
This commit is contained in:
vramik 2022-09-07 20:40:43 +02:00 committed by Pedro Igor
parent 3d910bc7b0
commit 4f4dbd622a
20 changed files with 89 additions and 22 deletions

View file

@ -20,6 +20,7 @@ import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
@ -58,6 +59,10 @@ public class JpaAuthenticationSessionEntity extends UpdatableEntity.Impl impleme
@Column
private int version;
@Column(insertable = false, updatable = false)
@Basic(fetch = FetchType.LAZY)
private Integer entityVersion;
@Type(type = "jsonb")
@Column(columnDefinition = "jsonb")
private final JpaAuthenticationSessionMetadata metadata;
@ -77,6 +82,10 @@ public class JpaAuthenticationSessionEntity extends UpdatableEntity.Impl impleme
this.metadata = new JpaAuthenticationSessionMetadata(cloner);
}
public boolean isMetadataInitialized() {
return metadata != null;
}
public void setParent(JpaRootAuthenticationSessionEntity root) {
this.root = root;
}
@ -93,7 +102,8 @@ public class JpaAuthenticationSessionEntity extends UpdatableEntity.Impl impleme
@Override
public Integer getEntityVersion() {
return metadata.getEntityVersion();
if (isMetadataInitialized()) return metadata.getEntityVersion();
return entityVersion;
}
@Override

View file

@ -73,6 +73,10 @@ public class JpaComponentEntity extends UpdatableEntity.Impl implements MapCompo
@Column
private int version;
@Column(insertable = false, updatable = false)
@Basic(fetch = FetchType.LAZY)
private Integer entityVersion;
@Column(insertable = false, updatable = false)
@Basic(fetch = FetchType.LAZY)
private String providerType;
@ -117,7 +121,8 @@ public class JpaComponentEntity extends UpdatableEntity.Impl implements MapCompo
@Override
public Integer getEntityVersion() {
return this.metadata.getEntityVersion();
if (isMetadataInitialized()) return this.metadata.getEntityVersion();
return entityVersion;
}
@Override

View file

@ -63,6 +63,10 @@ public class JpaUserConsentEntity extends UpdatableEntity.Impl implements MapUse
@Basic(fetch = FetchType.LAZY)
private String clientId;
@Column(insertable = false, updatable = false)
@Basic(fetch = FetchType.LAZY)
private Integer entityVersion;
@Type(type = "jsonb")
@Column(columnDefinition = "jsonb")
private final JpaUserConsentMetadata metadata;
@ -79,8 +83,13 @@ public class JpaUserConsentEntity extends UpdatableEntity.Impl implements MapUse
this.metadata = new JpaUserConsentMetadata(cloner);
}
public boolean isMetadataInitialized() {
return metadata != null;
}
public Integer getEntityVersion() {
return this.metadata.getEntityVersion();
if (isMetadataInitialized()) return this.metadata.getEntityVersion();
return entityVersion;
}
public void setEntityVersion(Integer version) {
@ -98,7 +107,8 @@ public class JpaUserConsentEntity extends UpdatableEntity.Impl implements MapUse
@Override
public String getClientId() {
return this.metadata.getClientId();
if (isMetadataInitialized()) return this.metadata.getClientId();
return clientId;
}
@Override

View file

@ -54,6 +54,10 @@ public class JpaUserFederatedIdentityEntity extends UpdatableEntity.Impl impleme
@GeneratedValue
private UUID id;
@Column(insertable = false, updatable = false)
@Basic(fetch = FetchType.LAZY)
private Integer entityVersion;
@Column(insertable = false, updatable = false)
@Basic(fetch = FetchType.LAZY)
private String identityProvider;
@ -78,8 +82,13 @@ public class JpaUserFederatedIdentityEntity extends UpdatableEntity.Impl impleme
this.metadata = new JpaUserFederatedIdentityMetadata(cloner);
}
public boolean isMetadataInitialized() {
return metadata != null;
}
public Integer getEntityVersion() {
return this.metadata.getEntityVersion();
if (isMetadataInitialized()) return this.metadata.getEntityVersion();
return entityVersion;
}
public void setEntityVersion(Integer version) {
@ -107,7 +116,8 @@ public class JpaUserFederatedIdentityEntity extends UpdatableEntity.Impl impleme
@Override
public String getUserId() {
return this.metadata.getUserId();
if (isMetadataInitialized()) return this.metadata.getUserId();
return userId;
}
@Override
@ -117,7 +127,8 @@ public class JpaUserFederatedIdentityEntity extends UpdatableEntity.Impl impleme
@Override
public String getIdentityProvider() {
return this.metadata.getIdentityProvider();
if (isMetadataInitialized()) return this.metadata.getIdentityProvider();
return identityProvider;
}
@Override

View file

@ -90,13 +90,18 @@ public class JpaClientSessionEntity extends AbstractAuthenticatedClientSessionEn
this.metadata = new JpaClientSessionMetadata(cloner);
}
public boolean isMetadataInitialized() {
return metadata != null;
}
public void setParent(JpaUserSessionEntity root) {
this.root = root;
}
@Override
public Integer getEntityVersion() {
return metadata.getEntityVersion();
if (isMetadataInitialized()) return metadata.getEntityVersion();
return entityVersion;
}
@Override
@ -137,7 +142,8 @@ public class JpaClientSessionEntity extends AbstractAuthenticatedClientSessionEn
@Override
public String getClientId() {
return metadata.getClientId();
if (isMetadataInitialized()) return metadata.getClientId();
return clientId;
}
@Override

View file

@ -67,4 +67,13 @@ limitations under the License.
</ext:createJsonIndex>
</changeSet>
<changeSet author="keycloak" id="auth-sessions-14161">
<ext:addGeneratedColumn tableName="kc_auth_session">
<ext:column name="entityversion" type="INTEGER" jsonColumn="metadata" jsonProperty="entityVersion"/>
</ext:addGeneratedColumn>
<createIndex tableName="kc_auth_session" indexName="auth_session_entityVersion">
<column name="entityversion"/>
</createIndex>
</changeSet>
</databaseChangeLog>

View file

@ -22,7 +22,6 @@ limitations under the License.
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<!-- format of id of changeSet: authz-permission-${org.keycloak.models.map.storage.jpa.Constants.CURRENT_SCHEMA_VERSION_AUTHZ_PERMISSION} -->
<changeSet author="keycloak" id="authz-permission-1">
<createTable tableName="kc_authz_permission">

View file

@ -22,7 +22,6 @@ limitations under the License.
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<!-- format of id of changeSet: authz-resource-server-${org.keycloak.models.map.storage.jpa.Constants.CURRENT_SCHEMA_VERSION_AUTHZ_RESOURCE_SERVER} -->
<changeSet author="keycloak" id="authz-resource-server-1">
<createTable tableName="kc_authz_resource_server">

View file

@ -22,7 +22,6 @@ limitations under the License.
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<!-- format of id of changeSet: authz-scope-${org.keycloak.models.map.storage.jpa.Constants.CURRENT_SCHEMA_VERSION_AUTHZ_SCOPE} -->
<changeSet author="keycloak" id="authz-scope-1">
<createTable tableName="kc_authz_scope">

View file

@ -22,7 +22,6 @@ limitations under the License.
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<!-- format of id of changeSet: client-scopes-${org.keycloak.models.map.storage.jpa.Constants.CURRENT_SCHEMA_VERSION_CLIENT_SCOPE} -->
<changeSet author="keycloak" id="client-scopes-1">
<createTable tableName="kc_client_scope">

View file

@ -22,7 +22,6 @@ limitations under the License.
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<!-- format of id of changeSet: clients-${org.keycloak.models.map.storage.jpa.Constants.CURRENT_SCHEMA_VERSION_CLIENT} -->
<changeSet author="keycloak" id="clients-1">
<createTable tableName="kc_client">

View file

@ -22,7 +22,6 @@ limitations under the License.
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<!-- format of id of changeSet: groups-${org.keycloak.models.map.storage.jpa.Constants.CURRENT_SCHEMA_VERSION_GROUP} -->
<changeSet author="keycloak" id="groups-1">
<createTable tableName="kc_group">

View file

@ -18,6 +18,5 @@ limitations under the License.
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<!-- format of id of changelog file names: jpa-client-scopes-changelog-${org.keycloak.models.map.storage.jpa.Constants.CURRENT_SCHEMA_VERSION_CLIENT_SCOPE}.xml -->
<include file="META-INF/client-scopes/jpa-client-scopes-changelog-1.xml"/>
</databaseChangeLog>

View file

@ -18,6 +18,5 @@ limitations under the License.
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<!-- format of id of changelog file names: jpa-clients-changelog-${org.keycloak.models.map.storage.jpa.Constants.CURRENT_SCHEMA_VERSION_CLIENT}.xml -->
<include file="META-INF/clients/jpa-clients-changelog-1.xml"/>
</databaseChangeLog>

View file

@ -18,6 +18,5 @@ limitations under the License.
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<!-- format of id of changelog file names: jpa-groups-changelog-${org.keycloak.models.map.storage.jpa.Constants.CURRENT_SCHEMA_VERSION_GROUP.xml -->
<include file="META-INF/groups/jpa-groups-changelog-1.xml"/>
</databaseChangeLog>

View file

@ -18,6 +18,5 @@ limitations under the License.
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<!-- format of id of changelog file names: jpa-roles-changelog-${org.keycloak.models.map.storage.jpa.Constants.CURRENT_SCHEMA_VERSION_ROLE}.xml -->
<include file="META-INF/roles/jpa-roles-changelog-1.xml"/>
</databaseChangeLog>

View file

@ -18,6 +18,5 @@ limitations under the License.
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<!-- format of id of changelog file names: jpa-single-use-objects-changelog-${org.keycloak.models.map.storage.jpa.Constants.CURRENT_SCHEMA_VERSION_SINGLE_USE_OBJECT}.xml -->
<include file="META-INF/single-use-objects/jpa-single-use-objects-changelog-1.xml"/>
</databaseChangeLog>

View file

@ -18,6 +18,5 @@ limitations under the License.
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<!-- format of id of changelog file names: jpa-user-login-failures-changelog-${org.keycloak.models.map.storage.jpa.Constants.CURRENT_SCHEMA_VERSION_USER_LOGIN_FAILURE}.xml -->
<include file="META-INF/user-login-failures/jpa-user-login-failures-changelog-1.xml"/>
</databaseChangeLog>

View file

@ -101,4 +101,13 @@ limitations under the License.
<addUniqueConstraint tableName="kc_realm_attribute" columnNames="fk_root, name, value" deferrable="true" initiallyDeferred="true" />
</changeSet>
<changeSet author="keycloak" id="realms-14161">
<ext:addGeneratedColumn tableName="kc_component">
<ext:column name="entityversion" type="INTEGER" jsonColumn="metadata" jsonProperty="entityVersion"/>
</ext:addGeneratedColumn>
<createIndex tableName="kc_component" indexName="component_entityVersion">
<column name="entityversion"/>
</createIndex>
</changeSet>
</databaseChangeLog>

View file

@ -177,4 +177,23 @@ limitations under the License.
<addUniqueConstraint tableName="kc_user_role" columnNames="user_id, role_id" deferrable="true" initiallyDeferred="true" />
<addUniqueConstraint tableName="kc_user_attribute" columnNames="fk_root, name, value" deferrable="true" initiallyDeferred="true" />
</changeSet>
<changeSet author="keycloak" id="users-14161">
<ext:addGeneratedColumn tableName="kc_user_consent">
<ext:column name="entityversion" type="INTEGER" jsonColumn="metadata" jsonProperty="entityVersion"/>
</ext:addGeneratedColumn>
<createIndex tableName="kc_user_consent" indexName="user_consent_entityVersion">
<column name="entityversion"/>
</createIndex>
<ext:addGeneratedColumn tableName="kc_user_federated_identity">
<ext:column name="entityversion" type="INTEGER" jsonColumn="metadata" jsonProperty="entityVersion"/>
</ext:addGeneratedColumn>
<createIndex tableName="kc_user_federated_identity" indexName="user_federated_identity_entityVersion">
<column name="entityversion"/>
</createIndex>
</changeSet>
</databaseChangeLog>