3 * Copyright (C) 2009-2023 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;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.mockito.Mockito;
25 import org.sonar.api.utils.log.LogTester;
26 import org.sonar.api.utils.log.LoggerLevel;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.dialect.Dialect;
29 import org.sonar.db.dialect.H2;
30 import org.sonar.db.dialect.MsSql;
31 import org.sonar.db.dialect.Oracle;
32 import org.sonar.db.dialect.PostgreSql;
33 import org.sonar.server.platform.DefaultServerUpgradeStatus;
34 import org.sonar.server.platform.db.migration.engine.MigrationEngine;
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.verify;
39 import static org.mockito.Mockito.verifyNoInteractions;
40 import static org.mockito.Mockito.when;
42 public class AutoDbMigrationTest {
44 public LogTester logTester = new LogTester();
46 private DbClient dbClient = mock(DbClient.class, Mockito.RETURNS_DEEP_STUBS);
47 private DefaultServerUpgradeStatus serverUpgradeStatus = mock(DefaultServerUpgradeStatus.class);
48 private MigrationEngine migrationEngine = mock(MigrationEngine.class);
49 private AutoDbMigration underTest = new AutoDbMigration(serverUpgradeStatus, migrationEngine);
52 public void start_runs_MigrationEngine_on_h2_if_fresh_install() {
53 start_runs_MigrationEngine_for_dialect_if_fresh_install(new H2());
57 public void start_runs_MigrationEngine_on_postgre_if_fresh_install() {
58 start_runs_MigrationEngine_for_dialect_if_fresh_install(new PostgreSql());
62 public void start_runs_MigrationEngine_on_Oracle_if_fresh_install() {
63 start_runs_MigrationEngine_for_dialect_if_fresh_install(new Oracle());
67 public void start_runs_MigrationEngine_on_MsSQL_if_fresh_install() {
68 start_runs_MigrationEngine_for_dialect_if_fresh_install(new MsSql());
71 private void start_runs_MigrationEngine_for_dialect_if_fresh_install(Dialect dialect) {
73 mockFreshInstall(true);
77 verify(migrationEngine).execute();
82 public void start_does_nothing_if_not_fresh_install() {
83 mockFreshInstall(false);
87 verifyNoInteractions(migrationEngine);
88 assertThat(logTester.logs(LoggerLevel.INFO)).isEmpty();
92 public void start_runs_MigrationEngine_if_blue_green_upgrade() {
93 mockFreshInstall(false);
94 when(serverUpgradeStatus.isUpgraded()).thenReturn(true);
95 when(serverUpgradeStatus.isBlueGreen()).thenReturn(true);
99 verify(migrationEngine).execute();
100 assertThat(logTester.logs(LoggerLevel.INFO)).contains("Automatically perform DB migration on blue/green deployment");
104 public void start_does_nothing_if_blue_green_but_no_upgrade() {
105 mockFreshInstall(false);
106 when(serverUpgradeStatus.isUpgraded()).thenReturn(false);
107 when(serverUpgradeStatus.isBlueGreen()).thenReturn(true);
111 verifyNoInteractions(migrationEngine);
112 assertThat(logTester.logs(LoggerLevel.INFO)).isEmpty();
116 public void start_runs_MigrationEngine_if_autoDbMigration_enabled() {
117 mockFreshInstall(false);
118 when(serverUpgradeStatus.isUpgraded()).thenReturn(true);
119 when(serverUpgradeStatus.isAutoDbUpgrade()).thenReturn(true);
123 verify(migrationEngine).execute();
124 assertThat(logTester.logs(LoggerLevel.INFO)).contains("Automatically perform DB migration, as automatic database upgrade is enabled");
128 public void start_does_nothing_if_autoDbMigration_but_no_upgrade() {
129 mockFreshInstall(false);
130 when(serverUpgradeStatus.isUpgraded()).thenReturn(false);
131 when(serverUpgradeStatus.isAutoDbUpgrade()).thenReturn(true);
135 verifyNoInteractions(migrationEngine);
136 assertThat(logTester.logs(LoggerLevel.INFO)).isEmpty();
140 public void stop_has_no_effect() {
144 private void mockFreshInstall(boolean value) {
145 when(serverUpgradeStatus.isFreshInstall()).thenReturn(value);
148 private void mockDialect(Dialect dialect) {
149 when(dbClient.getDatabase().getDialect()).thenReturn(dialect);
152 private void verifyInfoLog() {
153 assertThat(logTester.logs()).hasSize(1);
154 assertThat(logTester.logs(LoggerLevel.INFO)).containsExactly("Automatically perform DB migration on fresh install");