liquibase.snapshot.SnapshotGeneratorFactory Java Examples
The following examples show how to use
liquibase.snapshot.SnapshotGeneratorFactory.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DropSpatialIndexGeneratorGeoDB.java From liquibase-spatial with Apache License 2.0 | 6 votes |
/** * Generates the SQL statement to drop the spatial index if it exists. * * @param statement * the drop spatial index statement. * @param database * the database. * @return the drop spatial index statement, if the index exists. */ public Sql[] generateSqlIfExists(final DropSpatialIndexStatement statement, final Database database) { final String catalogName = statement.getTableCatalogName(); final String schemaName = statement.getTableSchemaName(); final String tableName = statement.getTableName(); final SpatialIndexExistsPrecondition precondition = new SpatialIndexExistsPrecondition(); precondition.setCatalogName(catalogName); precondition.setSchemaName(schemaName); precondition.setTableName(tableName); final DatabaseObject example = precondition.getExample(database, tableName); try { // If a spatial index exists on the table, drop it. if (SnapshotGeneratorFactory.getInstance().has(example, database)) { return generateSql(statement, database, null); } } catch (final Exception e) { throw new UnexpectedLiquibaseException(e); } return new Sql[0]; }
Example #2
Source File: GeometryColumnsUtils.java From liquibase-spatial with Apache License 2.0 | 6 votes |
/** * Indicates if the <code>GEOMETRY_COLUMNS</code> table or view exists. * * @param database * the database to check. * @return <code>true</code> if the table or view exists. */ public static boolean geometryColumnsExists(final Database database) { String geometryColumnsName = database.correctObjectName( "geometry_columns", Table.class); DatabaseObject example = null; if (database instanceof DerbyDatabase || database instanceof H2Database) { final Table tableExample = new Table(); tableExample.setName(geometryColumnsName); tableExample.setSchema(database.getDefaultCatalogName(), database.getDefaultSchemaName()); example = tableExample; } else if (database instanceof PostgresDatabase) { final View viewExample = new View(); viewExample.setName(geometryColumnsName); viewExample.setSchema(database.getDefaultCatalogName(), "public"); example = viewExample; } try { return example != null && SnapshotGeneratorFactory.getInstance().has(example, database); } catch (final LiquibaseException e) { throw new UnexpectedLiquibaseException( "Failed to determine if the geometry_columns table or view exists", e); } }
Example #3
Source File: UniqueConstraintExistsCheck.java From aerogear-unifiedpush-server with Apache License 2.0 | 6 votes |
@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"); } }
Example #4
Source File: HiveStandardChangeLogHistoryService.java From liquibase-impala with Apache License 2.0 | 5 votes |
private boolean hasDatabaseChangeLogTable() throws DatabaseException { if (hasDatabaseChangeLogTable == null) { try { hasDatabaseChangeLogTable = SnapshotGeneratorFactory.getInstance().hasDatabaseChangeLogTable(getDatabase()); } catch (LiquibaseException e) { throw new UnexpectedLiquibaseException(e); } } return hasDatabaseChangeLogTable; }
Example #5
Source File: HiveStandardChangeLogHistoryService.java From liquibase-impala with Apache License 2.0 | 5 votes |
@Override public void destroy() throws DatabaseException { Database database = getDatabase(); try { if (SnapshotGeneratorFactory.getInstance().has(new Table().setName(database.getDatabaseChangeLogTableName()).setSchema(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName()), database)) { ExecutorService.getInstance().getExecutor(database).execute(new DropTableStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName(), false)); } reset(); } catch (InvalidExampleException e) { throw new UnexpectedLiquibaseException(e); } }
Example #6
Source File: DropSpatialIndexGeneratorOracle.java From liquibase-spatial with Apache License 2.0 | 5 votes |
@Override public Sql[] generateSql(final DropSpatialIndexStatement statement, final Database database, final SqlGeneratorChain sqlGeneratorChain) { final String indexName = statement.getIndexName(); final Index example = new Index().setName(indexName); if (statement.getTableName() != null) { example.setTable((Table) new Table().setName(statement.getTableName()).setSchema( statement.getTableCatalogName(), statement.getTableSchemaName())); } Index index; try { index = SnapshotGeneratorFactory.getInstance().createSnapshot(example, database); } catch (final Exception e) { throw new UnexpectedLiquibaseException("Failed to create a snapshot of '" + indexName + "'", e); } final String tableName = index.getTable().getName(); final Column column = index.getColumns().get(0); final StringBuilder sql = new StringBuilder(); sql.append("DELETE FROM user_sdo_geom_metadata "); sql.append("WHERE table_name = '").append(database.correctObjectName(tableName, Table.class)); sql.append("' AND column_name = '").append( database.correctObjectName(column.getName(), Column.class)); sql.append("'"); final UnparsedSql deleteMetadata = new UnparsedSql(sql.toString(), new View().setName("user_sdo_geom_metadata")); return new Sql[] { deleteMetadata }; }
Example #7
Source File: CustomKeycloakTask.java From keycloak with Apache License 2.0 | 5 votes |
protected boolean isApplicable() throws CustomChangeException { try { String correctedTableName = database.correctObjectName("REALM", Table.class); if (SnapshotGeneratorFactory.getInstance().has(new Table().setName(correctedTableName), database)) { try (Statement st = connection.createStatement(); ResultSet resultSet = st.executeQuery("SELECT ID FROM " + getTableName(correctedTableName))) { return (resultSet.next()); } } else { return false; } } catch (Exception e) { throw new CustomChangeException("Failed to check database availability", e); } }
Example #8
Source File: AddRealmCodeSecret.java From keycloak with Apache License 2.0 | 5 votes |
@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); String correctedSchemaName = database.escapeObjectName(database.getDefaultSchemaName(), Schema.class); if (SnapshotGeneratorFactory.getInstance().has(new Table().setName(correctedTableName), database)) { try (Statement st = connection.createStatement(); ResultSet resultSet = st.executeQuery("SELECT ID FROM " + LiquibaseJpaUpdaterProvider.getTable(correctedTableName, correctedSchemaName) + " 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=?").addWhereParameters(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); } }
Example #9
Source File: AbstractJdbcDatabase.java From jweb-cms with GNU Affero General Public License v3.0 | 4 votes |
@Override public void dropDatabaseObjects(final CatalogAndSchema schemaToDrop) throws LiquibaseException { ObjectQuotingStrategy currentStrategy = this.getObjectQuotingStrategy(); this.setObjectQuotingStrategy(ObjectQuotingStrategy.QUOTE_ALL_OBJECTS); try { DatabaseSnapshot snapshot; try { final SnapshotControl snapshotControl = new SnapshotControl(this); final Set<Class<? extends DatabaseObject>> typesToInclude = snapshotControl.getTypesToInclude(); //We do not need to remove indexes and primary/unique keys explicitly. They should be removed //as part of tables. typesToInclude.remove(Index.class); typesToInclude.remove(PrimaryKey.class); typesToInclude.remove(UniqueConstraint.class); if (supportsForeignKeyDisable()) { //We do not remove ForeignKey because they will be disabled and removed as parts of tables. typesToInclude.remove(ForeignKey.class); } final long createSnapshotStarted = System.currentTimeMillis(); snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(schemaToDrop, this, snapshotControl); LogService.getLog(getClass()).debug(LogType.LOG, String.format("Database snapshot generated in %d ms. Snapshot includes: %s", System.currentTimeMillis() - createSnapshotStarted, typesToInclude)); } catch (LiquibaseException e) { throw new UnexpectedLiquibaseException(e); } final long changeSetStarted = System.currentTimeMillis(); CompareControl compareControl = new CompareControl( new CompareControl.SchemaComparison[]{ new CompareControl.SchemaComparison( CatalogAndSchema.DEFAULT, schemaToDrop)}, snapshot.getSnapshotControl().getTypesToInclude()); DiffResult diffResult = DiffGeneratorFactory.getInstance().compare( new EmptyDatabaseSnapshot(this), snapshot, compareControl); List<ChangeSet> changeSets = new DiffToChangeLog(diffResult, new DiffOutputControl(true, true, false, null).addIncludedSchema(schemaToDrop)).generateChangeSets(); LogService.getLog(getClass()).debug(LogType.LOG, String.format("ChangeSet to Remove Database Objects generated in %d ms.", System.currentTimeMillis() - changeSetStarted)); boolean previousAutoCommit = this.getAutoCommitMode(); this.commit(); //clear out currently executed statements this.setAutoCommit(false); //some DDL doesn't work in autocommit mode final boolean reEnableFK = supportsForeignKeyDisable() && disableForeignKeyChecks(); try { for (ChangeSet changeSet : changeSets) { changeSet.setFailOnError(false); for (Change change : changeSet.getChanges()) { if (change instanceof DropTableChange) { ((DropTableChange) change).setCascadeConstraints(true); } SqlStatement[] sqlStatements = change.generateStatements(this); for (SqlStatement statement : sqlStatements) { ExecutorService.getInstance().getExecutor(this).execute(statement); } } this.commit(); } } finally { if (reEnableFK) { enableForeignKeyChecks(); } } ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(this).destroy(); LockServiceFactory.getInstance().getLockService(this).destroy(); this.setAutoCommit(previousAutoCommit); LogService.getLog(getClass()).info(LogType.LOG, String.format("Successfully deleted all supported object types in schema %s.", schemaToDrop.toString())); } finally { this.setObjectQuotingStrategy(currentStrategy); this.commit(); } }
Example #10
Source File: HiveStandardChangeLogHistoryService.java From liquibase-impala with Apache License 2.0 | 4 votes |
@Override public void init() throws DatabaseException { if (serviceInitialized) { return; } Database database = getDatabase(); Executor executor = ExecutorService.getInstance().getExecutor(database); Table changeLogTable = null; try { changeLogTable = SnapshotGeneratorFactory.getInstance().getDatabaseChangeLogTable(new SnapshotControl(database, false, Table.class, Column.class), database); } catch (LiquibaseException e) { throw new UnexpectedLiquibaseException(e); } List<SqlStatement> statementsToExecute = new ArrayList<SqlStatement>(); if (changeLogTable != null) { boolean hasDescription = changeLogTable.getColumn("DESCRIPTION") != null; boolean hasComments = changeLogTable.getColumn("COMMENTS") != null; boolean hasTag = changeLogTable.getColumn("TAG") != null; boolean hasLiquibase = changeLogTable.getColumn("LIQUIBASE") != null; boolean hasContexts = changeLogTable.getColumn("CONTEXTS") != null; boolean hasLabels = changeLogTable.getColumn("LABELS") != null; boolean hasOrderExecuted = changeLogTable.getColumn("ORDEREXECUTED") != null; boolean hasExecTypeColumn = changeLogTable.getColumn("EXECTYPE") != null; String charTypeName = getCharTypeName(); boolean hasDeploymentIdColumn = changeLogTable.getColumn("DEPLOYMENT_ID") != null; if (!hasDescription) { executor.comment("Adding missing databasechangelog.description column"); statementsToExecute.add(new AddColumnStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName(), "DESCRIPTION", charTypeName, null)); } if (!hasTag) { executor.comment("Adding missing databasechangelog.tag column"); statementsToExecute.add(new AddColumnStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName(), "TAG", charTypeName, null)); } if (!hasComments) { executor.comment("Adding missing databasechangelog.comments column"); statementsToExecute.add(new AddColumnStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName(), "COMMENTS", charTypeName, null)); } if (!hasLiquibase) { executor.comment("Adding missing databasechangelog.liquibase column"); statementsToExecute.add(new AddColumnStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName(), "LIQUIBASE", charTypeName, null)); } if (!hasOrderExecuted) { executor.comment("Adding missing databasechangelog.orderexecuted column"); statementsToExecute.add(new AddColumnStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName(), "ORDEREXECUTED", "int", null)); } if (!hasExecTypeColumn) { executor.comment("Adding missing databasechangelog.exectype column"); statementsToExecute.add(new AddColumnStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName(), "EXECTYPE", charTypeName, null)); } if (!hasContexts) { executor.comment("Adding missing databasechangelog.contexts column"); statementsToExecute.add(new AddColumnStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName(), "CONTEXTS", charTypeName, null)); } if (!hasLabels) { executor.comment("Adding missing databasechangelog.labels column"); statementsToExecute.add(new AddColumnStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName(), "LABELS", charTypeName, null)); } if (!hasDeploymentIdColumn) { executor.comment("Adding missing databasechangelog.deployment_id column"); statementsToExecute.add(new AddColumnStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName(), "DEPLOYMENT_ID", charTypeName, null)); } } else { executor.comment("Create Database Change Log Table"); SqlStatement createTableStatement = new CreateDatabaseChangeLogTableStatement(); if (!canCreateChangeLogTable()) { throw new DatabaseException("Cannot create " + getDatabase().escapeTableName(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName()) + " table for your getDatabase().\n\n" + "Please construct it manually using the following SQL as a base and re-run Liquibase:\n\n" + createTableStatement); } // If there is no table in the database for recording change history create one. statementsToExecute.add(createTableStatement); LOG.info("Creating database history table with name: " + getDatabase().escapeTableName(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName())); } for (SqlStatement sql : statementsToExecute) { if (SqlGeneratorFactory.getInstance().supports(sql, database)) { executor.execute(sql); getDatabase().commit(); } else { LOG.info("Cannot run " + sql.getClass().getSimpleName() + " on " + getDatabase().getShortName() + " when checking databasechangelog table"); } } serviceInitialized = true; }
Example #11
Source File: LiquibaseJpaUpdaterProvider.java From keycloak with Apache License 2.0 | 4 votes |
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(); if (ranChangeSets.isEmpty()) { logger.infov("Initializing database schema. Using changelog {0}", changelog); } else { if (logger.isDebugEnabled()) { logger.debugv("Updating database from {0} to {1}. Using changelog {2}", ranChangeSets.get(ranChangeSets.size() - 1).getId(), changeSets.get(changeSets.size() - 1).getId(), changelog); } else { logger.infov("Updating database. Using changelog {0}", changelog); } } if (exportWriter != null) { if (ranChangeSets.isEmpty()) { outputChangeLogTableCreationScript(liquibase, exportWriter); } liquibase.update((Contexts) null, new LabelExpression(), exportWriter, false); } else { liquibase.update((Contexts) null); } logger.debugv("Completed database update for changelog {0}", changelog); } else { logger.debugv("Database is up to date for changelog {0}", changelog); } // Needs to restart liquibase services to clear ChangeLogHistoryServiceFactory.getInstance(). // See https://issues.jboss.org/browse/KEYCLOAK-3769 for discussion relevant to why reset needs to be here resetLiquibaseServices(liquibase); }
Example #12
Source File: QuarkusJpaUpdaterProvider.java From keycloak with Apache License 2.0 | 4 votes |
protected void updateChangeSet(Liquibase liquibase, Writer exportWriter) throws LiquibaseException { 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(); if (ranChangeSets.isEmpty()) { logger.infov("Initializing database schema. Using changelog {0}", changelog); } else { if (logger.isDebugEnabled()) { logger.debugv("Updating database from {0} to {1}. Using changelog {2}", ranChangeSets.get(ranChangeSets.size() - 1).getId(), changeSets.get(changeSets.size() - 1).getId(), changelog); } else { logger.infov("Updating database. Using changelog {0}", changelog); } } if (exportWriter != null) { if (ranChangeSets.isEmpty()) { outputChangeLogTableCreationScript(liquibase, exportWriter); } liquibase.update((Contexts) null, new LabelExpression(), exportWriter, false); } else { liquibase.update((Contexts) null); } logger.debugv("Completed database update for changelog {0}", changelog); } else { logger.debugv("Database is up to date for changelog {0}", changelog); } // Needs to restart liquibase services to clear ChangeLogHistoryServiceFactory.getInstance(). // See https://issues.jboss.org/browse/KEYCLOAK-3769 for discussion relevant to why reset needs to be here resetLiquibaseServices(liquibase); }