Search in sources :

Example 36 with FlywayException

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

the class DbMigrate method migrate.

/**
     * Starts the actual migration.
     *
     * @return The number of successfully applied migrations.
     * @throws FlywayException when migration failed.
     */
public int migrate() throws FlywayException {
    try {
        for (final FlywayCallback callback : configuration.getCallbacks()) {
            new TransactionTemplate(connectionUserObjects).execute(new Callable<Object>() {

                @Override
                public Object call() throws SQLException {
                    dbSupportUserObjects.changeCurrentSchemaTo(schema);
                    callback.beforeMigrate(connectionUserObjects);
                    return null;
                }
            });
        }
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        int migrationSuccessCount = 0;
        while (true) {
            final boolean firstRun = migrationSuccessCount == 0;
            boolean done = metaDataTable.lock(new Callable<Boolean>() {

                @Override
                public Boolean call() {
                    MigrationInfoServiceImpl infoService = new MigrationInfoServiceImpl(migrationResolver, metaDataTable, configuration.getTarget(), configuration.isOutOfOrder(), true, true, true);
                    infoService.refresh();
                    MigrationVersion currentSchemaVersion = MigrationVersion.EMPTY;
                    if (infoService.current() != null) {
                        currentSchemaVersion = infoService.current().getVersion();
                    }
                    if (firstRun) {
                        LOG.info("Current version of schema " + schema + ": " + currentSchemaVersion);
                        if (configuration.isOutOfOrder()) {
                            LOG.warn("outOfOrder mode is active. Migration of schema " + schema + " may not be reproducible.");
                        }
                    }
                    MigrationInfo[] future = infoService.future();
                    if (future.length > 0) {
                        MigrationInfo[] resolved = infoService.resolved();
                        if (resolved.length == 0) {
                            LOG.warn("Schema " + schema + " has version " + currentSchemaVersion + ", but no migration could be resolved in the configured locations !");
                        } else {
                            int offset = resolved.length - 1;
                            while (resolved[offset].getVersion() == null) {
                                // Skip repeatable migrations
                                offset--;
                            }
                            LOG.warn("Schema " + schema + " has a version (" + currentSchemaVersion + ") that is newer than the latest available migration (" + resolved[offset].getVersion() + ") !");
                        }
                    }
                    MigrationInfo[] failed = infoService.failed();
                    if (failed.length > 0) {
                        if ((failed.length == 1) && (failed[0].getState() == MigrationState.FUTURE_FAILED) && (configuration.isIgnoreFutureMigrations() || ignoreFailedFutureMigration)) {
                            LOG.warn("Schema " + schema + " contains a failed future migration to version " + failed[0].getVersion() + " !");
                        } else {
                            if (failed[0].getVersion() == null) {
                                throw new FlywayException("Schema " + schema + " contains a failed repeatable migration (" + failed[0].getDescription() + ") !");
                            }
                            throw new FlywayException("Schema " + schema + " contains a failed migration to version " + failed[0].getVersion() + " !");
                        }
                    }
                    MigrationInfoImpl[] pendingMigrations = infoService.pending();
                    if (pendingMigrations.length == 0) {
                        return true;
                    }
                    boolean isOutOfOrder = pendingMigrations[0].getVersion() != null && pendingMigrations[0].getVersion().compareTo(currentSchemaVersion) < 0;
                    return applyMigration(pendingMigrations[0], isOutOfOrder);
                }
            });
            if (done) {
                // No further migrations available
                break;
            }
            migrationSuccessCount++;
        }
        stopWatch.stop();
        logSummary(migrationSuccessCount, stopWatch.getTotalTimeMillis());
        for (final FlywayCallback callback : configuration.getCallbacks()) {
            new TransactionTemplate(connectionUserObjects).execute(new Callable<Object>() {

                @Override
                public Object call() throws SQLException {
                    dbSupportUserObjects.changeCurrentSchemaTo(schema);
                    callback.afterMigrate(connectionUserObjects);
                    return null;
                }
            });
        }
        return migrationSuccessCount;
    } finally {
        dbSupportUserObjects.restoreCurrentSchema();
    }
}
Also used : MigrationInfoServiceImpl(org.flywaydb.core.internal.info.MigrationInfoServiceImpl) FlywayException(org.flywaydb.core.api.FlywayException) SQLException(java.sql.SQLException) TransactionTemplate(org.flywaydb.core.internal.util.jdbc.TransactionTemplate) StopWatch(org.flywaydb.core.internal.util.StopWatch) MigrationVersion(org.flywaydb.core.api.MigrationVersion) FlywayCallback(org.flywaydb.core.api.callback.FlywayCallback)

