Search in sources :

Example 6 with SqlListener

use of liquibase.listener.SqlListener in project liquibase by liquibase.

the class DerbyConnection method checkPoint.

private void checkPoint() throws DatabaseException {
    Statement st = null;
    try {
        st = createStatement();
        final String sql = "CALL SYSCS_UTIL.SYSCS_CHECKPOINT_DATABASE()";
        for (SqlListener listener : Scope.getCurrentScope().getListeners(SqlListener.class)) {
            listener.writeSqlWillRun(sql);
        }
        st.execute(sql);
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        JdbcUtil.closeStatement(st);
    }
}
Also used : SqlListener(liquibase.listener.SqlListener) SQLException(java.sql.SQLException) Statement(java.sql.Statement) DatabaseException(liquibase.exception.DatabaseException)

Example 7 with SqlListener

use of liquibase.listener.SqlListener in project liquibase by liquibase.

the class AbstractIntegrationTest method clearDatabase.

/**
 * Drops all supported object types in all testing schemas and the DATABASECHANGELOG table if it resides in a
 * different schema from the test schemas.
 *
 * @throws DatabaseException if something goes wrong during object deletion
 */
protected void clearDatabase() throws DatabaseException {
    wipeDatabase();
    try {
        Statement statement = null;
        try {
            // only drop the DATABASECHANGELOG table if it really exists.
            if (SnapshotGeneratorFactory.getInstance().has(new Table().setName(database.getDatabaseChangeLogTableName()).setSchema(new Schema(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName())), database)) {
                statement = ((JdbcConnection) database.getConnection()).getUnderlyingConnection().createStatement();
                final String sql = "DROP TABLE " + database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName());
                for (SqlListener listener : Scope.getCurrentScope().getListeners(SqlListener.class)) {
                    listener.writeSqlWillRun(sql);
                }
                statement.execute(sql);
                database.commit();
            }
        } catch (Exception e) {
            Scope.getCurrentScope().getLog(getClass()).warning("Probably expected error dropping databasechangelog table");
            e.printStackTrace();
            database.rollback();
        } finally {
            if (statement != null) {
                statement.close();
            }
        }
        // Now drop the DATABASECHANGELOGLOCK table (if it exists)
        try {
            if (SnapshotGeneratorFactory.getInstance().has(new Table().setName(database.getDatabaseChangeLogLockTableName()).setSchema(new Schema(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName())), database)) {
                statement = ((JdbcConnection) database.getConnection()).getUnderlyingConnection().createStatement();
                String sql = "DROP TABLE " + database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogLockTableName());
                for (SqlListener listener : Scope.getCurrentScope().getListeners(SqlListener.class)) {
                    listener.writeSqlWillRun(sql);
                }
                statement.execute(sql);
                database.commit();
            }
        } catch (Exception e) {
            Scope.getCurrentScope().getLog(getClass()).warning("Probably expected error dropping databasechangeloglock table");
            e.printStackTrace();
            database.rollback();
        } finally {
            if (statement != null) {
                statement.close();
            }
        }
    } catch (SQLException e) {
        throw new DatabaseException(e);
    }
    SnapshotGeneratorFactory.resetAll();
    DatabaseFactory.reset();
}
Also used : SqlListener(liquibase.listener.SqlListener) SQLException(java.sql.SQLException) InsertExecutablePreparedStatement(liquibase.statement.InsertExecutablePreparedStatement) CreateTableStatement(liquibase.statement.core.CreateTableStatement) DropTableStatement(liquibase.statement.core.DropTableStatement) SqlStatement(liquibase.statement.SqlStatement) Statement(java.sql.Statement) JdbcConnection(liquibase.database.jvm.JdbcConnection) DatabaseException(liquibase.exception.DatabaseException) ValidationFailedException(liquibase.exception.ValidationFailedException) ChangeLogParseException(liquibase.exception.ChangeLogParseException) SQLException(java.sql.SQLException) DatabaseException(liquibase.exception.DatabaseException) LiquibaseException(liquibase.exception.LiquibaseException)

Example 8 with SqlListener

use of liquibase.listener.SqlListener 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 9 with SqlListener

use of liquibase.listener.SqlListener in project liquibase by liquibase.

the class AbstractIntegrationTest method testRunUpdateOnOldChangelogTableFormat.

@Test
// Successful execution qualifies as test success.
@SuppressWarnings("squid:S2699")
public void testRunUpdateOnOldChangelogTableFormat() throws Exception {
    assumeNotNull(this.getDatabase());
    Liquibase liquibase = createLiquibase(completeChangeLog);
    clearDatabase();
    String nullableKeyword = database.requiresExplicitNullForColumns() ? " NULL" : "";
    String sql = "CREATE TABLE " + database.escapeTableName(database.getDefaultCatalogName(), database.getDefaultSchemaName(), "DATABASECHANGELOG") + " (id varchar(150) NOT NULL, " + "author VARCHAR(150) NOT NULL, " + "filename VARCHAR(255) NOT NULL, " + "dateExecuted " + DataTypeFactory.getInstance().fromDescription("datetime", database).toDatabaseDataType(database) + " NOT NULL, " + "md5sum VARCHAR(32)" + nullableKeyword + ", " + "description VARCHAR(255)" + nullableKeyword + ", " + "comments VARCHAR(255)" + nullableKeyword + ", " + "tag VARCHAR(255)" + nullableKeyword + ", " + "liquibase VARCHAR(10)" + nullableKeyword + ", " + "PRIMARY KEY (id, author, filename))";
    for (SqlListener listener : Scope.getCurrentScope().getListeners(SqlListener.class)) {
        listener.writeSqlWillRun(sql);
    }
    Connection conn = ((JdbcConnection) database.getConnection()).getUnderlyingConnection();
    boolean savedAcSetting = conn.getAutoCommit();
    conn.setAutoCommit(false);
    conn.createStatement().execute(sql);
    conn.commit();
    conn.setAutoCommit(savedAcSetting);
    liquibase = createLiquibase(completeChangeLog);
    liquibase.setChangeLogParameter("loginuser", testSystem.getUsername());
    liquibase.update(this.contexts);
}
Also used : SqlListener(liquibase.listener.SqlListener) Connection(java.sql.Connection) DatabaseConnection(liquibase.database.DatabaseConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection) Test(org.junit.Test)

Aggregations

SqlListener (liquibase.listener.SqlListener)9 DatabaseException (liquibase.exception.DatabaseException)8 SQLException (java.sql.SQLException)7 Statement (java.sql.Statement)6 JdbcConnection (liquibase.database.jvm.JdbcConnection)5 Database (liquibase.database.Database)2 ChangeLogParseException (liquibase.exception.ChangeLogParseException)2 LiquibaseException (liquibase.exception.LiquibaseException)2 ValidationFailedException (liquibase.exception.ValidationFailedException)2 SqlStatement (liquibase.statement.SqlStatement)2 Connection (java.sql.Connection)1 ColumnConfig (liquibase.change.ColumnConfig)1 DatabaseConnection (liquibase.database.DatabaseConnection)1 MockDatabase (liquibase.database.core.MockDatabase)1 PostgresDatabase (liquibase.database.core.PostgresDatabase)1 UnsupportedDatabase (liquibase.database.core.UnsupportedDatabase)1 ExampleCustomDatabase (liquibase.database.example.ExampleCustomDatabase)1 LockException (liquibase.exception.LockException)1 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)1 DatabaseTestSystem (liquibase.extension.testing.testsystem.DatabaseTestSystem)1