3 * Copyright (C) 2009-2017 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.v66;
22 import java.sql.SQLException;
23 import org.sonar.api.utils.System2;
24 import org.sonar.db.Database;
25 import org.sonar.server.platform.db.migration.step.DataChange;
26 import org.sonar.server.platform.db.migration.step.MassUpdate;
28 import static org.apache.commons.lang.StringUtils.repeat;
30 public class PopulateMainProjectBranches extends DataChange {
31 private static final int KEE_MAX_LENGTH = 255;
32 static final String NULL_KEY = repeat("_", KEE_MAX_LENGTH);
33 private static final String INSERT_MAIN_PROJECT_BRANCHES = "INSERT INTO project_branches (uuid, project_uuid, kee_type, kee, branch_type, "
34 + "merge_branch_uuid, pull_request_title, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
36 private final System2 system2;
38 public PopulateMainProjectBranches(Database db, System2 system2) {
40 this.system2 = system2;
44 protected void execute(Context context) throws SQLException {
45 long now = system2.now();
46 MassUpdate massUpdate = context.prepareMassUpdate();
47 massUpdate.select("SELECT uuid FROM projects p "
48 + "WHERE p.scope='PRJ' AND p.qualifier='TRK' AND p.main_branch_project_uuid IS NULL "
49 + "AND NOT EXISTS (SELECT uuid FROM project_branches b WHERE b.uuid = p.uuid)");
50 massUpdate.update(INSERT_MAIN_PROJECT_BRANCHES);
51 massUpdate.rowPluralName("projects");
52 massUpdate.execute((row, update) -> {
53 String uuid = row.getString(1);
54 update.setString(1, uuid);
55 update.setString(2, uuid);
56 update.setString(3, "BRANCH");
57 update.setString(4, NULL_KEY);
58 update.setString(5, "LONG");
59 update.setString(6, null);
60 update.setString(7, null);
61 update.setLong(8, now);
62 update.setLong(9, now);