step.migration.MigrationManager Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (C) 2020, exense GmbH
*
* This file is part of STEP
*
* STEP is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* STEP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with STEP. If not, see .
******************************************************************************/
package step.migration;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import step.core.GlobalContext;
import step.core.Version;
public class MigrationManager {
private static final Logger logger = LoggerFactory.getLogger(MigrationManager.class);
protected final GlobalContext context;
protected List migrators = new ArrayList<>();
public MigrationManager(GlobalContext context) {
super();
this.context = context;
}
/**
* Register a new migration task
*
* @param migrationTask the task to be registered
*/
public void register(MigrationTask migrationTask) {
migrationTask.setContext(context);
migrators.add(migrationTask);
}
/**
* Runs all the migration tasks registered to migrate
*
* @param from the initial version
* @param to the new version
* @return true if the migration ran successfully
*/
public boolean migrate(Version from, Version to) {
logger.info("Migrating from "+from+" to "+to);
AtomicBoolean successful = new AtomicBoolean(true);
List matchedMigrators = new ArrayList<>();
boolean upgrade = to.compareTo(from)>=1;
for(MigrationTask migrator:migrators) {
if(migrator.asOfVersion.compareTo(upgrade?from:to)>=1 && migrator.asOfVersion.compareTo(upgrade?to:from)<=0) {
matchedMigrators.add(migrator);
}
}
matchedMigrators.sort(new Comparator() {
@Override
public int compare(MigrationTask o1, MigrationTask o2) {
return (upgrade?1:-1)*o1.asOfVersion.compareTo(o2.asOfVersion);
}
});
matchedMigrators.forEach(m->{
logger.info("Running migration task "+m);
long t1 = System.currentTimeMillis();
try {
if(upgrade) {
m.runUpgradeScript();
} else {
m.runDowngradeScript();
}
logger.info("Migration task "+m+" successfully executed in "+(System.currentTimeMillis()-t1)+"ms");
} catch(Exception e) {
logger.error("Error while running upgrade/downgrade script "+m, e);
successful.set(false);
}
});
return successful.get();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy