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 public class PopulateMainProjectBranches extends DataChange {
30 private static final String MAIN_BRANCH_NAME = "master";
32 private final System2 system2;
34 public PopulateMainProjectBranches(Database db, System2 system2) {
36 this.system2 = system2;
40 protected void execute(Context context) throws SQLException {
41 long now = system2.now();
42 MassUpdate massUpdate = context.prepareMassUpdate();
43 massUpdate.select("SELECT uuid FROM projects p "
44 + "WHERE p.scope='PRJ' AND p.qualifier='TRK' AND p.main_branch_project_uuid IS NULL "
45 + "AND NOT EXISTS (SELECT uuid FROM project_branches b WHERE b.uuid = p.uuid)");
46 massUpdate.update("INSERT INTO project_branches (uuid, project_uuid, kee, branch_type, "
47 + "merge_branch_uuid, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)");
48 massUpdate.rowPluralName("projects");
49 massUpdate.execute((row, update) -> {
50 String uuid = row.getString(1);
51 update.setString(1, uuid);
52 update.setString(2, uuid);
53 update.setString(3, MAIN_BRANCH_NAME);
54 update.setString(4, "LONG");
55 update.setString(5, null);
56 update.setLong(6, now);
57 update.setLong(7, now);