Example 37 with FlywayException

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

the class DbSupportFactory method getDatabaseProductName.

/**
     * Retrieves the name of the database product.
     *
     * @param connection The connection to use to query the database.
     * @return The name of the database product. Ex.: Oracle, MySQL, ...
     */
private static String getDatabaseProductName(Connection connection) {
    try {
        DatabaseMetaData databaseMetaData = connection.getMetaData();
        if (databaseMetaData == null) {
            throw new FlywayException("Unable to read database metadata while it is null!");
        }
        String databaseProductName = databaseMetaData.getDatabaseProductName();
        if (databaseProductName == null) {
            throw new FlywayException("Unable to determine database. Product name is null.");
        }
        int databaseMajorVersion = databaseMetaData.getDatabaseMajorVersion();
        int databaseMinorVersion = databaseMetaData.getDatabaseMinorVersion();
        return databaseProductName + " " + databaseMajorVersion + "." + databaseMinorVersion;
    } catch (SQLException e) {
        throw new FlywaySqlException("Error while determining database product name", e);
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) SQLException(java.sql.SQLException) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 38 with FlywayException

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

the class DbSupportFactory method getDriverName.

/**
     * Retrieves the name of the JDBC driver
     *
     * @param connection The connection to use to query the database.
     * @return The name of the driver. Ex: RedshiftJDBC
     */
private static String getDriverName(Connection connection) {
    try {
        DatabaseMetaData databaseMetaData = connection.getMetaData();
        if (databaseMetaData == null) {
            throw new FlywayException("Unable to read database metadata while it is null!");
        }
        String driverName = databaseMetaData.getDriverName();
        if (driverName == null) {
            throw new FlywayException("Unable to determine JDBC  driver name. JDBC driver name is null.");
        }
        return driverName;
    } catch (SQLException e) {
        throw new FlywaySqlException("Error while determining JDBC driver name", e);
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) SQLException(java.sql.SQLException) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 39 with FlywayException

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

the class Flyway method doValidate.

/**
     * Performs the actual validation. All set up must have taken place beforehand.
     *
     * @param connectionMetaDataTable The database connection for the metadata table.
     * @param dbSupport               The database-specific support.
     * @param migrationResolver       The migration resolver;
     * @param metaDataTable           The metadata table.
     * @param schemas                 The schemas managed by Flyway.
     * @param pending                 Whether pending migrations are ok.
     */
private void doValidate(Connection connectionMetaDataTable, DbSupport dbSupport, MigrationResolver migrationResolver, MetaDataTable metaDataTable, Schema[] schemas, FlywayCallback[] flywayCallbacks, boolean pending) {
    String validationError = new DbValidate(connectionMetaDataTable, dbSupport, metaDataTable, schemas[0], migrationResolver, target, outOfOrder, pending, ignoreMissingMigrations, ignoreFutureMigrations, flywayCallbacks).validate();
    if (validationError != null) {
        if (cleanOnValidationError) {
            new DbClean(connectionMetaDataTable, dbSupport, metaDataTable, schemas, flywayCallbacks, cleanDisabled).clean();
            metaDataTable.clearCache();
        } else {
            throw new FlywayException("Validate failed: " + validationError);
        }
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) DbValidate(org.flywaydb.core.internal.command.DbValidate) DbClean(org.flywaydb.core.internal.command.DbClean)

Example 40 with FlywayException

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

the class AbstractFlywayMojo method getConfigFileProperties.

/**
     * Retrieve the properties from the config file (if specified).
     */
private Properties getConfigFileProperties() throws IOException {
    Properties properties = new Properties();
    String configFileProp = System.getProperty("flyway.configFile");
    if (configFileProp != null) {
        configFile = new File(configFileProp);
        if (!configFile.isAbsolute()) {
            configFile = new File(mavenProject.getBasedir(), configFileProp);
        }
    }
    if (configFile == null) {
        File file = new File(mavenProject.getBasedir(), "flyway.properties");
        if (file.isFile() && file.canRead()) {
            configFile = file;
        } else {
            log.debug("flyway.properties not found. Skipping.");
            return properties;
        }
    } else if (!configFile.canRead() || !configFile.isFile()) {
        throw new FlywayException("Unable to read config file: " + configFile.getAbsolutePath());
    }
    properties.load(new InputStreamReader(new FileInputStream(configFile), encoding));
    return properties;
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) InputStreamReader(java.io.InputStreamReader) Properties(java.util.Properties) File(java.io.File) FileInputStream(java.io.FileInputStream)

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