KEYCLOAK-5461 Upgrade to Liquibase 3.5.5

This commit is contained in:
Martin Kanis authored and Hynek Mlnařík committed 2018-06-22 13:20:10 +02:00
1 parent 30a066db41
commit 998227ac53
12 files changed
+491 -27

No files matched your search

@@ -18,10 +18,28 @@
package org.keycloak.connections.jpa.updater.liquibase;
import liquibase.Contexts;
import liquibase.LabelExpression;
import liquibase.Liquibase;
import liquibase.changelog.ChangeLogHistoryService;
import liquibase.changelog.ChangeLogHistoryServiceFactory;
import liquibase.changelog.ChangeSet;
import liquibase.changelog.RanChangeSet;
import liquibase.database.Database;
import liquibase.exception.DatabaseException;
import liquibase.exception.LiquibaseException;
import liquibase.executor.Executor;
import liquibase.executor.ExecutorService;
import liquibase.executor.LoggingExecutor;
import liquibase.snapshot.SnapshotControl;
import liquibase.snapshot.SnapshotGeneratorFactory;
import liquibase.statement.SqlStatement;
import liquibase.statement.core.AddColumnStatement;
import liquibase.statement.core.CreateDatabaseChangeLogTableStatement;
import liquibase.statement.core.SetNullableStatement;
import liquibase.statement.core.UpdateStatement;
import liquibase.structure.core.Column;
import liquibase.structure.core.Table;
import liquibase.util.StreamUtil;
import org.jboss.logging.Logger;
import org.keycloak.common.util.reflections.Reflections;
import org.keycloak.connections.jpa.entityprovider.JpaEntityProvider;
@@ -36,16 +54,10 @@ import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import liquibase.LabelExpression;
import liquibase.database.Database;
import liquibase.exception.DatabaseException;
import liquibase.executor.Executor;
import liquibase.executor.ExecutorService;
import liquibase.executor.LoggingExecutor;
import liquibase.statement.core.CreateDatabaseChangeLogTableStatement;
import liquibase.util.StreamUtil;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
@@ -56,6 +68,8 @@ public class LiquibaseJpaUpdaterProvider implements JpaUpdaterProvider {
public static final String CHANGELOG = "META-INF/jpa-changelog-master.xml";
public static final String DEPLOYMENT_ID_COLUMN = "DEPLOYMENT_ID";
private final KeycloakSession session;
public LiquibaseJpaUpdaterProvider(KeycloakSession session) {
@@ -85,7 +99,7 @@ public class LiquibaseJpaUpdaterProvider implements JpaUpdaterProvider {
if (file != null) {
exportWriter = new FileWriter(file);
}
updateChangeSet(liquibase, liquibase.getChangeLogFile(), exportWriter);
updateChangeSet(liquibase, connection, exportWriter);
// Run update for each custom JpaEntityProvider
Set<JpaEntityProvider> jpaProviders = session.getAllProviders(JpaEntityProvider.class);
@@ -95,10 +109,10 @@ public class LiquibaseJpaUpdaterProvider implements JpaUpdaterProvider {
String factoryId = jpaProvider.getFactoryId();
String changelogTableName = JpaUtils.getCustomChangelogTableName(factoryId);
liquibase = getLiquibaseForCustomProviderUpdate(connection, defaultSchema, customChangelog, jpaProvider.getClass().getClassLoader(), changelogTableName);
updateChangeSet(liquibase, liquibase.getChangeLogFile(), exportWriter);
updateChangeSet(liquibase, connection, exportWriter);
}
}
} catch (LiquibaseException | IOException e) {
} catch (LiquibaseException | IOException | SQLException e) {
throw new RuntimeException("Failed to update database", e);
} finally {
ThreadLocalSessionContext.removeCurrentSession();
@@ -112,7 +126,40 @@ public class LiquibaseJpaUpdaterProvider implements JpaUpdaterProvider {
}
}
protected void updateChangeSet(Liquibase liquibase, String changelog, Writer exportWriter) throws LiquibaseException, IOException {
protected void updateChangeSet(Liquibase liquibase, Connection connection, Writer exportWriter) throws LiquibaseException, SQLException {
String changelog = liquibase.getChangeLogFile();
Database database = liquibase.getDatabase();
Table changelogTable = SnapshotGeneratorFactory.getInstance().getDatabaseChangeLogTable(new SnapshotControl(database, false, Table.class, Column.class), database);
if (changelogTable != null) {
boolean hasDeploymentIdColumn = changelogTable.getColumn(DEPLOYMENT_ID_COLUMN) != null;
// create DEPLOYMENT_ID column if it doesn't exist
if (!hasDeploymentIdColumn) {
ChangeLogHistoryService changelogHistoryService = ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database);
changelogHistoryService.generateDeploymentId();
String deploymentId = changelogHistoryService.getDeploymentId();
logger.debugv("Adding missing column {0}={1} to {2} table", DEPLOYMENT_ID_COLUMN, deploymentId,changelogTable.getName());
List<SqlStatement> statementsToExecute = new ArrayList<>();
statementsToExecute.add(new AddColumnStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(),
changelogTable.getName(), DEPLOYMENT_ID_COLUMN, "VARCHAR(10)", null));
statementsToExecute.add(new UpdateStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), changelogTable.getName())
.addNewColumnValue(DEPLOYMENT_ID_COLUMN, deploymentId));
statementsToExecute.add(new SetNullableStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(),
changelogTable.getName(), DEPLOYMENT_ID_COLUMN, "VARCHAR(10)", false));
ExecutorService executorService = ExecutorService.getInstance();
Executor executor = executorService.getExecutor(liquibase.getDatabase());
for (SqlStatement sql : statementsToExecute) {
executor.execute(sql);
database.commit();
}
}
}
List<ChangeSet> changeSets = getLiquibaseUnrunChangeSets(liquibase);
if (!changeSets.isEmpty()) {
List<RanChangeSet> ranChangeSets = liquibase.getDatabase().getRanChangeSetList();
@@ -240,6 +240,9 @@ public class DefaultLiquibaseConnectionProvider implements LiquibaseConnectionPr
public int getPriority() {
return 0;
}
@Override
public void closeLogFile() {}
};
@Override
@@ -17,9 +17,13 @@
-->
<databaseChangeLog logicalFilePath="META-INF/db2-jpa-changelog-1.0.0.Final.xml" 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.2.xsd">
<changeSet author="sthorger@redhat.com" id="1.0.0.Final">
<changeSet author="sthorger@redhat.com" id="1.0.0.Final-KEYCLOAK-5461">
<preConditions onSqlOutput="TEST" onFail="MARK_RAN">
<dbms type="db2" />
<not>
<changeSetExecuted id="1.0.0.Final" author="sthorger@redhat.com" changeLogFile="META-INF/db2-jpa-changelog-1.0.0.Final.xml"/>
</not>
</preConditions>
<createTable tableName="APPLICATION_DEFAULT_ROLES">
@@ -100,7 +104,7 @@
</column>
<column name="DEVICE" type="VARCHAR(255)"/>
<column name="HASH_ITERATIONS" type="INT"/>
<column name="SALT" type="BLOB(16)"/>
<column name="SALT" type="TINYBLOB(16)"/>
<column name="TYPE" type="VARCHAR(255)"/>
<column name="VALUE" type="VARCHAR(255)"/>
<column name="USER_ID" type="VARCHAR(36)"/>
@@ -17,8 +17,11 @@
-->
<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.2.xsd">
<changeSet author="sthorger@redhat.com" id="1.0.0.Final">
<preConditions onSqlOutput="TEST" onFail="MARK_RAN">
<changeSet author="sthorger@redhat.com" id="1.0.0.Final-KEYCLOAK-5461">
<preConditions onSqlOutput="TEST" onFail="MARK_RAN">
<not>
<changeSetExecuted id="1.0.0.Final" author="sthorger@redhat.com" changeLogFile="META-INF/jpa-changelog-1.0.0.Final.xml"/>
</not>
<not>
<dbms type="db2" />
</not>
@@ -98,7 +101,7 @@
</column>
<column name="DEVICE" type="VARCHAR(255)"/>
<column name="HASH_ITERATIONS" type="INT"/>
<column name="SALT" type="BLOB(16)"/>
<column name="SALT" type="TINYBLOB(16)"/>
<column name="TYPE" type="VARCHAR(255)"/>
<column name="VALUE" type="VARCHAR(255)"/>
<column name="USER_ID" type="VARCHAR(36)"/>
@@ -17,7 +17,12 @@
-->
<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">
<changeSet author="bburke@redhat.com" id="2.1.0">
<changeSet author="bburke@redhat.com" id="2.1.0-KEYCLOAK-5461">
<preConditions onSqlOutput="TEST" onFail="MARK_RAN">
<not>
<changeSetExecuted id="2.1.0" author="bburke@redhat.com" changeLogFile="META-INF/jpa-changelog-2.1.0.xml"/>
</not>
</preConditions>
<createTable tableName="BROKER_LINK">
<column name="IDENTITY_PROVIDER" type="VARCHAR(255)">
@@ -88,7 +93,7 @@
</column>
<column name="DEVICE" type="VARCHAR(255)"/>
<column name="HASH_ITERATIONS" type="INT"/>
<column name="SALT" type="BLOB(16)"/>
<column name="SALT" type="TINYBLOB(16)"/>
<column name="TYPE" type="VARCHAR(255)"/>
<column name="VALUE" type="VARCHAR(255)"/>
<column name="CREATED_DATE" type="BIGINT"/>
@@ -196,4 +201,4 @@
</changeSet>
</databaseChangeLog>
</databaseChangeLog>