Add CockroachDB support in the new JPA storage

Closes #10039
This commit is contained in:
Stefan Guilhen 2022-02-10 16:25:25 -03:00 committed by Hynek Mlnařík
parent 26ac142b99
commit 442d9bae2e
5 changed files with 27 additions and 10 deletions

View file

@ -21,6 +21,7 @@ import java.util.Arrays;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import liquibase.database.Database; import liquibase.database.Database;
import liquibase.database.core.CockroachDatabase;
import liquibase.database.core.PostgresDatabase; import liquibase.database.core.PostgresDatabase;
import liquibase.exception.ValidationErrors; import liquibase.exception.ValidationErrors;
import liquibase.sql.Sql; import liquibase.sql.Sql;
@ -96,8 +97,14 @@ public class CreateJsonIndexGenerator extends AbstractSqlGenerator<CreateJsonInd
} }
protected void handleJsonIndex(final CreateJsonIndexStatement statement, final Database database, final StringBuilder builder) { protected void handleJsonIndex(final CreateJsonIndexStatement statement, final Database database, final StringBuilder builder) {
if (database instanceof PostgresDatabase) { if (database instanceof CockroachDatabase) {
builder.append(" USING gin ("); builder.append(" USING gin (");
builder.append(Arrays.stream(statement.getColumns()).map(JsonEnabledColumnConfig.class::cast)
.map(c -> "(" + c.getJsonColumn() + "->'" + c.getJsonProperty() + "')")
.collect(Collectors.joining(", ")))
.append(")");
}
else if (database instanceof PostgresDatabase) { builder.append(" USING gin (");
builder.append(Arrays.stream(statement.getColumns()).map(JsonEnabledColumnConfig.class::cast) builder.append(Arrays.stream(statement.getColumns()).map(JsonEnabledColumnConfig.class::cast)
.map(c -> "(" + c.getJsonColumn() + "->'" + c.getJsonProperty() + "') jsonb_path_ops") .map(c -> "(" + c.getJsonColumn() + "->'" + c.getJsonProperty() + "') jsonb_path_ops")
.collect(Collectors.joining(", "))) .collect(Collectors.joining(", ")))

View file

@ -49,15 +49,19 @@ public class MapJpaLiquibaseUpdaterProvider implements MapJpaUpdaterProvider {
@Override @Override
public void update(Class modelType, Connection connection, String defaultSchema) { public void update(Class modelType, Connection connection, String defaultSchema) {
update(modelType, connection, null, defaultSchema); synchronized (MapJpaLiquibaseUpdaterProvider.class) {
this.updateSynch(modelType, connection, null, defaultSchema);
}
} }
@Override @Override
public void export(Class modelType, Connection connection, String defaultSchema, File file) { public void export(Class modelType, Connection connection, String defaultSchema, File file) {
update(modelType, connection, file, defaultSchema); synchronized (MapJpaLiquibaseUpdaterProvider.class) {
this.updateSynch(modelType, connection, file, defaultSchema);
}
} }
private void update(Class modelType, Connection connection, File file, String defaultSchema) { protected void updateSynch(Class modelType, Connection connection, File file, String defaultSchema) {
logger.debug("Starting database update"); logger.debug("Starting database update");
// Need ThreadLocal as liquibase doesn't seem to have API to inject custom objects into tasks // Need ThreadLocal as liquibase doesn't seem to have API to inject custom objects into tasks
@ -113,6 +117,12 @@ public class MapJpaLiquibaseUpdaterProvider implements MapJpaUpdaterProvider {
@Override @Override
public Status validate(Class modelType, Connection connection, String defaultSchema) { public Status validate(Class modelType, Connection connection, String defaultSchema) {
synchronized (MapJpaLiquibaseUpdaterProvider.class) {
return this.validateSynch(modelType, connection, defaultSchema);
}
}
protected Status validateSynch(final Class modelType, final Connection connection, final String defaultSchema) {
logger.debug("Validating if database is updated"); logger.debug("Validating if database is updated");
ThreadLocalSessionContext.setCurrentSession(session); ThreadLocalSessionContext.setCurrentSession(session);

View file

@ -22,7 +22,7 @@ limitations under the License.
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd 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"> 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.SUPPORTED_VERSION_CLIENT_SCOPE} --> <!-- 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"> <changeSet author="keycloak" id="client-scopes-1">
<createTable tableName="kc_client_scope"> <createTable tableName="kc_client_scope">
@ -64,7 +64,7 @@ limitations under the License.
<column name="name"/> <column name="name"/>
<column name="VALUE(255)" valueComputed="VALUE(255)"/> <column name="VALUE(255)" valueComputed="VALUE(255)"/>
</createIndex> </createIndex>
<modifySql dbms="postgresql"> <modifySql dbms="postgresql,cockroachdb">
<replace replace="VALUE(255)" with="(value::varchar(250))"/> <replace replace="VALUE(255)" with="(value::varchar(250))"/>
</modifySql> </modifySql>
</changeSet> </changeSet>

View file

@ -22,7 +22,7 @@ limitations under the License.
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd 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"> 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.SUPPORTED_VERSION_CLIENT} --> <!-- format of id of changeSet: clients-${org.keycloak.models.map.storage.jpa.Constants.CURRENT_SCHEMA_VERSION_CLIENT} -->
<changeSet author="keycloak" id="clients-1"> <changeSet author="keycloak" id="clients-1">
<createTable tableName="kc_client"> <createTable tableName="kc_client">
@ -71,7 +71,7 @@ limitations under the License.
<column name="name"/> <column name="name"/>
<column name="VALUE(255)" valueComputed="VALUE(255)"/> <column name="VALUE(255)" valueComputed="VALUE(255)"/>
</createIndex> </createIndex>
<modifySql dbms="postgresql"> <modifySql dbms="postgresql,cockroachdb">
<replace replace="VALUE(255)" with="(value::varchar(250))"/> <replace replace="VALUE(255)" with="(value::varchar(250))"/>
</modifySql> </modifySql>
</changeSet> </changeSet>

View file

@ -22,7 +22,7 @@ limitations under the License.
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd 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"> http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<!-- format of id of changeSet: roles-${org.keycloak.models.map.storage.jpa.Constants.SUPPORTED_VERSION_ROLE} --> <!-- format of id of changeSet: roles-${org.keycloak.models.map.storage.jpa.Constants.CURRENT_SCHEMA_VERSION_ROLE} -->
<changeSet author="keycloak" id="roles-1"> <changeSet author="keycloak" id="roles-1">
<createTable tableName="kc_role"> <createTable tableName="kc_role">
@ -73,7 +73,7 @@ limitations under the License.
<column name="name"/> <column name="name"/>
<column name="VALUE(255)" valueComputed="VALUE(255)"/> <column name="VALUE(255)" valueComputed="VALUE(255)"/>
</createIndex> </createIndex>
<modifySql dbms="postgresql"> <modifySql dbms="postgresql,cockroachdb">
<replace replace="VALUE(255)" with="(value::varchar(250))"/> <replace replace="VALUE(255)" with="(value::varchar(250))"/>
</modifySql> </modifySql>
</changeSet> </changeSet>