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.history;
22 import java.sql.Connection;
23 import java.sql.PreparedStatement;
24 import java.sql.SQLException;
25 import java.util.Arrays;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.sonar.db.CoreDbTester;
29 import org.sonar.server.platform.db.migration.step.MigrationStep;
30 import org.sonar.server.platform.db.migration.step.RegisteredMigrationStep;
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.assertj.core.api.Assertions.assertThatThrownBy;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.verify;
37 public class MigrationHistoryImplIT {
39 public CoreDbTester dbTester = CoreDbTester.createForSchema(MigrationHistoryImplIT.class, "schema_migration.sql");
41 private MigrationHistoryMeddler migrationHistoryMeddler = mock(MigrationHistoryMeddler.class);
42 private MigrationHistoryImpl underTest = new MigrationHistoryImpl(dbTester.database(), migrationHistoryMeddler);
45 public void start_does_not_fail_if_table_history_exists_and_calls_meddler() {
48 verify(migrationHistoryMeddler).meddle(underTest);
52 public void getLastMigrationNumber_returns_empty_if_history_table_is_empty() {
53 assertThat(underTest.getLastMigrationNumber()).isEmpty();
57 public void getLastMigrationNumber_returns_last_version_assuming_version_are_only_number() throws SQLException {
60 assertThat(underTest.getLastMigrationNumber()).contains(30L);
64 public void done_fails_with_NPE_if_argument_is_null() {
65 assertThatThrownBy(() -> underTest.done(null))
66 .isInstanceOf(NullPointerException.class);
70 public void done_adds_migration_number_to_table() {
71 underTest.done(new RegisteredMigrationStep(12, "aa", MigrationStep.class));
73 assertThat(underTest.getLastMigrationNumber()).contains(12L);
76 private void insert(int... versions) throws SQLException {
77 try (Connection connection = dbTester.database().getDataSource().getConnection()) {
78 Arrays.stream(versions).forEach(version -> insert(connection, version));
82 private void insert(Connection connection, long version) {
83 try (PreparedStatement statement = connection.prepareStatement("insert into schema_migrations(version) values (?)")) {
84 statement.setString(1, String.valueOf(version));
87 } catch (SQLException e) {
88 throw new IllegalStateException(String.format("Failed to insert row with value %s", version), e);