]> source.dussan.org Git - sonarqube.git/blob
54a5aac148a83e9aab953989b598f097054abec0
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.platform.ws;
21
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;
31
32 import static com.google.common.base.Preconditions.checkState;
33 import static org.sonar.server.platform.ws.DbMigrationJsonWriter.NO_CONNECTION_TO_DB;
34 import static org.sonar.server.platform.ws.DbMigrationJsonWriter.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;
39
40 /**
41  * Implementation of the {@code db_migration_status} action for the System WebService.
42  */
43 public class DbMigrationStatusAction implements SystemWsAction {
44
45   private final DatabaseVersion databaseVersion;
46   private final DatabaseMigrationState databaseMigrationState;
47   private final Database database;
48
49   public DbMigrationStatusAction(DatabaseVersion databaseVersion, Database database, DatabaseMigrationState databaseMigrationState) {
50     this.databaseVersion = databaseVersion;
51     this.database = database;
52     this.databaseMigrationState = databaseMigrationState;
53   }
54
55   @Override
56   public void define(WebService.NewController controller) {
57     controller.createAction("db_migration_status")
58       .setDescription("Display the database migration status of SonarQube." +
59         "<br/>" +
60         statusDescription())
61       .setSince("5.2")
62       .setHandler(this)
63       .setResponseExample(Resources.getResource(this.getClass(), "example-migrate_db.json"));
64   }
65
66   @Override
67   public void handle(Request request, Response response) throws Exception {
68     Optional<Long> currentVersion = databaseVersion.getVersion();
69     checkState(currentVersion.isPresent(), NO_CONNECTION_TO_DB);
70
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);
77       } else {
78         switch (databaseMigrationState.getStatus()) {
79           case RUNNING, FAILED, SUCCEEDED:
80             write(json, databaseMigrationState);
81             break;
82           case NONE:
83             writeMigrationRequiredResponse(json);
84             break;
85           default:
86             throw new IllegalArgumentException(UNSUPPORTED_DATABASE_MIGRATION_STATUS);
87         }
88       }
89     }
90   }
91 }