liquibase.exception.UnexpectedLiquibaseException Java Examples

The following examples show how to use liquibase.exception.UnexpectedLiquibaseException. 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: CustomLockService.java    From keycloak with Apache License 2.0 6 votes vote down vote up
private Set<Integer> currentIdsInDatabaseChangeLogLockTable() throws DatabaseException {
    try {
        Executor executor = ExecutorService.getInstance().getExecutor(database);
        String idColumnName = database.escapeColumnName(database.getLiquibaseCatalogName(),
                database.getLiquibaseSchemaName(),
                database.getDatabaseChangeLogLockTableName(),
                "ID");
        String lockTableName = database.escapeTableName(database.getLiquibaseCatalogName(),
                database.getLiquibaseSchemaName(),
                database.getDatabaseChangeLogLockTableName());
        SqlStatement sqlStatement = new RawSqlStatement("SELECT " + idColumnName + " FROM " + lockTableName);
        List<Map<String, ?>> rows = executor.queryForList(sqlStatement);
        Set<Integer> ids = rows.stream().map(columnMap -> ((Number) columnMap.get("ID")).intValue()).collect(Collectors.toSet());
        database.commit();
        return ids;
    } catch (UnexpectedLiquibaseException ulie) {
        // It can happen with MariaDB Galera 10.1 that UnexpectedLiquibaseException is rethrown due the DB lock.
        // It is sufficient to just rollback transaction and retry in that case.
        if (ulie.getCause() != null && ulie.getCause() instanceof DatabaseException) {
            throw (DatabaseException) ulie.getCause();
        } else {
            throw ulie;
        }
    }
}
 
Example #2
Source File: GeometryColumnsUtils.java    From liquibase-spatial with Apache License 2.0 6 votes vote down vote up
/**
 * 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: SpatialIndexExistsPrecondition.java    From liquibase-spatial with Apache License 2.0 6 votes vote down vote up
/**
 * @see liquibase.serializer.LiquibaseSerializable#getSerializableFieldValue(java.lang.String)
 */
@Override
public Object getSerializableFieldValue(final String field) {
   final Object value;
   if ("catalogName".equals(field)) {
      value = getCatalogName();
   } else if ("schemaName".equals(field)) {
      value = getSchemaName();
   } else if ("tableName".equals(field)) {
      value = getTableName();
   } else if ("columnNames".equals(field)) {
      value = getColumnNames();
   } else if ("indexName".equals(field)) {
      value = getIndexName();
   } else {
      throw new UnexpectedLiquibaseException("Unexpected field request on "
            + getSerializedObjectName() + ": " + field);
   }
   return value;
}
 
Example #4
Source File: TestDataSourceProvider.java    From multiapps with Apache License 2.0 6 votes vote down vote up
public static DataSource getDataSource(String liquibaseChangelogLocation) throws Exception {
    // create a hsql in memory connection
    Connection connection = createH2InMemory();

    // Create the schema for unit testing
    Database liquibaseDb = DatabaseFactory.getInstance()
                                          .findCorrectDatabaseImplementation(new JdbcConnection(connection));
    Liquibase lq = new Liquibase(liquibaseChangelogLocation, new ClassLoaderResourceAccessor(), liquibaseDb);
    try {
        lq.update("");
    } catch (MigrationFailedException e) {
        // catch the exception because in PopulateConfigurationRegistrySpaceIdColumnChange liquibase change there is rest call
        if (e.getCause()
             .getClass() != UnexpectedLiquibaseException.class) {
            throw e;
        }
    }

    // Initialize the fileService to use our in-memory connection through a pool emulation (so
    // that close releases rather than close)
    return new SingleConnectionDataSource(connection, true);
}
 
Example #5
Source File: GeometryType.java    From liquibase-spatial with Apache License 2.0 6 votes vote down vote up
/**
 * @see liquibase.datatype.LiquibaseDataType#sqlToObject(java.lang.String,
 *      liquibase.database.Database)
 */
@Override
public Object sqlToObject(final String value, final Database database) {
   final Geometry returnValue;
   if (value == null || value.equalsIgnoreCase("null")) {
      returnValue = null;
   } else {
      final WKTReader reader = new WKTReader();
      try {
         // TODO: Check for SRID.
         returnValue = reader.read(value);
      } catch (final ParseException e) {
         throw new UnexpectedLiquibaseException("Cannot parse " + value + " to a Geometry", e);
      }
   }
   return returnValue;
}
 
