use of liquibase.snapshot.SnapshotGeneratorFactory in project liquibase by liquibase.
the class AbstractDatabaseDiffTask method getDiffResult.
protected DiffResult getDiffResult() {
Liquibase liquibase = getLiquibase();
Database targetDatabase = liquibase.getDatabase();
Database referenceDatabase = createDatabaseFromType(referenceDatabaseType, getResourceAccessor());
CatalogAndSchema targetCatalogAndSchema = buildCatalogAndSchema(targetDatabase);
CatalogAndSchema referenceCatalogAndSchema = buildCatalogAndSchema(referenceDatabase);
CompareControl.SchemaComparison[] schemaComparisons = { new CompareControl.SchemaComparison(referenceCatalogAndSchema, targetCatalogAndSchema) };
SnapshotGeneratorFactory snapshotGeneratorFactory = SnapshotGeneratorFactory.getInstance();
DatabaseSnapshot referenceSnapshot;
try {
referenceSnapshot = snapshotGeneratorFactory.createSnapshot(referenceDatabase.getDefaultSchema(), referenceDatabase, new SnapshotControl(referenceDatabase, diffTypes));
} catch (LiquibaseException e) {
throw new BuildException("Unable to create a DatabaseSnapshot: " + e.getMessage(), e);
}
CompareControl compareControl = new CompareControl(schemaComparisons, referenceSnapshot.getSnapshotControl().getTypesToInclude());
try {
return liquibase.diff(referenceDatabase, targetDatabase, compareControl);
} catch (LiquibaseException e) {
throw new BuildException("Unable to diff databases: " + e.getMessage(), e);
}
}
use of liquibase.snapshot.SnapshotGeneratorFactory in project liquibase by liquibase.
the class AbstractIntegrationTest method testContextsWithHyphensWorkInFormattedSql.
@Test
public void testContextsWithHyphensWorkInFormattedSql() throws Exception {
assumeNotNull(this.getDatabase());
Liquibase liquibase = createLiquibase("changelogs/common/sqlstyle/formatted.changelog.sql");
liquibase.update("hyphen-context-using-sql,camelCaseContextUsingSql");
SnapshotGeneratorFactory tableSnapshotGenerator = SnapshotGeneratorFactory.getInstance();
assertTrue(tableSnapshotGenerator.has(new Table().setName("hyphen_context"), database));
assertTrue(tableSnapshotGenerator.has(new Table().setName("camel_context"), database));
assertTrue(tableSnapshotGenerator.has(new Table().setName("bar_id"), database));
assertTrue(tableSnapshotGenerator.has(new Table().setName("foo_id"), database));
}
use of liquibase.snapshot.SnapshotGeneratorFactory in project liquibase by liquibase.
the class AbstractIntegrationTest method contextsWithHyphensWorkInFormattedSql.
@Test
public void contextsWithHyphensWorkInFormattedSql() throws Exception {
if (database == null) {
return;
}
Liquibase liquibase = createLiquibase("changelogs/common/sqlstyle/formatted.changelog.sql");
liquibase.update("hyphen-context-using-sql,camelCaseContextUsingSql");
SnapshotGeneratorFactory tableSnapshotGenerator = SnapshotGeneratorFactory.getInstance();
assertNotNull(tableSnapshotGenerator.has(new Table().setName("hyphen_context"), database));
assertNotNull(tableSnapshotGenerator.has(new Table().setName("camel_context"), database));
assertNotNull(tableSnapshotGenerator.has(new Table().setName("bar_id"), database));
assertNotNull(tableSnapshotGenerator.has(new Table().setName("foo_id"), database));
}
use of liquibase.snapshot.SnapshotGeneratorFactory in project liquibase by liquibase.
the class AbstractIntegrationTest method wipeDatabase.
/**
* Wipes all Liquibase schemas in the database before testing starts. This includes the DATABASECHANGELOG/LOCK
* tables.
*/
protected void wipeDatabase() {
emptySchemas.clear();
try {
// TODO the cleaner solution would be to have a noCachingHasObject() Method in SnapshotGeneratorFactory
try {
if (database.getConnection() != null) {
String sql = "DROP TABLE " + database.getDatabaseChangeLogLockTableName();
for (SqlListener listener : Scope.getCurrentScope().getListeners(SqlListener.class)) {
listener.writeSqlWillRun(sql);
}
((JdbcConnection) database.getConnection()).getUnderlyingConnection().createStatement().executeUpdate(sql);
database.commit();
}
} catch (SQLException e) {
if (database instanceof PostgresDatabase) {
// throws "current transaction is aborted" unless we roll back the connection
database.rollback();
}
}
SnapshotGeneratorFactory.resetAll();
LockService lockService = LockServiceFactory.getInstance().getLockService(database);
emptyTestSchema(CatalogAndSchema.DEFAULT.getCatalogName(), CatalogAndSchema.DEFAULT.getSchemaName(), database);
SnapshotGeneratorFactory factory = SnapshotGeneratorFactory.getInstance();
if (database.supportsSchemas()) {
emptyTestSchema(null, ALT_SCHEMA, database);
}
if (supportsAltCatalogTests()) {
if (database.supportsSchemas() && database.supportsCatalogs()) {
emptyTestSchema(ALT_CATALOG, ALT_SCHEMA, database);
}
}
/*
* There is a special treatment for identifiers in the case when (a) the RDBMS does NOT support
* schemas AND (b) the RDBMS DOES support catalogs AND (c) someone uses "schemaName=..." in a
* Liquibase ChangeSet. In this case, AbstractJdbcDatabase.escapeObjectName assumes the author
* was intending to write "catalog=..." and transparently rewrites the expression.
* For us, this means that we have to wipe both ALT_SCHEMA and ALT_CATALOG to be sure we
* are doing a thorough cleanup.
*/
CatalogAndSchema[] alternativeLocations = new CatalogAndSchema[] { new CatalogAndSchema(ALT_CATALOG, null), new CatalogAndSchema(null, ALT_SCHEMA), new CatalogAndSchema("LBCAT2", database.getDefaultSchemaName()), new CatalogAndSchema(null, "LBCAT2"), new CatalogAndSchema("lbcat2", database.getDefaultSchemaName()), new CatalogAndSchema(null, "lbcat2") };
for (CatalogAndSchema location : alternativeLocations) {
emptyTestSchema(location.getCatalogName(), location.getSchemaName(), database);
}
database.commit();
SnapshotGeneratorFactory.resetAll();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of liquibase.snapshot.SnapshotGeneratorFactory in project liquibase by liquibase.
the class AbstractIntegrationTest method emptyTestSchema.
/**
* Transforms a given combination of catalogName and schemaName into the standardized format for the given
* database. If the database has the
*
* @param catalogName catalog name (or null)
* @param schemaName schema name (or null)
* @param database the database where the target might exist
* @throws LiquibaseException if any problem occurs during the process
*/
protected void emptyTestSchema(String catalogName, String schemaName, Database database) throws LiquibaseException {
SnapshotGeneratorFactory factory = SnapshotGeneratorFactory.getInstance();
CatalogAndSchema target = new CatalogAndSchema(catalogName, schemaName).standardize(database);
Schema schema = new Schema(target.getCatalogName(), target.getSchemaName());
if (factory.has(schema, database)) {
if (!emptySchemas.contains(target.toString())) {
database.dropDatabaseObjects(target);
emptySchemas.add(target.toString());
}
}
}
Aggregations