Search in sources :

Example 6 with StopWatch

use of org.flywaydb.core.internal.util.StopWatch in project flyway by flyway.

the class DbValidate method validate.

/**
     * Starts the actual migration.
     *
     * @return The validation error, if any.
     */
public String validate() {
    if (!schema.exists()) {
        if (!migrationResolver.resolveMigrations().isEmpty() && !pending) {
            return "Schema " + schema + " doesn't exist yet";
        }
        return null;
    }
    try {
        for (final FlywayCallback callback : callbacks) {
            new TransactionTemplate(connection).execute(new Callable<Object>() {

                @Override
                public Object call() throws SQLException {
                    dbSupport.changeCurrentSchemaTo(schema);
                    callback.beforeValidate(connection);
                    return null;
                }
            });
        }
        LOG.debug("Validating migrations ...");
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        Pair<Integer, String> result = new TransactionTemplate(connection).execute(new Callable<Pair<Integer, String>>() {

            @Override
            public Pair<Integer, String> call() {
                dbSupport.changeCurrentSchemaTo(schema);
                MigrationInfoServiceImpl migrationInfoService = new MigrationInfoServiceImpl(migrationResolver, metaDataTable, target, outOfOrder, pending, missing, future);
                migrationInfoService.refresh();
                int count = migrationInfoService.all().length;
                String validationError = migrationInfoService.validate();
                return Pair.of(count, validationError);
            }
        });
        stopWatch.stop();
        String error = result.getRight();
        if (error == null) {
            int count = result.getLeft();
            if (count == 1) {
                LOG.info(String.format("Successfully validated 1 migration (execution time %s)", TimeFormat.format(stopWatch.getTotalTimeMillis())));
            } else {
                LOG.info(String.format("Successfully validated %d migrations (execution time %s)", count, TimeFormat.format(stopWatch.getTotalTimeMillis())));
            }
        }
        for (final FlywayCallback callback : callbacks) {
            new TransactionTemplate(connection).execute(new Callable<Object>() {

                @Override
                public Object call() throws SQLException {
                    dbSupport.changeCurrentSchemaTo(schema);
                    callback.afterValidate(connection);
                    return null;
                }
            });
        }
        return error;
    } finally {
        dbSupport.restoreCurrentSchema();
    }
}
Also used : MigrationInfoServiceImpl(org.flywaydb.core.internal.info.MigrationInfoServiceImpl) SQLException(java.sql.SQLException) TransactionTemplate(org.flywaydb.core.internal.util.jdbc.TransactionTemplate) StopWatch(org.flywaydb.core.internal.util.StopWatch) FlywayCallback(org.flywaydb.core.api.callback.FlywayCallback) Pair(org.flywaydb.core.internal.util.Pair)

Aggregations

StopWatch (org.flywaydb.core.internal.util.StopWatch)6 TransactionTemplate (org.flywaydb.core.internal.util.jdbc.TransactionTemplate)6 SQLException (java.sql.SQLException)4 FlywayCallback (org.flywaydb.core.api.callback.FlywayCallback)3 FlywayException (org.flywaydb.core.api.FlywayException)2 MigrationVersion (org.flywaydb.core.api.MigrationVersion)2 MigrationInfoServiceImpl (org.flywaydb.core.internal.info.MigrationInfoServiceImpl)2 MigrationExecutor (org.flywaydb.core.api.resolver.MigrationExecutor)1 FlywaySqlException (org.flywaydb.core.internal.dbsupport.FlywaySqlException)1 AppliedMigration (org.flywaydb.core.internal.metadatatable.AppliedMigration)1 Pair (org.flywaydb.core.internal.util.Pair)1