Search in sources :

Example 1 with SnapshotGeneratorFactory

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);
    }
}
Also used : Liquibase(liquibase.Liquibase) Database(liquibase.database.Database) CompareControl(liquibase.diff.compare.CompareControl) SnapshotGeneratorFactory(liquibase.snapshot.SnapshotGeneratorFactory) LiquibaseException(liquibase.exception.LiquibaseException) BuildException(org.apache.tools.ant.BuildException) CatalogAndSchema(liquibase.CatalogAndSchema) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) SnapshotControl(liquibase.snapshot.SnapshotControl)

Example 2 with SnapshotGeneratorFactory

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));
}
Also used : SnapshotGeneratorFactory(liquibase.snapshot.SnapshotGeneratorFactory) Test(org.junit.Test)

Example 3 with SnapshotGeneratorFactory

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));
}
Also used : Liquibase(liquibase.Liquibase) SnapshotGeneratorFactory(liquibase.snapshot.SnapshotGeneratorFactory) Test(org.junit.Test)

Example 4 with SnapshotGeneratorFactory

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);
    }
}
Also used : PostgresDatabase(liquibase.database.core.PostgresDatabase) SqlListener(liquibase.listener.SqlListener) LockService(liquibase.lockservice.LockService) SQLException(java.sql.SQLException) JdbcConnection(liquibase.database.jvm.JdbcConnection) SnapshotGeneratorFactory(liquibase.snapshot.SnapshotGeneratorFactory) ValidationFailedException(liquibase.exception.ValidationFailedException) ChangeLogParseException(liquibase.exception.ChangeLogParseException) SQLException(java.sql.SQLException) DatabaseException(liquibase.exception.DatabaseException) LiquibaseException(liquibase.exception.LiquibaseException)

Example 5 with SnapshotGeneratorFactory

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());
        }
    }
}
Also used : SnapshotGeneratorFactory(liquibase.snapshot.SnapshotGeneratorFactory)

Aggregations

SnapshotGeneratorFactory (liquibase.snapshot.SnapshotGeneratorFactory)5 Liquibase (liquibase.Liquibase)2 LiquibaseException (liquibase.exception.LiquibaseException)2 Test (org.junit.Test)2 SQLException (java.sql.SQLException)1 CatalogAndSchema (liquibase.CatalogAndSchema)1 Database (liquibase.database.Database)1 PostgresDatabase (liquibase.database.core.PostgresDatabase)1 JdbcConnection (liquibase.database.jvm.JdbcConnection)1 CompareControl (liquibase.diff.compare.CompareControl)1 ChangeLogParseException (liquibase.exception.ChangeLogParseException)1 DatabaseException (liquibase.exception.DatabaseException)1 ValidationFailedException (liquibase.exception.ValidationFailedException)1 SqlListener (liquibase.listener.SqlListener)1 LockService (liquibase.lockservice.LockService)1 DatabaseSnapshot (liquibase.snapshot.DatabaseSnapshot)1 SnapshotControl (liquibase.snapshot.SnapshotControl)1 BuildException (org.apache.tools.ant.BuildException)1