3 * Copyright (C) 2009-2021 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.version;
22 import java.util.Optional;
23 import org.sonar.server.platform.db.migration.history.MigrationHistory;
24 import org.sonar.server.platform.db.migration.step.MigrationSteps;
26 public class DatabaseVersion {
29 * The minimum supported version which can be upgraded. Lower
30 * versions must be previously upgraded to LTS version.
31 * Note that the value can't be less than current LTS version.
33 public static final long MIN_UPGRADE_VERSION = 2_800;
35 private final MigrationSteps migrationSteps;
36 private final MigrationHistory migrationHistory;
38 public DatabaseVersion(MigrationSteps migrationSteps, MigrationHistory migrationHistory) {
39 this.migrationSteps = migrationSteps;
40 this.migrationHistory = migrationHistory;
43 public Status getStatus() {
44 return getStatus(migrationHistory.getLastMigrationNumber(), migrationSteps.getMaxMigrationNumber());
48 * Convenience method to retrieve the value of {@link MigrationHistory#getLastMigrationNumber()}.
50 public Optional<Long> getVersion() {
51 return migrationHistory.getLastMigrationNumber();
54 private static Status getStatus(Optional<Long> currentVersion, long lastVersion) {
55 if (!currentVersion.isPresent()) {
56 return Status.FRESH_INSTALL;
58 Long aLong = currentVersion.get();
59 if (aLong == lastVersion) {
60 return Status.UP_TO_DATE;
62 if (aLong > lastVersion) {
63 return Status.REQUIRES_DOWNGRADE;
65 return Status.REQUIRES_UPGRADE;
69 UP_TO_DATE, REQUIRES_UPGRADE, REQUIRES_DOWNGRADE, FRESH_INSTALL