private void generateChangeLog(final Database database, final CatalogAndSchema catalogAndSchema, final DiffToChangeLog changeLogWriter, PrintStream outputStream, final Set<Class<? extends DatabaseObject>> compareTypes) throws DatabaseException, IOException, ParserConfigurationException { @SuppressWarnings("unchecked") final SnapshotControl snapshotControl = new SnapshotControl(database, compareTypes.toArray(new Class[compareTypes.size()])); final CompareControl compareControl = new CompareControl(new CompareControl.SchemaComparison[]{ new CompareControl.SchemaComparison(catalogAndSchema, catalogAndSchema)}, compareTypes); final CatalogAndSchema[] compareControlSchemas = compareControl .getSchemas(CompareControl.DatabaseRole.REFERENCE); try { final DatabaseSnapshot referenceSnapshot = SnapshotGeneratorFactory.getInstance() .createSnapshot(compareControlSchemas, database, snapshotControl); final DatabaseSnapshot comparisonSnapshot = SnapshotGeneratorFactory.getInstance() .createSnapshot(compareControlSchemas, null, snapshotControl); final DiffResult diffResult = DiffGeneratorFactory.getInstance() .compare(referenceSnapshot, comparisonSnapshot, compareControl); changeLogWriter.setDiffResult(diffResult); changeLogWriter.print(outputStream); } catch (InvalidExampleException e) { throw new UnexpectedLiquibaseException(e); } }
protected boolean isApplicable() throws CustomChangeException { try { String correctedTableName = database.correctObjectName("REALM", Table.class); if (SnapshotGeneratorFactory.getInstance().has(new Table().setName(correctedTableName), database)) { ResultSet resultSet = connection.createStatement().executeQuery("SELECT ID FROM " + getTableName(correctedTableName)); try { return (resultSet.next()); } finally { resultSet.close(); } } else { return false; } } catch (Exception e) { throw new CustomChangeException("Failed to check database availability", e); } }
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);
protected boolean isApplicable() throws CustomChangeException { try { String correctedTableName = database.correctObjectName("REALM", Table.class); if (SnapshotGeneratorFactory.getInstance().has(new Table().setName(correctedTableName), database)) { ResultSet resultSet = connection.createStatement().executeQuery("SELECT ID FROM " + getTableName(correctedTableName)); try { return (resultSet.next()); } finally { resultSet.close(); } } else { return false; } } catch (Exception e) { throw new CustomChangeException("Failed to check database availability", e); } }
private void generateChangeLog(final Database database, final CatalogAndSchema catalogAndSchema, final DiffToChangeLog changeLogWriter, PrintStream outputStream, final Set<Class<? extends DatabaseObject>> compareTypes) throws DatabaseException, IOException, ParserConfigurationException { @SuppressWarnings({"unchecked", "rawtypes"}) final SnapshotControl snapshotControl = new SnapshotControl(database, compareTypes.toArray(new Class[compareTypes.size()])); final CompareControl compareControl = new CompareControl(new CompareControl.SchemaComparison[]{ new CompareControl.SchemaComparison(catalogAndSchema, catalogAndSchema)}, compareTypes); final CatalogAndSchema[] compareControlSchemas = compareControl .getSchemas(CompareControl.DatabaseRole.REFERENCE); try { final DatabaseSnapshot referenceSnapshot = SnapshotGeneratorFactory.getInstance() .createSnapshot(compareControlSchemas, database, snapshotControl); final DatabaseSnapshot comparisonSnapshot = SnapshotGeneratorFactory.getInstance() .createSnapshot(compareControlSchemas, null, snapshotControl); final DiffResult diffResult = DiffGeneratorFactory.getInstance() .compare(referenceSnapshot, comparisonSnapshot, compareControl); changeLogWriter.setDiffResult(diffResult); changeLogWriter.print(outputStream); } catch (InvalidExampleException e) { throw new UnexpectedLiquibaseException(e); } }
@Override public void check(Database database) throws CustomPreconditionFailedException, CustomPreconditionErrorException { Column column = new Column(this.columnNames); UniqueConstraint uniqueConstraint = new UniqueConstraint(this.constraintName, null, null, this.tableName, column); boolean markFailed = false; try { if (!SnapshotGeneratorFactory.getInstance().has(uniqueConstraint, database)) { markFailed = true; } } catch (Exception e) { throw new CustomPreconditionErrorException("custom precondition check errored", e); } if (markFailed) { throw new CustomPreconditionFailedException(this.constraintName + " doesn't exist"); } } }
public static String createDatabaseSnapshotAsString(DataSource ds) throws LiquibaseException, SQLException { try (Connection conn = ds.getConnection()) { Database database = DatabaseFactory.getInstance() .findCorrectDatabaseImplementation(new JdbcConnection(conn)); DatabaseSnapshot currentSnapshot = SnapshotGeneratorFactory .getInstance().createSnapshot(database.getDefaultSchema(), database, new SnapshotControl(database)); return SnapshotSerializerFactory.getInstance().getSerializer("json") .serialize(currentSnapshot, true); } } }
@Override public SqlStatement[] generateStatements(Database database) throws CustomChangeException { try { StringBuilder sb = new StringBuilder(); sb.append("Generated codeSecret for realms: "); Connection connection = ((JdbcConnection) (database.getConnection())).getWrappedConnection(); ArrayList<SqlStatement> statements = new ArrayList<SqlStatement>(); String correctedTableName = database.correctObjectName("REALM", Table.class); if (SnapshotGeneratorFactory.getInstance().has(new Table().setName(correctedTableName), database)) { ResultSet resultSet = connection.createStatement().executeQuery("SELECT ID FROM " + LiquibaseJpaUpdaterProvider.getTable(correctedTableName, database.getDefaultSchemaName()) + " WHERE CODE_SECRET IS NULL"); while (resultSet.next()) { String id = resultSet.getString(1); UpdateStatement statement = new UpdateStatement(null, null, correctedTableName) .addNewColumnValue("CODE_SECRET", KeycloakModelUtils.generateCodeSecret()) .setWhereClause("ID='" + id + "'"); statements.add(statement); if (!resultSet.isFirst()) { sb.append(", "); } sb.append(id); } if (!statements.isEmpty()) { confirmationMessage = sb.toString(); } } return statements.toArray(new SqlStatement[statements.size()]); } catch (Exception e) { throw new CustomChangeException("Failed to add realm code secret", e); } }
List<Schema> schemas = connectionProvider.getDatabase().getSchemas(); SnapshotGeneratorFactory dsgf = SnapshotGeneratorFactory.getInstance(); dsgf.createSnapshot(new CatalogAndSchema(catalog, schemaName), liquibaseDatabase, snapshotControl);
String correctedSchemaName = database.escapeObjectName(database.getDefaultSchemaName(), Schema.class); if (SnapshotGeneratorFactory.getInstance().has(new Table().setName(correctedTableName), database)) { ResultSet resultSet = connection.createStatement().executeQuery("SELECT ID FROM " + LiquibaseJpaUpdaterProvider.getTable(correctedTableName, correctedSchemaName) + " WHERE CODE_SECRET IS NULL"); while (resultSet.next()) {
List<Schema> schemas = connectionProvider.getDatabase().getSchemas(); SnapshotGeneratorFactory dsgf = SnapshotGeneratorFactory.getInstance(); dsgf.createSnapshot(new CatalogAndSchema(catalog, schemaName), liquibaseDatabase, snapshotControl);