Search in sources :

Example 11 with InsertStatement

use of liquibase.statement.core.InsertStatement in project keycloak by keycloak.

the class JpaUpdate1_2_0_Beta1 method convertSocialToIdFedUsers.

protected void convertSocialToIdFedUsers() throws SQLException, DatabaseException {
    String federatedIdentityTableName = database.correctObjectName("FEDERATED_IDENTITY", Table.class);
    PreparedStatement statement = jdbcConnection.prepareStatement("select REALM_ID, USER_ID, SOCIAL_PROVIDER, SOCIAL_USER_ID, SOCIAL_USERNAME from " + getTableName("USER_SOCIAL_LINK"));
    try {
        ResultSet resultSet = statement.executeQuery();
        try {
            int count = 0;
            while (resultSet.next()) {
                InsertStatement insert = new InsertStatement(null, null, federatedIdentityTableName).addColumnValue("REALM_ID", resultSet.getString("REALM_ID")).addColumnValue("USER_ID", resultSet.getString("USER_ID")).addColumnValue("IDENTITY_PROVIDER", resultSet.getString("SOCIAL_PROVIDER")).addColumnValue("FEDERATED_USER_ID", resultSet.getString("SOCIAL_USER_ID")).addColumnValue("FEDERATED_USERNAME", resultSet.getString("SOCIAL_USERNAME"));
                count++;
                statements.add(insert);
            }
            confirmationMessage.append("Updating " + count + " social links to federated identities. ");
        } finally {
            resultSet.close();
        }
    } finally {
        statement.close();
    }
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) InsertStatement(liquibase.statement.core.InsertStatement)

Example 12 with InsertStatement

use of liquibase.statement.core.InsertStatement in project keycloak by keycloak.

the class JpaUpdate1_2_0_CR1 method generateStatementsImpl.

@Override
protected void generateStatementsImpl() throws CustomChangeException {
    String realmClientTableName = database.correctObjectName("REALM_CLIENT", Table.class);
    try {
        String trueValue = DataTypeFactory.getInstance().getTrueBooleanValue(database);
        PreparedStatement statement = jdbcConnection.prepareStatement("select CLIENT.REALM_ID, CLIENT.ID CLIENT_ID from " + getTableName("CLIENT") + " CLIENT where CLIENT.CONSENT_REQUIRED = " + trueValue);
        try {
            ResultSet resultSet = statement.executeQuery();
            try {
                while (resultSet.next()) {
                    String realmId = resultSet.getString("REALM_ID");
                    String oauthClientId = resultSet.getString("CLIENT_ID");
                    InsertStatement realmClientInsert = new InsertStatement(null, null, realmClientTableName).addColumnValue("REALM_ID", realmId).addColumnValue("CLIENT_ID", oauthClientId);
                    statements.add(realmClientInsert);
                }
            } finally {
                resultSet.close();
            }
        } finally {
            statement.close();
        }
        confirmationMessage.append("Inserted " + statements.size() + " OAuth Clients to REALM_CLIENT table");
    } catch (Exception e) {
        throw new CustomChangeException(getTaskId() + ": Exception when updating data from previous version", e);
    }
}
Also used : ResultSet(java.sql.ResultSet) CustomChangeException(liquibase.exception.CustomChangeException) PreparedStatement(java.sql.PreparedStatement) InsertStatement(liquibase.statement.core.InsertStatement) CustomChangeException(liquibase.exception.CustomChangeException)

Example 13 with InsertStatement

use of liquibase.statement.core.InsertStatement in project keycloak by keycloak.

the class JpaUpdate14_0_0_MigrateSamlArtifactAttribute method generateStatementsImpl.

@Override
protected void generateStatementsImpl() throws CustomChangeException {
    extractClientsData("SELECT C.ID, C.CLIENT_ID FROM " + getTableName("CLIENT") + " C " + "LEFT JOIN " + getTableName("CLIENT_ATTRIBUTES") + " CA " + "ON C.ID = CA.CLIENT_ID AND CA.NAME='" + SAML_ARTIFACT_BINDING_IDENTIFIER + "' " + "WHERE C.PROTOCOL='saml' AND CA.NAME IS NULL");
    for (Map.Entry<String, String> clientPair : clientIds.entrySet()) {
        String id = clientPair.getKey();
        String clientId = clientPair.getValue();
        String samlIdentifier = computeArtifactBindingIdentifierString(clientId);
        statements.add(new InsertStatement(null, null, database.correctObjectName("CLIENT_ATTRIBUTES", Table.class)).addColumnValue("CLIENT_ID", id).addColumnValue("NAME", SAML_ARTIFACT_BINDING_IDENTIFIER).addColumnValue("VALUE", samlIdentifier));
    }
}
Also used : Table(liquibase.structure.core.Table) ArtifactBindingUtils.computeArtifactBindingIdentifierString(org.keycloak.protocol.saml.util.ArtifactBindingUtils.computeArtifactBindingIdentifierString) Map(java.util.Map) HashMap(java.util.HashMap) InsertStatement(liquibase.statement.core.InsertStatement)

