3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.platform.db.migration.engine;
22 import java.util.Arrays;
23 import java.util.HashSet;
25 import org.sonar.server.platform.db.migration.step.MigrationStep;
26 import org.sonar.server.platform.db.migration.step.MigrationSteps;
27 import org.sonar.server.platform.db.migration.step.MigrationStepsExecutor;
28 import org.sonar.server.platform.db.migration.step.MigrationStepsExecutorImpl;
29 import org.sonar.server.platform.db.migration.version.DbVersion;
34 * <li>adding all the {@link MigrationStep} classes to the container after building it</li>
35 * <li>adding dependencies for them to the container if there aren't already available in parent container
36 * (see {@link DbVersion#getSupportComponents()})</li>
37 * <li>adding the {@link MigrationStepsExecutorImpl} to the container</li>
40 public class MigrationContainerPopulatorImpl implements MigrationContainerPopulator {
41 private final DbVersion[] dbVersions;
42 private final Class<? extends MigrationStepsExecutor> executorType;
44 public MigrationContainerPopulatorImpl(DbVersion... dbVersions) {
45 this(MigrationStepsExecutorImpl.class, dbVersions);
48 protected MigrationContainerPopulatorImpl(Class<? extends MigrationStepsExecutor> executorType, DbVersion... dbVersions) {
49 this.dbVersions = dbVersions;
50 this.executorType = executorType;
54 public void populateContainer(MigrationContainer container) {
55 container.add(executorType);
56 populateFromDbVersion(container);
57 populateFromMigrationSteps(container);
60 private void populateFromDbVersion(MigrationContainer container) {
61 Arrays.stream(dbVersions)
62 .flatMap(DbVersion::getSupportComponents)
63 .forEach(container::add);
66 private static void populateFromMigrationSteps(MigrationContainer container) {
67 MigrationSteps migrationSteps = container.getComponentByType(MigrationSteps.class);
68 Set<Class<? extends MigrationStep>> classes = new HashSet<>();
69 migrationSteps.readAll().forEach(step -> {
70 Class<? extends MigrationStep> stepClass = step.getStepClass();
71 if (!classes.contains(stepClass)) {
72 container.add(stepClass);
73 classes.add(stepClass);