aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2016-12-16 09:12:19 +0100
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2016-12-16 14:35:28 +0100
commit1c34d6ef83b6e9dbfada259c3111acbad3c1ff03 (patch)
tree1d1c8b9133084a3cb884aada25089436054e22ac /sonar-db
parent90d681b83f6399929da48aad426e20f857ff60f3 (diff)
downloadsonarqube-1c34d6ef83b6e9dbfada259c3111acbad3c1ff03.tar.gz
sonarqube-1c34d6ef83b6e9dbfada259c3111acbad3c1ff03.zip
SONAR-8445 move DatabaseVersion to sonar-db-migration
Diffstat (limited to 'sonar-db')
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/SqTables.java (renamed from sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java)65
-rw-r--r--sonar-db/src/test/java/org/sonar/db/DatabaseCommands.java6
-rw-r--r--sonar-db/src/test/java/org/sonar/db/version/DatabaseVersionTest.java68
3 files changed, 7 insertions, 132 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java b/sonar-db/src/main/java/org/sonar/db/version/SqTables.java
index 6dcb89683e0..e08c27fdc89 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/SqTables.java
@@ -19,33 +19,16 @@
*/
package org.sonar.db.version;
-import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableSet;
-import java.util.Collections;
-import java.util.List;
import java.util.Set;
-import javax.annotation.Nullable;
-import org.sonar.db.DatabaseUtils;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
-
-public class DatabaseVersion {
-
- public static final int LAST_VERSION = 1_502;
-
- /**
- * The minimum supported version which can be upgraded. Lower
- * versions must be previously upgraded to LTS version.
- * Note that the value can't be less than current LTS version.
- */
- public static final int MIN_UPGRADE_VERSION = 1_152;
+public final class SqTables {
/**
* These tables are still involved in DB migrations, so potentially
* incorrect collation must be fixed so that joins with other
* tables are possible.
*
- * @see org.sonar.db.charset.ColumnDef#isInSonarQubeTable()
+ * @see org.sonar.db.charset.ColumnDef#isInSonarQubeTable()
*/
public static final Set<String> OLD_DROPPED_TABLES = ImmutableSet.of(
"active_dashboards",
@@ -109,47 +92,7 @@ public class DatabaseVersion {
"user_tokens",
"webhook_deliveries");
- private final DbClient dbClient;
-
- public DatabaseVersion(DbClient dbClient) {
- this.dbClient = dbClient;
- }
-
- @VisibleForTesting
- static Status getStatus(@Nullable Integer currentVersion, int lastVersion) {
- Status status = Status.FRESH_INSTALL;
- if (currentVersion != null) {
- if (currentVersion == lastVersion) {
- status = Status.UP_TO_DATE;
- } else if (currentVersion > lastVersion) {
- status = Status.REQUIRES_DOWNGRADE;
- } else {
- status = Status.REQUIRES_UPGRADE;
- }
- }
- return status;
- }
-
- public Status getStatus() {
- return getStatus(getVersion(), LAST_VERSION);
- }
-
- public Integer getVersion() {
- try (DbSession dbSession = dbClient.openSession(false)) {
- if (!DatabaseUtils.tableExists("SCHEMA_MIGRATIONS", dbSession.getConnection())) {
- return null;
- }
-
- List<Integer> versions = dbClient.schemaMigrationDao().selectVersions(dbSession);
- if (!versions.isEmpty()) {
- Collections.sort(versions);
- return versions.get(versions.size() - 1);
- }
- return null;
- }
- }
-
- public enum Status {
- UP_TO_DATE, REQUIRES_UPGRADE, REQUIRES_DOWNGRADE, FRESH_INSTALL
+ private SqTables() {
+ // prevents instantiation
}
}
diff --git a/sonar-db/src/test/java/org/sonar/db/DatabaseCommands.java b/sonar-db/src/test/java/org/sonar/db/DatabaseCommands.java
index 812ad84eb32..9ec7da2b031 100644
--- a/sonar-db/src/test/java/org/sonar/db/DatabaseCommands.java
+++ b/sonar-db/src/test/java/org/sonar/db/DatabaseCommands.java
@@ -43,7 +43,7 @@ import org.sonar.db.dialect.MsSql;
import org.sonar.db.dialect.MySql;
import org.sonar.db.dialect.Oracle;
import org.sonar.db.dialect.PostgreSql;
-import org.sonar.db.version.DatabaseVersion;
+import org.sonar.db.version.SqTables;
public abstract class DatabaseCommands {
private final IDataTypeFactory dbUnitFactory;
@@ -149,7 +149,7 @@ public abstract class DatabaseCommands {
try {
connection.setAutoCommit(false);
statement = connection.createStatement();
- for (String table : DatabaseVersion.TABLES) {
+ for (String table : SqTables.TABLES) {
try {
if (shouldTruncate(connection, table)) {
statement.executeUpdate(truncateSql(table));
@@ -193,7 +193,7 @@ public abstract class DatabaseCommands {
connection.setAutoCommit(false);
statement = connection.createStatement();
- for (String table : DatabaseVersion.TABLES) {
+ for (String table : SqTables.TABLES) {
try {
resultSet = statement.executeQuery("SELECT CASE WHEN MAX(ID) IS NULL THEN 1 ELSE MAX(ID)+1 END FROM " + table);
resultSet.next();
diff --git a/sonar-db/src/test/java/org/sonar/db/version/DatabaseVersionTest.java b/sonar-db/src/test/java/org/sonar/db/version/DatabaseVersionTest.java
deleted file mode 100644
index cb1e3219ce9..00000000000
--- a/sonar-db/src/test/java/org/sonar/db/version/DatabaseVersionTest.java
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program 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
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.db.version;
-
-import org.junit.Rule;
-import org.junit.Test;
-import org.sonar.api.utils.System2;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.db.DbTester;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class DatabaseVersionTest {
-
- @Rule
- public DbTester dbTester = DbTester.create(System2.INSTANCE);
-
- private DbClient dbClient = dbTester.getDbClient();
- private DbSession dbSession = dbTester.getSession();
- private DatabaseVersion underTest = new DatabaseVersion(dbClient);
-
- @Test
- public void getVersion() {
- dbTester.getDbClient().schemaMigrationDao().insert(dbSession, "1");
- dbTester.getDbClient().schemaMigrationDao().insert(dbSession, "2");
- dbTester.getDbClient().schemaMigrationDao().insert(dbSession, "4");
- dbTester.getDbClient().schemaMigrationDao().insert(dbSession, "123");
- dbTester.getDbClient().schemaMigrationDao().insert(dbSession, "50");
- dbSession.commit();
-
- Integer version = underTest.getVersion();
-
- assertThat(version).isEqualTo(123);
- }
-
- @Test
- public void getVersion_no_rows() {
- Integer version = underTest.getVersion();
-
- assertThat(version).isNull();
- }
-
- @Test
- public void getStatus() {
- assertThat(DatabaseVersion.getStatus(null, 150)).isEqualTo(DatabaseVersion.Status.FRESH_INSTALL);
- assertThat(DatabaseVersion.getStatus(123, 150)).isEqualTo(DatabaseVersion.Status.REQUIRES_UPGRADE);
- assertThat(DatabaseVersion.getStatus(150, 150)).isEqualTo(DatabaseVersion.Status.UP_TO_DATE);
- assertThat(DatabaseVersion.getStatus(200, 150)).isEqualTo(DatabaseVersion.Status.REQUIRES_DOWNGRADE);
- }
-}