Example 14 with InsertStatement

use of liquibase.statement.core.InsertStatement in project keycloak by keycloak.

the class CustomInsertLockRecordGenerator method generateSql.

@Override
public Sql[] generateSql(InitializeDatabaseChangeLogLockTableStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    // get the IDs that are already in the database if migration
    Set<Integer> currentIds = new HashSet<>();
    if (statement instanceof CustomInitializeDatabaseChangeLogLockTableStatement) {
        currentIds = ((CustomInitializeDatabaseChangeLogLockTableStatement) statement).getCurrentIds();
    }
    // generate all the IDs that are currently missing in the lock table
    List<Sql> result = new ArrayList<>();
    for (DBLockProvider.Namespace lock : DBLockProvider.Namespace.values()) {
        if (!currentIds.contains(lock.getId())) {
            InsertStatement insertStatement = new InsertStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogLockTableName()).addColumnValue("ID", lock.getId()).addColumnValue("LOCKED", Boolean.FALSE);
            result.addAll(Arrays.asList(SqlGeneratorFactory.getInstance().generateSql(insertStatement, database)));
        }
    }
    return result.toArray(new Sql[result.size()]);
}
Also used : ArrayList(java.util.ArrayList) DBLockProvider(org.keycloak.models.dblock.DBLockProvider) InsertStatement(liquibase.statement.core.InsertStatement) HashSet(java.util.HashSet) Sql(liquibase.sql.Sql)

Example 15 with InsertStatement

use of liquibase.statement.core.InsertStatement in project openmrs-core by openmrs.

the class InsertWithUuidDataChangeTest method shouldNotModifyUuidValueIfColumnExists.

@Test
public void shouldNotModifyUuidValueIfColumnExists() {
    InsertWithUuidDataChange dataChange = new InsertWithUuidDataChange();
    String expected = "expected_value_for_UUID";
    ColumnConfig uuid = new ColumnConfig(new Column("uuid"));
    uuid.setValue(expected);
    dataChange.addColumn(uuid);
    SqlStatement[] statements = dataChange.generateStatements(new MySQLDatabase());
    InsertStatement insertStatement = (InsertStatement) statements[0];
    Object actual = insertStatement.getColumnValue("uuid");
    assertEquals(expected, actual);
}
Also used : SqlStatement(liquibase.statement.SqlStatement) ColumnConfig(liquibase.change.ColumnConfig) Column(liquibase.structure.core.Column) MySQLDatabase(liquibase.database.core.MySQLDatabase) InsertStatement(liquibase.statement.core.InsertStatement) Test(org.junit.jupiter.api.Test)

Aggregations

InsertStatement (liquibase.statement.core.InsertStatement)26 PreparedStatement (java.sql.PreparedStatement)9 ResultSet (java.sql.ResultSet)9 Sql (liquibase.sql.Sql)7 CustomChangeException (liquibase.exception.CustomChangeException)6 SqlStatement (liquibase.statement.SqlStatement)6 Table (liquibase.structure.core.Table)6 ColumnConfig (liquibase.change.ColumnConfig)4 SequenceNextValueFunction (liquibase.statement.SequenceNextValueFunction)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 MySQLDatabase (liquibase.database.core.MySQLDatabase)3 PostgresDatabase (liquibase.database.core.PostgresDatabase)3 DeleteStatement (liquibase.statement.core.DeleteStatement)3 Column (liquibase.structure.core.Column)3 HashMap (java.util.HashMap)2 ObjectQuotingStrategy (liquibase.database.ObjectQuotingStrategy)2 SequenceCurrentValueFunction (liquibase.statement.SequenceCurrentValueFunction)2 Test (org.junit.jupiter.api.Test)2 CsvMalformedLineException (com.opencsv.exceptions.CsvMalformedLineException)1