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.ws;
22 import com.google.common.io.Resources;
23 import java.util.Optional;
24 import org.sonar.api.server.ws.Request;
25 import org.sonar.api.server.ws.Response;
26 import org.sonar.api.server.ws.WebService;
27 import org.sonar.api.utils.text.JsonWriter;
28 import org.sonar.db.Database;
29 import org.sonar.server.platform.db.migration.DatabaseMigrationState;
30 import org.sonar.server.platform.db.migration.version.DatabaseVersion;
32 import static com.google.common.base.Preconditions.checkState;
33 import static org.sonar.server.platform.db.migration.DatabaseMigrationState.NO_CONNECTION_TO_DB;
34 import static org.sonar.server.platform.db.migration.DatabaseMigrationState.UNSUPPORTED_DATABASE_MIGRATION_STATUS;
35 import static org.sonar.server.platform.ws.DbMigrationJsonWriter.statusDescription;
36 import static org.sonar.server.platform.ws.DbMigrationJsonWriter.write;
37 import static org.sonar.server.platform.ws.DbMigrationJsonWriter.writeMigrationRequiredResponse;
38 import static org.sonar.server.platform.ws.DbMigrationJsonWriter.writeNotSupportedResponse;
41 * Implementation of the {@code db_migration_status} action for the System WebService.
43 public class DbMigrationStatusAction implements SystemWsAction {
45 private final DatabaseVersion databaseVersion;
46 private final DatabaseMigrationState databaseMigrationState;
47 private final Database database;
49 public DbMigrationStatusAction(DatabaseVersion databaseVersion, Database database, DatabaseMigrationState databaseMigrationState) {
50 this.databaseVersion = databaseVersion;
51 this.database = database;
52 this.databaseMigrationState = databaseMigrationState;
56 public void define(WebService.NewController controller) {
57 controller.createAction("db_migration_status")
58 .setDescription("Display the database migration status of SonarQube." +
63 .setResponseExample(Resources.getResource(this.getClass(), "example-migrate_db.json"));
67 public void handle(Request request, Response response) throws Exception {
68 Optional<Long> currentVersion = databaseVersion.getVersion();
69 checkState(currentVersion.isPresent(), NO_CONNECTION_TO_DB);
71 try (JsonWriter json = response.newJsonWriter()) {
72 DatabaseVersion.Status status = databaseVersion.getStatus();
73 if (status == DatabaseVersion.Status.UP_TO_DATE || status == DatabaseVersion.Status.REQUIRES_DOWNGRADE) {
74 write(json, databaseMigrationState);
75 } else if (!database.getDialect().supportsMigration()) {
76 writeNotSupportedResponse(json);
78 switch (databaseMigrationState.getStatus()) {
79 case RUNNING, FAILED, SUCCEEDED:
80 write(json, databaseMigrationState);
83 writeMigrationRequiredResponse(json);
86 throw new IllegalArgumentException(UNSUPPORTED_DATABASE_MIGRATION_STATUS);