3 * Copyright (C) 2009-2024 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.step;
22 import java.util.List;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.sonar.core.platform.Container;
26 import org.sonar.core.util.logs.Profiler;
27 import org.sonar.server.platform.db.migration.history.MigrationHistory;
29 import static com.google.common.base.Preconditions.checkState;
31 public class MigrationStepsExecutorImpl implements MigrationStepsExecutor {
32 private static final Logger LOGGER = LoggerFactory.getLogger("DbMigrations");
33 private static final String GLOBAL_START_MESSAGE = "Executing DB migrations...";
34 private static final String GLOBAL_END_MESSAGE = "Executed DB migrations: {}";
35 private static final String STEP_START_PATTERN = "{}...";
36 private static final String STEP_STOP_PATTERN = "{}: {}";
38 private final Container migrationContainer;
39 private final MigrationHistory migrationHistory;
41 public MigrationStepsExecutorImpl(Container migrationContainer, MigrationHistory migrationHistory) {
42 this.migrationContainer = migrationContainer;
43 this.migrationHistory = migrationHistory;
47 public void execute(List<RegisteredMigrationStep> steps) {
48 Profiler globalProfiler = Profiler.create(LOGGER);
49 globalProfiler.startInfo(GLOBAL_START_MESSAGE);
50 boolean allStepsExecuted = false;
52 steps.forEach(this::execute);
53 allStepsExecuted = true;
55 if (allStepsExecuted) {
56 globalProfiler.stopInfo(GLOBAL_END_MESSAGE, "success");
58 globalProfiler.stopError(GLOBAL_END_MESSAGE, "failure");
63 private void execute(RegisteredMigrationStep step) {
64 MigrationStep migrationStep = migrationContainer.getComponentByType(step.getStepClass());
65 checkState(migrationStep != null, "Can not find instance of " + step.getStepClass());
67 execute(step, migrationStep);
70 private void execute(RegisteredMigrationStep step, MigrationStep migrationStep) {
71 Profiler stepProfiler = Profiler.create(LOGGER);
72 stepProfiler.startInfo(STEP_START_PATTERN, step);
75 migrationStep.execute();
76 migrationHistory.done(step);
78 } catch (Exception e) {
79 throw new MigrationStepExecutionException(step, e);
82 stepProfiler.stopInfo(STEP_STOP_PATTERN, step, "success");
84 stepProfiler.stopError(STEP_STOP_PATTERN, step, "failure");