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.db.migration.version.v101;
22 import com.google.common.annotations.VisibleForTesting;
23 import java.sql.SQLException;
24 import java.util.Arrays;
26 import org.apache.commons.lang.StringUtils;
27 import org.sonar.db.Database;
28 import org.sonar.db.DatabaseUtils;
29 import org.sonar.server.platform.db.migration.step.DataChange;
30 import org.sonar.server.platform.db.migration.step.MassRowSplitter;
31 import org.sonar.server.platform.db.migration.step.Select;
33 import static java.util.Collections.emptySet;
34 import static java.util.stream.Collectors.toSet;
36 class MigrateScmAccountsFromUsersToScmAccounts extends DataChange {
39 static final char SCM_ACCOUNTS_SEPARATOR_CHAR = '\n';
41 public MigrateScmAccountsFromUsersToScmAccounts(Database db) {
46 protected void execute(Context context) throws SQLException {
47 if (isScmColumnDropped()) {
53 private boolean isScmColumnDropped() throws SQLException {
54 try (var connection = getDatabase().getDataSource().getConnection()) {
55 return !DatabaseUtils.tableColumnExists(connection, DropScmAccountsInUsers.TABLE_NAME, DropScmAccountsInUsers.COLUMN_NAME);
59 private static void migrateData(Context context) throws SQLException {
60 MassRowSplitter<ScmAccountRow> massRowSplitter = context.prepareMassRowSplitter();
62 massRowSplitter.select("select u.uuid, lower(u.scm_accounts) from users u where u.active=? and not exists (select 1 from scm_accounts sa where sa.user_uuid = u.uuid)")
65 massRowSplitter.insert("insert into scm_accounts (user_uuid, scm_account) values (?, ?)");
67 massRowSplitter.splitRow(MigrateScmAccountsFromUsersToScmAccounts::toScmAccountRows);
69 massRowSplitter.execute((scmAccountRow, insert) -> {
70 insert.setString(1, scmAccountRow.userUuid());
71 insert.setString(2, scmAccountRow.scmAccount());
76 private static Set<ScmAccountRow> toScmAccountRows(Select.Row row) {
78 String userUuid = row.getString(1);
79 String[] scmAccounts = StringUtils.split(row.getString(2), SCM_ACCOUNTS_SEPARATOR_CHAR);
80 if (scmAccounts == null) {
83 return Arrays.stream(scmAccounts)
84 .map(scmAccount -> new ScmAccountRow(userUuid, scmAccount))
86 } catch (SQLException sqlException) {
87 throw new RuntimeException(sqlException);
92 record ScmAccountRow(String userUuid, String scmAccount) {