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.sql.SQLException;
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.server.ws.WebService.NewAction;
28 import org.sonar.api.utils.text.JsonWriter;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.property.PropertyDto;
31 import org.sonar.server.platform.db.migration.adhoc.AddMeasuresMigratedColumnToPortfoliosTable;
32 import org.sonar.server.platform.db.migration.adhoc.AddMeasuresMigratedColumnToProjectBranchesTable;
33 import org.sonar.server.platform.db.migration.adhoc.CreateIndexOnPortfoliosMeasuresMigrated;
34 import org.sonar.server.platform.db.migration.adhoc.CreateIndexOnProjectBranchesMeasuresMigrated;
35 import org.sonar.server.platform.db.migration.adhoc.CreateMeasuresTable;
36 import org.sonar.server.user.UserSession;
38 import static java.lang.String.format;
39 import static org.sonar.core.config.CorePropertyDefinitions.SYSTEM_MEASURES_MIGRATION_ENABLED;
42 * Implementation of the {@code prepare_migration} action for the System WebService.
44 public class PrepareMigrationAction implements SystemWsAction {
46 public static final String PARAM_ENABLE = "enable";
47 private final UserSession userSession;
48 private final DbClient dbClient;
49 private final CreateMeasuresTable createMeasuresTable;
50 private final AddMeasuresMigratedColumnToProjectBranchesTable addMeasuresMigratedColumnToProjectBranchesTable;
51 private final AddMeasuresMigratedColumnToPortfoliosTable addMeasuresMigratedColumnToPortfoliosTable;
52 private final CreateIndexOnProjectBranchesMeasuresMigrated createIndexOnProjectBranchesMeasuresMigrated;
53 private final CreateIndexOnPortfoliosMeasuresMigrated createIndexOnPortfoliosMeasuresMigrated;
55 public PrepareMigrationAction(UserSession userSession, DbClient dbClient, CreateMeasuresTable createMeasuresTable,
56 AddMeasuresMigratedColumnToProjectBranchesTable addMeasuresMigratedColumnToProjectBranchesTable,
57 AddMeasuresMigratedColumnToPortfoliosTable addMeasuresMigratedColumnToPortfoliosTable,
58 CreateIndexOnProjectBranchesMeasuresMigrated createIndexOnProjectBranchesMeasuresMigrated,
59 CreateIndexOnPortfoliosMeasuresMigrated createIndexOnPortfoliosMeasuresMigrated) {
60 this.userSession = userSession;
61 this.dbClient = dbClient;
62 this.createMeasuresTable = createMeasuresTable;
63 this.addMeasuresMigratedColumnToProjectBranchesTable = addMeasuresMigratedColumnToProjectBranchesTable;
64 this.addMeasuresMigratedColumnToPortfoliosTable = addMeasuresMigratedColumnToPortfoliosTable;
65 this.createIndexOnProjectBranchesMeasuresMigrated = createIndexOnProjectBranchesMeasuresMigrated;
66 this.createIndexOnPortfoliosMeasuresMigrated = createIndexOnPortfoliosMeasuresMigrated;
70 public void define(WebService.NewController controller) {
71 NewAction action = controller.createAction("prepare_migration")
72 .setDescription("Prepare the migration to the next major version of SonarQube." +
74 "Sending a POST request to this URL enables the 'live_measures' table migration. " +
75 "It is strongly advised to <strong>make a database backup</strong> before invoking this WS. " +
76 "Requires system administration permission.")
81 .setResponseExample(Resources.getResource(this.getClass(), "example-prepare_migration.json"));
83 action.createParam(PARAM_ENABLE)
84 .setDescription("Set to true to enable the migration mode. Set to false to disable.")
85 .setBooleanPossibleValues()
90 public void handle(Request request, Response response) throws Exception {
91 userSession.checkIsSystemAdministrator();
93 boolean enable = request.mandatoryParamAsBoolean(PARAM_ENABLE);
97 updateProperty(enable);
99 try (JsonWriter json = response.newJsonWriter()) {
101 .prop("message", format("The 'live_measures' migration mode is %s", enable ? "enabled" : "disabled"))
106 private void updateDdl() throws SQLException {
107 createMeasuresTable.execute();
108 addMeasuresMigratedColumnToProjectBranchesTable.execute();
109 addMeasuresMigratedColumnToPortfoliosTable.execute();
110 createIndexOnProjectBranchesMeasuresMigrated.execute();
111 createIndexOnPortfoliosMeasuresMigrated.execute();
114 private void updateProperty(boolean enable) {
115 dbClient.propertiesDao().saveProperty(new PropertyDto().setKey(SYSTEM_MEASURES_MIGRATION_ENABLED).setValue(Boolean.toString(enable)));