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.v2.api.system.controller;
22 import io.swagger.v3.oas.annotations.Operation;
23 import java.util.Optional;
24 import javax.annotation.Nullable;
25 import org.sonar.db.Database;
26 import org.sonar.server.platform.db.migration.DatabaseMigrationState;
27 import org.sonar.server.platform.db.migration.version.DatabaseVersion;
28 import org.sonar.server.v2.common.DateString;
29 import org.springframework.web.bind.annotation.GetMapping;
30 import org.springframework.web.bind.annotation.RequestMapping;
31 import org.springframework.web.bind.annotation.RestController;
33 import static com.google.common.base.Preconditions.checkState;
34 import static org.sonar.server.platform.db.migration.DatabaseMigrationState.NO_CONNECTION_TO_DB;
35 import static org.sonar.server.platform.db.migration.DatabaseMigrationState.UNSUPPORTED_DATABASE_MIGRATION_STATUS;
36 import static org.sonar.server.v2.WebApiEndpoints.DATABASE_MIGRATIONS_ENDPOINT;
39 @RequestMapping(DATABASE_MIGRATIONS_ENDPOINT)
40 public class DatabaseMigrationsController {
42 private final DatabaseVersion databaseVersion;
43 private final DatabaseMigrationState databaseMigrationState;
44 private final Database database;
46 public DatabaseMigrationsController(DatabaseVersion databaseVersion, DatabaseMigrationState databaseMigrationState, Database database) {
47 this.databaseVersion = databaseVersion;
48 this.databaseMigrationState = databaseMigrationState;
49 this.database = database;
52 @Operation(summary = "Gets the status of ongoing database migrations, if any", description = "Return the detailed status of ongoing database migrations" +
53 " including starting date. If no migration is ongoing or needed it is still possible to call this endpoint and receive appropriate information.")
55 public DatabaseMigrationsResponse getStatus() {
56 Optional<Long> currentVersion = databaseVersion.getVersion();
57 checkState(currentVersion.isPresent(), NO_CONNECTION_TO_DB);
58 DatabaseVersion.Status status = databaseVersion.getStatus();
59 if (status == DatabaseVersion.Status.UP_TO_DATE || status == DatabaseVersion.Status.REQUIRES_DOWNGRADE) {
60 return new DatabaseMigrationsResponse(databaseMigrationState);
61 } else if (!database.getDialect().supportsMigration()) {
62 return new DatabaseMigrationsResponse(DatabaseMigrationState.Status.STATUS_NOT_SUPPORTED);
64 return switch (databaseMigrationState.getStatus()) {
65 case RUNNING, FAILED, SUCCEEDED -> new DatabaseMigrationsResponse(databaseMigrationState);
66 case NONE -> new DatabaseMigrationsResponse(DatabaseMigrationState.Status.MIGRATION_REQUIRED);
67 default -> throw new IllegalArgumentException(UNSUPPORTED_DATABASE_MIGRATION_STATUS);
73 public record DatabaseMigrationsResponse(String status, @Nullable String startedAt, @Nullable String message) {
75 public DatabaseMigrationsResponse(DatabaseMigrationState state) {
76 this(state.getStatus().toString(),
77 DateString.from(state.getStartedAt()),
78 state.getError() != null ? state.getError().getMessage() : state.getStatus().getMessage());
81 public DatabaseMigrationsResponse(DatabaseMigrationState.Status status) {
82 this(status.toString(), null, status.getMessage());