protected MigrationPlan createMigrationPlan(final Map.Entry<String, String> database) throws MojoExecutionException { final Map<String, MigrationInformation> availableMigrations = getAvailableMigrations(database.getKey()); final MigrationPlan migrationPlan = new MigrationPlan(); // Do we have any special migrations given? final String migrations = database.getValue(); if (StringUtils.isEmpty(migrations)) { for (MigrationInformation availableMigration : availableMigrations.values()) { migrationPlan.addMigration( availableMigration.getName(), Integer.MAX_VALUE, availableMigration.getPriority()); } return migrationPlan; // No } final String[] migrationNames = StringUtils.stripAll(StringUtils.split(migrations, "/")); for (String migrationName : migrationNames) { final String[] migrationFields = StringUtils.stripAll(StringUtils.split(migrationName, "@")); if (migrationFields == null || migrationFields.length < 1 || migrationFields.length > 2) { throw new MojoExecutionException("Migration " + migrationName + " is invalid."); } int targetVersion = migrationFields.length == 2 ? Integer.parseInt(migrationFields[1], 10) : Integer.MAX_VALUE; MigrationInformation migrationInformation = availableMigrations.get(migrationFields[0]); if (migrationInformation == null) { throw new MojoExecutionException("Migration " + migrationName + " is unknown!"); } migrationPlan.addMigration( migrationInformation.getName(), targetVersion, migrationInformation.getPriority()); } return migrationPlan; }
@Override protected void doExecute() throws Exception { final boolean permission = config.getBoolean(getPropertyName("permission.upgrade-db"), false); if (!permission) { throw new MojoExecutionException("No permission to run this task!"); } final Map<String, String> databases = extractDatabases(migrations); for (Map.Entry<String, String> database : databases.entrySet()) { final String databaseName = database.getKey(); final DBIConfig databaseConfig = getDBIConfigFor(databaseName); final DBI rootDbDbi = new DBI( databaseConfig.getDBUrl(), rootDBIConfig.getDBUser(), rootDBIConfig.getDBPassword()); final DBI dbi = getDBIFor(databaseName); try { final MigrationPlan rootMigrationPlan = createMigrationPlan(database); if (!rootMigrationPlan.isEmpty()) { LOG.info("Migrating {} ...", databaseName); Migratory migratory = new Migratory(migratoryConfig, dbi, rootDbDbi); migratory.addLocator(new MojoLocator(migratory, manifestUrl)); migratory.dbMigrate(rootMigrationPlan, optionList); } } catch (MigratoryException me) { LOG.warn( String.format( "While creating '%s': %s, Reason: %s", databaseName, me.getMessage(), me.getReason())); } LOG.info("... done"); } }