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.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import step.core.Version;
import step.core.collections.CollectionFactory;
public class MigrationManager {
private static final Logger logger = LoggerFactory.getLogger(MigrationManager.class);
private final List> migrationTasks = new ArrayList<>();
private final Map, Object> bindings = new HashMap<>();
public MigrationManager() {
super();
}
public void addBinding(Class class_, T object) {
bindings.put(class_, object);
}
/**
* Register a new migration task
*
* @param migrationTask the task to be registered
*/
public void register(Class extends MigrationTask> migrationTask) {
migrationTasks.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
*/
@SuppressWarnings("unchecked")
public boolean migrate(CollectionFactory collectionFactory, Version from, Version to) {
final MigrationContext migrationContext = new MigrationContext();
bindings.forEach((k, v) -> migrationContext.put((Class