Search in sources :

Example 1 with CustomChangeException

use of liquibase.exception.CustomChangeException in project ORCID-Source by ORCID.

the class AdministrativeChangesOptionChangeTask method execute.

@Override
public void execute(Database database) throws CustomChangeException {
    LOGGER.info("Running...");
    final JdbcConnection conn = (JdbcConnection) database.getConnection();
    try (PreparedStatement selectStatement = conn.prepareStatement(SELECT_SQL);
        PreparedStatement updateStatement = conn.prepareStatement(UPDATE_SQL)) {
        boolean done = false;
        conn.setAutoCommit(false);
        while (!done) {
            LOGGER.info("Getting next batch...");
            done = true;
            ResultSet resultsSet = selectStatement.executeQuery();
            while (resultsSet.next()) {
                done = false;
                String orcid = resultsSet.getString(1);
                LOGGER.debug("Processing orcid: {}", orcid);
                updateStatement.setString(1, orcid);
                updateStatement.addBatch();
            }
            updateStatement.executeBatch();
            conn.commit();
        }
    } catch (DatabaseException | SQLException e) {
        throw new CustomChangeException("Problem populating administrative changes option", e);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) CustomChangeException(liquibase.exception.CustomChangeException) JdbcConnection(liquibase.database.jvm.JdbcConnection) PreparedStatement(java.sql.PreparedStatement) DatabaseException(liquibase.exception.DatabaseException)

Example 2 with CustomChangeException

use of liquibase.exception.CustomChangeException in project keycloak by keycloak.

the class AbstractUserFedToComponent method convertFedProviderToComponent.

protected void convertFedProviderToComponent(String providerId, String newMapperType) throws CustomChangeException {
    try {
        PreparedStatement statement = jdbcConnection.prepareStatement("select ID, REALM_ID, PRIORITY, DISPLAY_NAME, FULL_SYNC_PERIOD, CHANGED_SYNC_PERIOD, LAST_SYNC from " + getTableName("USER_FEDERATION_PROVIDER") + " WHERE PROVIDER_NAME=?");
        statement.setString(1, providerId);
        try {
            ResultSet resultSet = statement.executeQuery();
            try {
                while (resultSet.next()) {
                    int index = 1;
                    String id = resultSet.getString(index++);
                    String realmId = resultSet.getString(index++);
                    int priority = resultSet.getInt(index++);
                    String displayName = resultSet.getString(index++);
                    int fullSyncPeriod = resultSet.getInt(index++);
                    int changedSyncPeriod = resultSet.getInt(index++);
                    int lastSync = resultSet.getInt(index++);
                    InsertStatement insertComponent = new InsertStatement(null, null, database.correctObjectName("COMPONENT", Table.class)).addColumnValue("ID", id).addColumnValue("REALM_ID", realmId).addColumnValue("PARENT_ID", realmId).addColumnValue("NAME", displayName).addColumnValue("PROVIDER_ID", providerId).addColumnValue("PROVIDER_TYPE", UserStorageProvider.class.getName());
                    statements.add(insertComponent);
                    statements.add(componentConfigStatement(id, "priority", Integer.toString(priority)));
                    statements.add(componentConfigStatement(id, "fullSyncPeriod", Integer.toString(fullSyncPeriod)));
                    statements.add(componentConfigStatement(id, "changedSyncPeriod", Integer.toString(changedSyncPeriod)));
                    statements.add(componentConfigStatement(id, "lastSync", Integer.toString(lastSync)));
                    PreparedStatement configStatement = jdbcConnection.prepareStatement("select name, VALUE from " + getTableName("USER_FEDERATION_CONFIG") + " WHERE USER_FEDERATION_PROVIDER_ID=?");
                    configStatement.setString(1, id);
                    try {
                        ResultSet configSet = configStatement.executeQuery();
                        try {
                            while (configSet.next()) {
                                String name = configSet.getString(1);
                                String value = configSet.getString(2);
                                // logger.info("adding component config: " + name + ": " + value);
                                statements.add(componentConfigStatement(id, name, value));
                            }
                        } finally {
                            configSet.close();
                        }
                    } finally {
                        configStatement.close();
                    }
                    if (newMapperType != null) {
                        convertFedMapperToComponent(realmId, id, newMapperType);
                    }
                    DeleteStatement configDelete = new DeleteStatement(null, null, database.correctObjectName("USER_FEDERATION_CONFIG", Table.class));
                    configDelete.setWhere("USER_FEDERATION_PROVIDER_ID=?");
                    configDelete.addWhereParameters(id);
                    statements.add(configDelete);
                    DeleteStatement deleteStatement = new DeleteStatement(null, null, database.correctObjectName("USER_FEDERATION_PROVIDER", Table.class));
                    deleteStatement.setWhere("ID=?");
                    deleteStatement.addWhereParameters(id);
                    statements.add(deleteStatement);
                }
            } finally {
                resultSet.close();
            }
        } finally {
            statement.close();
        }
        confirmationMessage.append("Updated " + statements.size() + " records in USER_FEDERATION_PROVIDER table for " + providerId + " conversion to component model");
    } catch (Exception e) {
        throw new CustomChangeException(getTaskId() + ": Exception when updating data from previous version", e);
    }
}
Also used : Table(liquibase.structure.core.Table) UserStorageProvider(org.keycloak.storage.UserStorageProvider) ResultSet(java.sql.ResultSet) CustomChangeException(liquibase.exception.CustomChangeException) PreparedStatement(java.sql.PreparedStatement) DeleteStatement(liquibase.statement.core.DeleteStatement) InsertStatement(liquibase.statement.core.InsertStatement) CustomChangeException(liquibase.exception.CustomChangeException)

Example 3 with CustomChangeException

use of liquibase.exception.CustomChangeException in project keycloak by keycloak.

the class JpaUpdate13_0_0_MigrateDefaultRoles method isRoleNameAvailable.

private boolean isRoleNameAvailable(String realmId, String roleName) throws CustomChangeException {
    try (PreparedStatement statement = jdbcConnection.prepareStatement("SELECT ID FROM " + getTableName("KEYCLOAK_ROLE") + " WHERE REALM_ID=? AND NAME=?")) {
        statement.setString(1, realmId);
        statement.setString(2, roleName);
        try (ResultSet rs = statement.executeQuery()) {
            // name is available
            return !rs.next();
        }
    } catch (Exception e) {
        throw new CustomChangeException(getTaskId() + ": Exception when extracting data from previous version", e);
    }
}
Also used : ResultSet(java.sql.ResultSet) CustomChangeException(liquibase.exception.CustomChangeException) PreparedStatement(java.sql.PreparedStatement) CustomChangeException(liquibase.exception.CustomChangeException)

Example 4 with CustomChangeException

use of liquibase.exception.CustomChangeException in project keycloak by keycloak.

the class JpaUpdate1_2_0_Beta1 method generateStatementsImpl.

@Override
protected void generateStatementsImpl() throws CustomChangeException {
    realmTableName = database.correctObjectName("REALM", Table.class);
    try {
        convertSocialToIdFedRealms();
        convertSocialToIdFedUsers();
        addAccessCodeLoginTimeout();
        addNewAdminRoles();
        addDefaultProtocolMappers();
    } catch (Exception e) {
        throw new CustomChangeException(getTaskId() + ": Exception when updating data from previous version", e);
    }
}
Also used : Table(liquibase.structure.core.Table) CustomChangeException(liquibase.exception.CustomChangeException) DatabaseException(liquibase.exception.DatabaseException) CustomChangeException(liquibase.exception.CustomChangeException) SQLException(java.sql.SQLException)

Example 5 with CustomChangeException

use of liquibase.exception.CustomChangeException in project keycloak by keycloak.

the class JpaUpdate1_4_0_Final method generateStatementsImpl.

@Override
protected void generateStatementsImpl() throws CustomChangeException {
    String userAttributeTableName = database.correctObjectName("USER_ATTRIBUTE", Table.class);
    try {
        PreparedStatement statement = jdbcConnection.prepareStatement("select NAME, USER_ID from " + getTableName("USER_ATTRIBUTE"));
        try {
            ResultSet resultSet = statement.executeQuery();
            try {
                while (resultSet.next()) {
                    String name = resultSet.getString(1);
                    String userId = resultSet.getString(2);
                    UpdateStatement updateStatement = new UpdateStatement(null, null, userAttributeTableName).addNewColumnValue("ID", KeycloakModelUtils.generateId()).setWhereClause("NAME='" + name + "' AND USER_ID='" + userId + "'");
                    statements.add(updateStatement);
                }
            } finally {
                resultSet.close();
            }
        } finally {
            statement.close();
        }
        confirmationMessage.append("Updated " + statements.size() + " attributes in USER_ATTRIBUTE table");
    } catch (Exception e) {
        throw new CustomChangeException(getTaskId() + ": Exception when updating data from previous version", e);
    }
}
Also used : UpdateStatement(liquibase.statement.core.UpdateStatement) ResultSet(java.sql.ResultSet) CustomChangeException(liquibase.exception.CustomChangeException) PreparedStatement(java.sql.PreparedStatement) CustomChangeException(liquibase.exception.CustomChangeException)

Aggregations

CustomChangeException (liquibase.exception.CustomChangeException)45 PreparedStatement (java.sql.PreparedStatement)32 DatabaseException (liquibase.exception.DatabaseException)26 ResultSet (java.sql.ResultSet)25 SQLException (java.sql.SQLException)25 JdbcConnection (liquibase.database.jvm.JdbcConnection)20 SetupException (liquibase.exception.SetupException)15 Statement (java.sql.Statement)8 ArrayList (java.util.ArrayList)6 InsertStatement (liquibase.statement.core.InsertStatement)6 Table (liquibase.structure.core.Table)6 Date (java.sql.Date)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 BatchUpdateException (java.sql.BatchUpdateException)4 Connection (java.sql.Connection)4 HashSet (java.util.HashSet)4 List (java.util.List)4 UpdateStatement (liquibase.statement.core.UpdateStatement)4 SqlStatement (liquibase.statement.SqlStatement)3