Search in sources :

Example 1 with ValidateOutput

use of org.flywaydb.core.api.output.ValidateOutput in project flyway by flyway.

the class DbValidate method validate.

/**
 * Starts the actual migration.
 *
 * @return The validation error, if any.
 */
public ValidateResult validate() {
    if (!schema.exists()) {
        if (!migrationResolver.resolveMigrations(new Context() {

            @Override
            public Configuration getConfiguration() {
                return configuration;
            }
        }).isEmpty() && !pending) {
            String validationErrorMessage = "Schema " + schema + " doesn't exist yet";
            ErrorDetails validationError = new ErrorDetails(ErrorCode.SCHEMA_DOES_NOT_EXIST, validationErrorMessage);
            return CommandResultFactory.createValidateResult(database.getCatalog(), validationError, 0, null, new ArrayList<>());
        }
        return CommandResultFactory.createValidateResult(database.getCatalog(), null, 0, null, new ArrayList<>());
    }
    callbackExecutor.onEvent(Event.BEFORE_VALIDATE);
    LOG.debug("Validating migrations ...");
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    Pair<Integer, List<ValidateOutput>> result = ExecutionTemplateFactory.createExecutionTemplate(connection.getJdbcConnection(), database).execute(new Callable<Pair<Integer, List<ValidateOutput>>>() {

        @Override
        public Pair<Integer, List<ValidateOutput>> call() {
            MigrationInfoServiceImpl migrationInfoService = new MigrationInfoServiceImpl(migrationResolver, schemaHistory, database, configuration, configuration.getTarget(), configuration.isOutOfOrder(), configuration.getCherryPick(), pending, configuration.isIgnoreMissingMigrations(), configuration.isIgnoreIgnoredMigrations(), configuration.isIgnoreFutureMigrations());
            migrationInfoService.refresh();
            int count = migrationInfoService.all().length;
            List<ValidateOutput> invalidMigrations = migrationInfoService.validate();
            return Pair.of(count, invalidMigrations);
        }
    });
    stopWatch.stop();
    List<String> warnings = new ArrayList<>();
    List<ValidateOutput> invalidMigrations = result.getRight();
    ErrorDetails validationError = null;
    int count = 0;
    if (invalidMigrations.isEmpty()) {
        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())));
            if (count == 0) {
                String noMigrationsWarning = "No migrations found. Are your locations set up correctly?";
                warnings.add(noMigrationsWarning);
                LOG.warn(noMigrationsWarning);
            }
        }
        callbackExecutor.onEvent(Event.AFTER_VALIDATE);
    } else {
        validationError = new ErrorDetails(ErrorCode.VALIDATE_ERROR, "Migrations have failed validation");
        callbackExecutor.onEvent(Event.AFTER_VALIDATE_ERROR);
    }
    return CommandResultFactory.createValidateResult(database.getCatalog(), validationError, count, invalidMigrations, warnings);
}
Also used : Context(org.flywaydb.core.api.resolver.Context) MigrationInfoServiceImpl(org.flywaydb.core.internal.info.MigrationInfoServiceImpl) ArrayList(java.util.ArrayList) ErrorDetails(org.flywaydb.core.api.ErrorDetails) StopWatch(org.flywaydb.core.internal.util.StopWatch) ValidateOutput(org.flywaydb.core.api.output.ValidateOutput) ArrayList(java.util.ArrayList) List(java.util.List) Pair(org.flywaydb.core.internal.util.Pair)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 ErrorDetails (org.flywaydb.core.api.ErrorDetails)1 ValidateOutput (org.flywaydb.core.api.output.ValidateOutput)1 Context (org.flywaydb.core.api.resolver.Context)1 MigrationInfoServiceImpl (org.flywaydb.core.internal.info.MigrationInfoServiceImpl)1 Pair (org.flywaydb.core.internal.util.Pair)1 StopWatch (org.flywaydb.core.internal.util.StopWatch)1