Search in sources :

Example 21 with FlywayException

use of org.flywaydb.core.api.FlywayException in project flyway by flyway.

the class OracleSchema method executeAlterStatementsForFlashbackTables.

/**
     * Executes ALTER statements for all tables that have Flashback enabled.
     * Flashback is an asynchronous process so we need to wait until it completes, otherwise cleaning the
     * tables in schema will sometimes fail with ORA-55622 or ORA-55610 depending on the race between
     * Flashback and Java code
     *
     * @throws SQLException when the statements could not be generated.
     */
private void executeAlterStatementsForFlashbackTables() throws SQLException {
    List<String> tableNames = jdbcTemplate.queryForStringList("SELECT table_name " + "FROM DBA_FLASHBACK_ARCHIVE_TABLES WHERE owner_name = ?", name);
    for (String tableName : tableNames) {
        jdbcTemplate.execute("ALTER TABLE " + dbSupport.quote(name, tableName) + " NO FLASHBACK ARCHIVE");
        String queryForOracleTechnicalTables = "SELECT count(archive_table_name) " + "FROM user_flashback_archive_tables " + "WHERE table_name = ?";
        //wait until the tables disappear
        while (jdbcTemplate.queryForInt(queryForOracleTechnicalTables, tableName) > 0) {
            try {
                LOG.debug("Actively waiting for Flashback cleanup on table: " + tableName);
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new FlywayException("Waiting for Flashback cleanup interrupted", e);
            }
        }
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException)

Example 22 with FlywayException

use of org.flywaydb.core.api.FlywayException in project flyway by flyway.

the class SqlScript method readLines.

/**
     * Parses the textual data provided by this reader into a list of lines.
     *
     * @param reader The reader for the textual data.
     * @return The list of lines (in order).
     * @throws IllegalStateException Thrown when the textual data parsing failed.
     */
private List<String> readLines(Reader reader) {
    List<String> lines = new ArrayList<String>();
    BufferedReader bufferedReader = new BufferedReader(reader);
    String line;
    try {
        while ((line = bufferedReader.readLine()) != null) {
            lines.add(line);
        }
    } catch (IOException e) {
        String message = resource == null ? "Unable to parse lines" : "Unable to parse " + resource.getLocation() + " (" + resource.getLocationOnDisk() + ")";
        throw new FlywayException(message, e);
    }
    return lines;
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException)

Example 23 with FlywayException

use of org.flywaydb.core.api.FlywayException in project flyway by flyway.

the class DbMigrate method applyMigration.

/**
     * Applies this migration to the database. The migration state and the execution time are updated accordingly.
     *
     * @param migration    The migration to apply.
     * @param isOutOfOrder If this migration is being applied out of order.
     * @return The result of the migration.
     */
private Boolean applyMigration(final MigrationInfoImpl migration, boolean isOutOfOrder) {
    MigrationVersion version = migration.getVersion();
    final MigrationExecutor migrationExecutor = migration.getResolvedMigration().getExecutor();
    final String migrationText;
    if (version != null) {
        migrationText = "schema " + schema + " to version " + version + " - " + migration.getDescription() + (isOutOfOrder ? " [out of order]" : "") + (migrationExecutor.executeInTransaction() ? "" : " [non-transactional]");
    } else {
        migrationText = "schema " + schema + " with repeatable migration " + migration.getDescription() + (migrationExecutor.executeInTransaction() ? "" : " [non-transactional]");
    }
    LOG.info("Migrating " + migrationText);
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    try {
        if (migrationExecutor.executeInTransaction()) {
            new TransactionTemplate(connectionUserObjects).execute(new Callable<Object>() {

                @Override
                public Object call() throws SQLException {
                    doMigrate(migration, migrationExecutor, migrationText);
                    return null;
                }
            });
        } else {
            try {
                doMigrate(migration, migrationExecutor, migrationText);
            } catch (SQLException e) {
                throw new FlywaySqlException("Unable to apply migration", e);
            }
        }
    } catch (FlywayException e) {
        String failedMsg = "Migration of " + migrationText + " failed!";
        if (dbSupport.supportsDdlTransactions() && migrationExecutor.executeInTransaction()) {
            LOG.error(failedMsg + " Changes successfully rolled back.");
        } else {
            LOG.error(failedMsg + " Please restore backups and roll back database and code!");
            stopWatch.stop();
            int executionTime = (int) stopWatch.getTotalTimeMillis();
            AppliedMigration appliedMigration = new AppliedMigration(version, migration.getDescription(), migration.getType(), migration.getScript(), migration.getResolvedMigration().getChecksum(), executionTime, false);
            metaDataTable.addAppliedMigration(appliedMigration);
        }
        throw e;
    }
    stopWatch.stop();
    int executionTime = (int) stopWatch.getTotalTimeMillis();
    AppliedMigration appliedMigration = new AppliedMigration(version, migration.getDescription(), migration.getType(), migration.getScript(), migration.getResolvedMigration().getChecksum(), executionTime, true);
    metaDataTable.addAppliedMigration(appliedMigration);
    return false;
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) FlywaySqlException(org.flywaydb.core.internal.dbsupport.FlywaySqlException) SQLException(java.sql.SQLException) MigrationExecutor(org.flywaydb.core.api.resolver.MigrationExecutor) TransactionTemplate(org.flywaydb.core.internal.util.jdbc.TransactionTemplate) StopWatch(org.flywaydb.core.internal.util.StopWatch) MigrationVersion(org.flywaydb.core.api.MigrationVersion) AppliedMigration(org.flywaydb.core.internal.metadatatable.AppliedMigration)

Example 24 with FlywayException

use of org.flywaydb.core.api.FlywayException in project flyway by flyway.

the class DbSupportFactory method getDatabaseProductVersion.

/**
     * Retrieves the database version.
     *
     * @param connection The connection to use to query the database.
     * @return The version of the database product.
     * Ex.: DSN11015 DB2 for z/OS Version 11
     * SQL10050 DB" for Linux, UNIX and Windows Version 10.5
     */
private static String getDatabaseProductVersion(Connection connection) {
    try {
        DatabaseMetaData databaseMetaData = connection.getMetaData();
        if (databaseMetaData == null) {
            throw new FlywayException("Unable to read database metadata while it is null!");
        }
        String databaseProductVersion = databaseMetaData.getDatabaseProductVersion();
        if (databaseProductVersion == null) {
            throw new FlywayException("Unable to determine database. Product version is null.");
        }
        return databaseProductVersion;
    } catch (SQLException e) {
        throw new FlywaySqlException("Error while determining database product version", e);
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) SQLException(java.sql.SQLException) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 25 with FlywayException

use of org.flywaydb.core.api.FlywayException in project flyway by flyway.

the class AndroidScanner method scanForClasses.

public Class<?>[] scanForClasses(Location location, Class<?> implementedInterface) throws Exception {
    String pkg = location.getPath().replace("/", ".");
    List<Class> classes = new ArrayList<Class>();
    DexFile dex = new DexFile(context.getApplicationInfo().sourceDir);
    Enumeration<String> entries = dex.entries();
    while (entries.hasMoreElements()) {
        String className = entries.nextElement();
        if (className.startsWith(pkg)) {
            Class<?> clazz = classLoader.loadClass(className);
            if (Modifier.isAbstract(clazz.getModifiers())) {
                LOG.debug("Skipping abstract class: " + className);
                continue;
            }
            if (!implementedInterface.isAssignableFrom(clazz)) {
                continue;
            }
            try {
                ClassUtils.instantiate(className, classLoader);
            } catch (Exception e) {
                throw new FlywayException("Unable to instantiate class: " + className);
            }
            classes.add(clazz);
            LOG.debug("Found class: " + className);
        }
    }
    return classes.toArray(new Class<?>[classes.size()]);
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) ArrayList(java.util.ArrayList) DexFile(dalvik.system.DexFile) FlywayException(org.flywaydb.core.api.FlywayException)

Aggregations

FlywayException (org.flywaydb.core.api.FlywayException)42 IOException (java.io.IOException)13 SQLException (java.sql.SQLException)11 ArrayList (java.util.ArrayList)6 MigrationVersion (org.flywaydb.core.api.MigrationVersion)6 Test (org.junit.Test)6 File (java.io.File)5 InputStreamReader (java.io.InputStreamReader)5 ResolvedMigrationImpl (org.flywaydb.core.internal.resolver.ResolvedMigrationImpl)5 FileInputStream (java.io.FileInputStream)4 ResolvedMigration (org.flywaydb.core.api.resolver.ResolvedMigration)4 StringReader (java.io.StringReader)3 Method (java.lang.reflect.Method)3 URL (java.net.URL)3 DatabaseMetaData (java.sql.DatabaseMetaData)3 HashMap (java.util.HashMap)3 Properties (java.util.Properties)3 FlywayCallback (org.flywaydb.core.api.callback.FlywayCallback)3 DriverDataSource (org.flywaydb.core.internal.util.jdbc.DriverDataSource)3 TransactionTemplate (org.flywaydb.core.internal.util.jdbc.TransactionTemplate)3