Example #6
Source File: DropSpatialIndexGeneratorGeoDB.java    From liquibase-spatial with Apache License 2.0 6 votes vote down vote up
/**
 * 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 #7
Source File: GeometryType.java    From liquibase-spatial with Apache License 2.0 6 votes vote down vote up
/**
 * @see liquibase.datatype.LiquibaseDataType#objectToSql(java.lang.Object,
 *      liquibase.database.Database)
 */
@Override
public String objectToSql(final Object value, final Database database) {
   final String returnValue;
   if (value instanceof Geometry) {
      // TODO: Tailor the output for the database.
      returnValue = ((Geometry) value).toText();
   } else if (value instanceof String) {
      returnValue = value.toString();
   } else if (value instanceof DatabaseFunction) {
      returnValue = value.toString();
   } else if (value == null || value.toString().equalsIgnoreCase("null")) {
      returnValue = null;
   } else {
      throw new UnexpectedLiquibaseException("Cannot convert type " + value.getClass()
            + " to a Geometry value");
   }
   return returnValue;
}
 
Example #8
Source File: OracleSpatialUtils.java    From liquibase-spatial with Apache License 2.0 6 votes vote down vote up
/**
 * Queries to the database to convert the given EPSG SRID to the corresponding Oracle SRID.
 *
 * @param srid
 *           the EPSG SRID.
 * @param database
 *           the database instance.
 * @return the corresponding Oracle SRID.
 */
public static String loadOracleSrid(final String srid, final Database database) {
   final String oracleSrid;
   final JdbcConnection jdbcConnection = (JdbcConnection) database.getConnection();
   final Connection connection = jdbcConnection.getUnderlyingConnection();
   Statement statement = null;
   try {
      statement = connection.createStatement();
      final ResultSet resultSet = statement.executeQuery("SELECT " + EPSG_TO_ORACLE_FUNCTION
            + "(" + srid + ") FROM dual");
      resultSet.next();
      oracleSrid = resultSet.getString(1);
   } catch (final SQLException e) {
      throw new UnexpectedLiquibaseException("Failed to find the Oracle SRID for EPSG:" + srid,
            e);
   } finally {
      try {
         statement.close();
      } catch (final SQLException ignore) {
      }
   }
   return oracleSrid;
}
 
Example #9
Source File: CustomLockService.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isDatabaseChangeLogLockTableInitialized(boolean tableJustCreated) throws DatabaseException {
    try {
        return super.isDatabaseChangeLogLockTableInitialized(tableJustCreated);
    } catch (UnexpectedLiquibaseException ulie) {
        // It can happen with MariaDB Galera 10.1 that UnexpectedLiquibaseException is rethrown due the DB lock. It is sufficient to just rollback transaction and retry in that case.
        if (ulie.getCause() != null && ulie.getCause() instanceof DatabaseException) {
            throw (DatabaseException) ulie.getCause();
        } else {
            throw ulie;
        }
    }
}
 
Example #10
Source File: GeometryColumnsUtils.java    From liquibase-spatial with Apache License 2.0 5 votes vote down vote up
/**
 * Determines if the given column is in <code>GEOMETRY_COLUMNS</code>.
 * 
 * @param database
 *           the database to query.
 * @param schemaName
 *           the schema name.
 * @param tableName
 *           the table name.
 * @param columnName
 *           the column name.
 * @return <code>true</code> if the column is a geometry column.
 */
public static boolean isGeometryColumn(final Database database,
      final String schemaName, final String tableName,
      final String columnName) {
   boolean isSpatialColumn = false;
   Statement jdbcStatement = null;
   try {
      if (geometryColumnsExists(database)) {
         final String query = "SELECT * FROM geometry_columns WHERE f_table_schema = '"
               + (schemaName == null ? database.getDefaultSchemaName()
                     : schemaName)
               + "' AND f_table_name = '"
               + tableName
               + "' AND upper(f_geometry_column) = '"
               + columnName.toUpperCase() + "'";
         final DatabaseConnection databaseConnection = database
               .getConnection();
         final JdbcConnection jdbcConnection = (JdbcConnection) databaseConnection;
         jdbcStatement = jdbcConnection.getUnderlyingConnection()
               .createStatement();
         final ResultSet rs = jdbcStatement.executeQuery(query);
         isSpatialColumn = rs.next();
      }
   } catch (final SQLException e) {
      throw new UnexpectedLiquibaseException(
            "Failed to determine if the column to be dropped is a geometry column",
            e);
   } finally {
      if (jdbcStatement != null) {
         try {
            jdbcStatement.close();
         } catch (final SQLException ignore) {
         }
      }
   }
   return isSpatialColumn;
}
 
