use of liquibase.CatalogAndSchema in project liquibase by liquibase.
the class AbstractExecuteTest method resetAvailableDatabases.
public void resetAvailableDatabases() throws Exception {
for (Database database : DatabaseTestContext.getInstance().getAvailableDatabases()) {
DatabaseConnection connection = database.getConnection();
Statement connectionStatement = ((JdbcConnection) connection).getUnderlyingConnection().createStatement();
try {
database.dropDatabaseObjects(CatalogAndSchema.DEFAULT);
} catch (Throwable e) {
throw new UnexpectedLiquibaseException("Error dropping objects for database " + database.getShortName(), e);
}
try {
connectionStatement.executeUpdate("drop table " + database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogLockTableName()));
} catch (SQLException e) {
;
}
connection.commit();
try {
connectionStatement.executeUpdate("drop table " + database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()));
} catch (SQLException e) {
;
}
connection.commit();
if (database.supportsSchemas()) {
database.dropDatabaseObjects(new CatalogAndSchema(DatabaseTestContext.ALT_CATALOG, DatabaseTestContext.ALT_SCHEMA));
connection.commit();
try {
connectionStatement.executeUpdate("drop table " + database.escapeTableName(DatabaseTestContext.ALT_CATALOG, DatabaseTestContext.ALT_SCHEMA, database.getDatabaseChangeLogLockTableName()));
} catch (SQLException e) {
//ok
}
connection.commit();
try {
connectionStatement.executeUpdate("drop table " + database.escapeTableName(DatabaseTestContext.ALT_CATALOG, DatabaseTestContext.ALT_SCHEMA, database.getDatabaseChangeLogTableName()));
} catch (SQLException e) {
//ok
}
connection.commit();
}
List<? extends SqlStatement> setupStatements = setupStatements(database);
if (setupStatements != null) {
for (SqlStatement statement : setupStatements) {
ExecutorService.getInstance().getExecutor(database).execute(statement);
}
}
connectionStatement.close();
}
}
use of liquibase.CatalogAndSchema in project dropwizard by dropwizard.
the class DbDumpCommand method generateChangeLog.
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);
}
}
use of liquibase.CatalogAndSchema in project liquibase by liquibase.
the class DiffCommand method createTargetSnapshot.
protected DatabaseSnapshot createTargetSnapshot() throws DatabaseException, InvalidExampleException {
CatalogAndSchema[] schemas;
if (compareControl == null || compareControl.getSchemaComparisons() == null) {
schemas = new CatalogAndSchema[] { targetDatabase.getDefaultSchema() };
} else {
schemas = new CatalogAndSchema[compareControl.getSchemaComparisons().length];
int i = 0;
for (CompareControl.SchemaComparison comparison : compareControl.getSchemaComparisons()) {
CatalogAndSchema schema;
if (targetDatabase.supportsSchemas()) {
schema = new CatalogAndSchema(targetDatabase.getDefaultCatalogName(), comparison.getComparisonSchema().getSchemaName());
} else {
schema = new CatalogAndSchema(comparison.getComparisonSchema().getSchemaName(), comparison.getComparisonSchema().getSchemaName());
}
schemas[i++] = schema;
}
}
SnapshotControl snapshotControl = getTargetSnapshotControl();
if (snapshotControl == null) {
snapshotControl = new SnapshotControl(targetDatabase, snapshotTypes);
}
if (getSnapshotListener() != null) {
snapshotControl.setSnapshotListener(getSnapshotListener());
}
ObjectQuotingStrategy originalStrategy = referenceDatabase.getObjectQuotingStrategy();
try {
referenceDatabase.setObjectQuotingStrategy(ObjectQuotingStrategy.QUOTE_ALL_OBJECTS);
return SnapshotGeneratorFactory.getInstance().createSnapshot(schemas, targetDatabase, snapshotControl);
} finally {
referenceDatabase.setObjectQuotingStrategy(originalStrategy);
}
}
use of liquibase.CatalogAndSchema in project liquibase by liquibase.
the class DropAllCommand method run.
@Override
protected CommandResult run() throws Exception {
try {
LockServiceFactory.getInstance().getLockService(database).waitForLock();
for (CatalogAndSchema schema : schemas) {
log.info("Dropping Database Objects in schema: " + schema);
checkLiquibaseTables(false, null, new Contexts(), new LabelExpression());
database.dropDatabaseObjects(schema);
}
} catch (DatabaseException e) {
throw e;
} catch (Exception e) {
throw new DatabaseException(e);
} finally {
LockServiceFactory.getInstance().getLockService(database).destroy();
resetServices();
}
return new CommandResult("All objects dropped from " + database.getConnection().getConnectionUserName() + "@" + database.getConnection().getURL());
}
use of liquibase.CatalogAndSchema in project liquibase by liquibase.
the class SQLiteDatabase method getAlterTableStatements.
public static List<SqlStatement> getAlterTableStatements(AlterTableVisitor alterTableVisitor, Database database, String catalogName, String schemaName, String tableName) throws DatabaseException {
List<SqlStatement> statements = new ArrayList<SqlStatement>();
Table table = null;
try {
table = SnapshotGeneratorFactory.getInstance().createSnapshot((Table) new Table().setName(tableName).setSchema(new Schema(new Catalog(null), null)), database);
List<ColumnConfig> createColumns = new ArrayList<ColumnConfig>();
List<ColumnConfig> copyColumns = new ArrayList<ColumnConfig>();
if (table != null) {
for (Column column : table.getColumns()) {
ColumnConfig new_column = new ColumnConfig(column);
if (alterTableVisitor.createThisColumn(new_column)) {
createColumns.add(new_column);
}
ColumnConfig copy_column = new ColumnConfig(column);
if (alterTableVisitor.copyThisColumn(copy_column)) {
copyColumns.add(copy_column);
}
}
}
for (ColumnConfig column : alterTableVisitor.getColumnsToAdd()) {
if (alterTableVisitor.createThisColumn(column)) {
createColumns.add(column);
}
if (alterTableVisitor.copyThisColumn(column)) {
copyColumns.add(column);
}
}
List<Index> newIndices = new ArrayList<Index>();
for (Index index : SnapshotGeneratorFactory.getInstance().createSnapshot(new CatalogAndSchema(catalogName, schemaName), database, new SnapshotControl(database, Index.class)).get(Index.class)) {
if (index.getTable().getName().equalsIgnoreCase(tableName)) {
if (alterTableVisitor.createThisIndex(index)) {
newIndices.add(index);
}
}
}
// rename table
String temp_table_name = tableName + "_temporary";
statements.addAll(Arrays.asList(new RenameTableStatement(catalogName, schemaName, tableName, temp_table_name)));
// create temporary table
CreateTableChange ct_change_tmp = new CreateTableChange();
ct_change_tmp.setSchemaName(schemaName);
ct_change_tmp.setTableName(tableName);
for (ColumnConfig column : createColumns) {
ct_change_tmp.addColumn(column);
}
statements.addAll(Arrays.asList(ct_change_tmp.generateStatements(database)));
// copy rows to temporary table
statements.addAll(Arrays.asList(new CopyRowsStatement(temp_table_name, tableName, copyColumns)));
// delete original table
statements.addAll(Arrays.asList(new DropTableStatement(catalogName, schemaName, temp_table_name, false)));
// validate indices
statements.addAll(Arrays.asList(new ReindexStatement(catalogName, schemaName, tableName)));
// add remaining indices
for (Index index_config : newIndices) {
AddColumnConfig[] columns = new AddColumnConfig[index_config.getColumns().size()];
for (int i = 0; i < index_config.getColumns().size(); i++) {
columns[i] = new AddColumnConfig(index_config.getColumns().get(i));
}
statements.addAll(Arrays.asList(new CreateIndexStatement(index_config.getName(), catalogName, schemaName, tableName, index_config.isUnique(), index_config.getAssociatedWithAsString(), columns)));
}
return statements;
} catch (InvalidExampleException e) {
throw new UnexpectedLiquibaseException(e);
}
}
Aggregations