migratory-core/src/main/java/com/nesscomputing/migratory/InternalMigrator.java
private List<MigrationResult> migratePersonality(final MetadataManager metadataManager, final String personalityName, final Integer targetVersion, final MigratoryOption [] options)
    {
        final Integer currentVersion = metadataManager.getCurrentVersion(personalityName);

        if (currentVersion == null  && !migratoryConfig.isCreatePersonalities()) {
            throw new MigratoryException(Reason.NEW_PERSONALITIES_DENIED);
        }

        // Make sure that the current state of the personality is sane.
        final List<MetadataInfo> history = metadataManager.getPersonalityHistory(personalityName);

        final MigrationManager migrationManager = new MigrationManager(migratoryContext, personalityName);

        // if null, this is a new personality.Don't validate it.
        if (history != null && !history.isEmpty()) {
            // "No verify" option skips this step.
            if (!MigratoryOption.containsOption(MigratoryOption.NO_VERIFY, options)) {
                final DbValidator dbValidator = new DbValidator(migrationManager);
                final ValidationResult validationResult = dbValidator.validate(history);

                if (validationResult.getValidationStatus() != ValidationStatus.OK) {
                    throw new MigratoryException(Reason.VALIDATION_FAILED, "Validation for Personality '%s' failed", personalityName);
                }
            }
            else {
                LOG.info("Skipped verification.");
            }
        }

        final MigrationPlanner migrationPlanner = new MigrationPlanner(migrationManager, currentVersion, targetVersion);

        migrationPlanner.plan();

        LOG.info("{}", migrationPlanner.toString());
        switch(migrationPlanner.getDirection()) {
        case FORWARD:
            if (!migratoryConfig.isAllowRollForward()) {
                throw new MigratoryException(Reason.ROLL_FORWARD_DENIED);
            }
            break;

        case BACK:
            if (!migratoryConfig.isAllowRollBack()) {
                throw new MigratoryException(Reason.ROLL_BACK_DENIED);
            }
            break;

        case DO_NOTHING:
            return null;

        default:
            LOG.warn("Encountered State {}. This should never happen!", migrationPlanner.getDirection());
            return null;
        }

        final DbMigrator migrator = new DbMigrator(migratoryContext, migrationPlanner);
        final List<MigrationResult> results = migrator.migrate(options);
        LOG.info("Migration finished in '{}' steps, result is {}",  results.size(), MigrationResult.determineMigrationState(results));
        return results;
    }
private List<MigrationResult> migratePersonality(final MetadataManager metadataManager, final String personalityName, final Integer targetVersion, final MigratoryOption [] options)
    {
        final Integer currentVersion = metadataManager.getCurrentVersion(personalityName);

        if (currentVersion == null  && !migratoryConfig.isCreatePersonalities()) {
            throw new MigratoryException(Reason.NEW_PERSONALITIES_DENIED);
        }

        // Make sure that the current state of the personality is sane.
        final List<MetadataInfo> history = metadataManager.getPersonalityHistory(personalityName);

        final MigrationManager migrationManager = new MigrationManager(migratoryContext, personalityName);

        // if null, this is a new personality.Don't validate it.
        if (history != null && !history.isEmpty()) {
            // "No verify" option skips this step.
            if (!MigratoryOption.containsOption(MigratoryOption.NO_VERIFY, options)) {
                final DbValidator dbValidator = new DbValidator(migrationManager);
                final ValidationResult validationResult = dbValidator.validate(history);

                if (validationResult.getValidationStatus() != ValidationStatus.OK) {
                    throw new MigratoryException(Reason.VALIDATION_FAILED, "Validation for Personality '%s' failed", personalityName);
                }
            }
            else {
                LOG.info("Skipped verification.");
            }
        }

        final MigrationPlanner migrationPlanner = new MigrationPlanner(migrationManager, currentVersion, targetVersion);

        migrationPlanner.plan();

        LOG.info("{}", migrationPlanner.toString());
        switch(migrationPlanner.getDirection()) {
        case FORWARD:
            if (!migratoryConfig.isAllowRollForward()) {
                throw new MigratoryException(Reason.ROLL_FORWARD_DENIED);
            }
            break;

        case BACK:
            if (!migratoryConfig.isAllowRollBack()) {
                throw new MigratoryException(Reason.ROLL_BACK_DENIED);
            }
            break;

        case DO_NOTHING:
            return null;

        default:
            LOG.warn("Encountered State {}. This should never happen!", migrationPlanner.getDirection());
            return null;
        }

        final DbMigrator migrator = new DbMigrator(migratoryContext, migrationPlanner);
        final List<MigrationResult> results = migrator.migrate(options);
        LOG.info("Migration finished in '{}' steps, result is {}",  results.size(), MigrationResult.determineMigrationState(results));
        return results;
    }
Performs the migration of a personality. This must be run under the table lock so that only one thread can use it at a time.