Example #11
Source File: GeometryColumnsUtils.java    From liquibase-spatial with Apache License 2.0 5 votes vote down vote up
/**
 * Determines if the given table is in <code>GEOMETRY_COLUMNS</code> and,
 * therefore, has a geometry column.
 * 
 * @param database
 *           the database to query.
 * @param schemaName
 *           the schema name.
 * @param tableName
 *           the table name to check.
 * @return <code>true</code> if the table has a geometry column.
 */
public static boolean hasGeometryColumn(final Database database,
      final String schemaName, final String tableName) {
   boolean isSpatialColumn = false;
   Statement jdbcStatement = null;
   try {
      if (geometryColumnsExists(database)) {
         final String query = "SELECT * FROM geometry_columns WHERE f_table_schema = '"
               + (schemaName == null ? database.getDefaultSchemaName()
                     : schemaName)
               + "' AND f_table_name = '"
               + tableName
               + "'";
         final DatabaseConnection databaseConnection = database
               .getConnection();
         final JdbcConnection jdbcConnection = (JdbcConnection) databaseConnection;
         jdbcStatement = jdbcConnection.getUnderlyingConnection()
               .createStatement();
         final ResultSet rs = jdbcStatement.executeQuery(query);
         isSpatialColumn = rs.next();
      }
   } catch (final SQLException e) {
      throw new UnexpectedLiquibaseException(
            "Failed to determine if the table has a geometry column", e);
   } finally {
      if (jdbcStatement != null) {
         try {
            jdbcStatement.close();
         } catch (final SQLException ignore) {
         }
      }
   }
   return isSpatialColumn;
}
 
Example #12
Source File: DropSpatialIndexGeneratorOracle.java    From liquibase-spatial with Apache License 2.0 5 votes vote down vote up
@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 #13
Source File: PerconaAddColumnChange.java    From liquibase-percona with Apache License 2.0 5 votes vote down vote up
private String addForeignKeyConstraint(AddColumnConfig column, Database database) {
    String result = "";
    ConstraintsConfig constraintsConfig = column.getConstraints();
    if (constraintsConfig != null && (StringUtil.isNotEmpty(constraintsConfig.getReferences()) || StringUtil.isNotEmpty(constraintsConfig.getReferencedTableName()) )) {
        result += ", ADD ";
        if (StringUtil.isNotEmpty(constraintsConfig.getForeignKeyName())) {
            result += "CONSTRAINT " + database.escapeConstraintName(constraintsConfig.getForeignKeyName()) + " ";
        }
        result +=  "FOREIGN KEY ("
                + database.escapeColumnName(null, null, null, column.getName()) + ") REFERENCES ";

        String referencedTable;
        String referencedColumn;

        if (StringUtil.isNotEmpty(constraintsConfig.getReferences())) {
            Matcher references = Pattern.compile("([\\w\\._]+)\\(([\\w_]+)\\)").matcher(constraintsConfig.getReferences());
            if (!references.matches()) {
                throw new UnexpectedLiquibaseException("Unable to get table name and column name from " + constraintsConfig.getReferences());
            }
            referencedTable = references.group(1);
            referencedColumn = references.group(2);
        } else {
            referencedTable = constraintsConfig.getReferencedTableName();
            referencedColumn = constraintsConfig.getReferencedColumnNames();
        }

        referencedTable = PerconaChangeUtil.resolveReferencedPerconaTableName(getTableName(), referencedTable);

        result += database.escapeTableName(null, null, referencedTable) + "(";
        result += database.escapeColumnName(null, null, null, referencedColumn);
        result += ")";
    }
    return result;
}
 
Example #14
Source File: HiveStandardChangeLogHistoryService.java    From liquibase-impala with Apache License 2.0 5 votes vote down vote up
@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 #15
Source File: HiveStandardChangeLogHistoryService.java    From liquibase-impala with Apache License 2.0 5 votes vote down vote up
private boolean hasDatabaseChangeLogTable() throws DatabaseException {
    if (hasDatabaseChangeLogTable == null) {
        try {
            hasDatabaseChangeLogTable = SnapshotGeneratorFactory.getInstance().hasDatabaseChangeLogTable(getDatabase());
        } catch (LiquibaseException e) {
            throw new UnexpectedLiquibaseException(e);
        }
    }
    return hasDatabaseChangeLogTable;
}
 
Example #16
Source File: HiveMarkChangeSetRanGenerator.java    From liquibase-impala with Apache License 2.0 5 votes vote down vote up
@Override
public Sql[] generateSql(MarkChangeSetRanStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    ChangeSet changeSet = statement.getChangeSet();

    SqlStatement runStatement;
    try {
        if (statement.getExecType().equals(ChangeSet.ExecType.FAILED) || statement.getExecType().equals(ChangeSet.ExecType.SKIPPED)) {
            return new Sql[0]; //don't mark
        }

        String tag = null;
        for (Change change : changeSet.getChanges()) {
            if (change instanceof TagDatabaseChange) {
                TagDatabaseChange tagChange = (TagDatabaseChange) change;
                tag = tagChange.getTag();
            }
        }

        runStatement = new HiveInsertStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName())
                .addColumnValue(changeSet.getId())
                .addColumnValue(changeSet.getAuthor())
                .addColumnValue(changeSet.getFilePath())
                .addColumnValue(DateTimeUtils.getCurrentTS("yyyy-MM-dd HH:mm:ss"))
                .addColumnValue(ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).getNextSequenceValue())
                .addColumnValue(statement.getExecType().value)
                .addColumnValue(changeSet.generateCheckSum().toString())
                .addColumnValue(changeSet.getDescription())
                .addColumnValue(StringUtils.trimToEmpty(changeSet.getComments()))
                .addColumnValue(tag == null ? "NULL" : tag)
                .addColumnValue(ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).getDeploymentId())
                .addColumnValue(changeSet.getContexts() == null || changeSet.getContexts().isEmpty() ? null : changeSet.getContexts().toString())
                .addColumnValue(changeSet.getLabels() == null || changeSet.getLabels().isEmpty() ? null : changeSet.getLabels().toString())
                .addColumnValue(LiquibaseUtil.getBuildVersion());
    } catch (LiquibaseException e) {
        throw new UnexpectedLiquibaseException(e);
    }

    return SqlGeneratorFactory.getInstance().generateSql(runStatement, database);
}
 
Example #17
Source File: HiveDropColumnGenerator.java    From liquibase-impala with Apache License 2.0 5 votes vote down vote up
private Sql[] generateMultipleColumnSql(DropColumnStatement dropColumnStatement, Database database, Map<String, String> columnsPreserved) {
    if (columnsPreserved == null) {
        throw new UnexpectedLiquibaseException("no columns to preserve");
    }
    List<Sql> result = new ArrayList<Sql>();
    Map<String, String> columnsPreservedCopy = new HashMap<String, String>(columnsPreserved);
    String alterTable;
    List<DropColumnStatement> columns = null;

    if (dropColumnStatement.isMultiple()) {
        columns = dropColumnStatement.getColumns();
        for (DropColumnStatement statement : columns) {
            columnsPreservedCopy.remove(statement.getColumnName());
        }
        alterTable = "ALTER TABLE " + database.escapeTableName(columns.get(0).getCatalogName(), columns.get(0).getSchemaName(), columns.get(0).getTableName()) + " REPLACE COLUMNS (";
    } else {
        columnsPreservedCopy.remove(dropColumnStatement.getColumnName());
        alterTable = "ALTER TABLE " + database.escapeTableName(dropColumnStatement.getCatalogName(), dropColumnStatement.getSchemaName(), dropColumnStatement.getTableName()) + " REPLACE COLUMNS (";
    }

    int i = 0;
    for (String columnName : columnsPreservedCopy.keySet()) {
        alterTable += database.escapeObjectName(columnName, Column.class) + " " + columnsPreservedCopy.get(columnName);
        if (i < columnsPreservedCopy.size() - 1) {
            alterTable += ",";
        } else {
            alterTable += ")";
        }
        i++;
    }

    if (dropColumnStatement.isMultiple()) {
        result.add(new UnparsedSql(alterTable, getAffectedColumns(columns)));
    } else {
        result.add(new UnparsedSql(alterTable, getAffectedColumn(dropColumnStatement)));
    }
    return result.toArray(new Sql[result.size()]);
}
 
Example #18
Source File: SpatialSupportedPrecondition.java    From liquibase-spatial with Apache License 2.0 4 votes vote down vote up
/**
 * @see liquibase.serializer.LiquibaseSerializable#getSerializableFieldValue(java.lang.String)
 */
@Override
public Object getSerializableFieldValue(final String field) {
   throw new UnexpectedLiquibaseException("Unexpected field request on "
         + getSerializedObjectName() + ": " + field);
}
 
Example #19
Source File: HiveStandardChangeLogHistoryService.java    From liquibase-impala with Apache License 2.0 4 votes vote down vote up
@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 #20
Source File: AbstractJdbcDatabase.java    From jweb-cms with GNU Affero General Public License v3.0 4 votes vote down vote up
@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();
    }
}