]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-18728 Drop Users.SCM_ACCOUNTS column
authorAurelien Poscia <aurelien.poscia@sonarsource.com>
Thu, 30 Mar 2023 11:21:12 +0000 (13:21 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 31 Mar 2023 20:03:27 +0000 (20:03 +0000)
33 files changed:
server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserMapper.xml
server/sonar-db-dao/src/schema/schema-sq.ddl
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/MigrationConfigurationModule.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v100/CreateIndexForEmailOnUsersTable.java [deleted file]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v100/CreateIndexForScmAccountOnScmAccountsTable.java [deleted file]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v100/CreateScmAccountsTable.java [deleted file]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v100/DbVersion100.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v100/MigrateScmAccountsFromUsersToScmAccounts.java [deleted file]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v110/CreateIndexForEmailOnUsersTable.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v110/CreateIndexForScmAccountOnScmAccountsTable.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v110/CreateScmAccountsTable.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v110/DbVersion110.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v110/DropScmAccountsInUsers.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v110/MigrateScmAccountsFromUsersToScmAccounts.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v100/CreateIndexForEmailOnUsersTableTest.java [deleted file]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v100/CreateIndexForScmAccountOnScmAccountsTableTest.java [deleted file]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v100/CreateScmAccountsTableTest.java [deleted file]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v100/MigrateScmAccountsFromUsersToScmAccountsTest.java [deleted file]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v110/CreateIndexForEmailOnUsersTableTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v110/CreateIndexForScmAccountOnScmAccountsTableTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v110/CreateScmAccountsTableTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v110/DbVersion110Test.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v110/DropScmAccountsInUsersTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v110/MigrateScmAccountsFromUsersToScmAccountsTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v100/CreateIndexForEmailOnUsersTableTest/schema.sql [deleted file]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v100/CreateIndexForScmAccountOnScmAccountsTableTest/schema.sql [deleted file]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v100/CreateIndexesForScmAccountsTest/schema.sql [deleted file]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v100/MigrateScmAccountsFromUsersToScmAccountsTest/schema.sql [deleted file]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v110/CreateIndexForEmailOnUsersTableTest/schema.sql [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v110/CreateIndexForScmAccountOnScmAccountsTableTest/schema.sql [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v110/CreateIndexesForScmAccountsTest/schema.sql [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v110/DropScmAccountsInUsersTest/schema.sql [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v110/MigrateScmAccountsFromUsersToScmAccountsTest/schema.sql [new file with mode: 0644]

index 4a9363a45d97ee9a90f717286a5608c6902e761b..4042833f53226e2795bd78b4075bdf84c382385e 100644 (file)
         u.name,
         u.email,
         u.active,
-        u.scm_accounts,
         u.salt,
         u.crypted_password,
         u.hash_method,
     <sql id="deactivateUserUpdatedFields">
         active = ${_false},
         email = null,
-        scm_accounts = null,
         salt = null,
         crypted_password = null,
         last_connection_date = null,
index 1d4b4ae594c8d76831c7d161f55da720a38a1b8d..536730e889cdbe4f598db23ffb4c2231e6a11127 100644 (file)
@@ -1010,7 +1010,6 @@ CREATE TABLE "USERS"(
     "SALT" CHARACTER VARYING(40),
     "HASH_METHOD" CHARACTER VARYING(10),
     "ACTIVE" BOOLEAN DEFAULT TRUE,
-    "SCM_ACCOUNTS" CHARACTER VARYING(4000),
     "EXTERNAL_LOGIN" CHARACTER VARYING(255) NOT NULL,
     "EXTERNAL_IDENTITY_PROVIDER" CHARACTER VARYING(100) NOT NULL,
     "EXTERNAL_ID" CHARACTER VARYING(255) NOT NULL,
index 991ad522b6f3e5f4c2ea9a91c3bd357acb4da90d..0bbcf637ee5c6ded9ff8982b27b4ba35580e00c4 100644 (file)
@@ -29,6 +29,7 @@ import org.sonar.server.platform.db.migration.step.MigrationStepRegistryImpl;
 import org.sonar.server.platform.db.migration.step.MigrationStepsProvider;
 import org.sonar.server.platform.db.migration.version.v00.DbVersion00;
 import org.sonar.server.platform.db.migration.version.v100.DbVersion100;
+import org.sonar.server.platform.db.migration.version.v110.DbVersion110;
 
 public class MigrationConfigurationModule extends Module {
   @Override
@@ -38,6 +39,7 @@ public class MigrationConfigurationModule extends Module {
       // DbVersion implementations
       DbVersion00.class,
       DbVersion100.class,
+      DbVersion110.class,
 
       // migration steps
       MigrationStepRegistryImpl.class,
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v100/CreateIndexForEmailOnUsersTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v100/CreateIndexForEmailOnUsersTable.java
deleted file mode 100644 (file)
index b05c763..0000000
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2023 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.server.platform.db.migration.version.v100;
-
-import com.google.common.annotations.VisibleForTesting;
-import java.sql.Connection;
-import java.sql.SQLException;
-import org.sonar.db.Database;
-import org.sonar.db.DatabaseUtils;
-import org.sonar.server.platform.db.migration.sql.CreateIndexBuilder;
-import org.sonar.server.platform.db.migration.step.DdlChange;
-
-public class CreateIndexForEmailOnUsersTable extends DdlChange {
-
-  @VisibleForTesting
-  static final String INDEX_NAME = "users_email";
-  @VisibleForTesting
-  static final String TABLE_NAME = "users";
-  @VisibleForTesting
-  static final String COLUMN_NAME = "email";
-
-  public CreateIndexForEmailOnUsersTable(Database db) {
-    super(db);
-  }
-
-  @Override
-  public void execute(Context context) throws SQLException {
-    try (Connection connection = getDatabase().getDataSource().getConnection()) {
-      createIndex(context, connection);
-    }
-  }
-
-  private static void createIndex(Context context, Connection connection) {
-    if (!DatabaseUtils.indexExistsIgnoreCase(TABLE_NAME, INDEX_NAME, connection)) {
-      context.execute(new CreateIndexBuilder()
-        .setTable(TABLE_NAME)
-        .setName(INDEX_NAME)
-        .addColumn(COLUMN_NAME)
-        .setUnique(false)
-        .build());
-    }
-  }
-}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v100/CreateIndexForScmAccountOnScmAccountsTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v100/CreateIndexForScmAccountOnScmAccountsTable.java
deleted file mode 100644 (file)
index c5d2557..0000000
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2023 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.server.platform.db.migration.version.v100;
-
-import com.google.common.annotations.VisibleForTesting;
-import java.sql.Connection;
-import java.sql.SQLException;
-import org.sonar.db.Database;
-import org.sonar.db.DatabaseUtils;
-import org.sonar.server.platform.db.migration.sql.CreateIndexBuilder;
-import org.sonar.server.platform.db.migration.step.DdlChange;
-
-import static org.sonar.server.platform.db.migration.version.v100.CreateScmAccountsTable.SCM_ACCOUNT_COLUMN_NAME;
-import static org.sonar.server.platform.db.migration.version.v100.CreateScmAccountsTable.TABLE_NAME;
-
-public class CreateIndexForScmAccountOnScmAccountsTable extends DdlChange {
-
-  @VisibleForTesting
-  static final String INDEX_NAME = "scm_accounts_scm_account";
-
-  public CreateIndexForScmAccountOnScmAccountsTable(Database db) {
-    super(db);
-  }
-
-  @Override
-  public void execute(Context context) throws SQLException {
-    try (Connection connection = getDatabase().getDataSource().getConnection()) {
-      createIndex(context, connection);
-    }
-  }
-
-  private static void createIndex(Context context, Connection connection) {
-    if (!DatabaseUtils.indexExistsIgnoreCase(TABLE_NAME, INDEX_NAME, connection)) {
-      context.execute(new CreateIndexBuilder()
-        .setTable(TABLE_NAME)
-        .setName(INDEX_NAME)
-        .addColumn(SCM_ACCOUNT_COLUMN_NAME)
-        .setUnique(false)
-        .build());
-    }
-  }
-}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v100/CreateScmAccountsTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v100/CreateScmAccountsTable.java
deleted file mode 100644 (file)
index 66e11bc..0000000
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2023 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.server.platform.db.migration.version.v100;
-
-import com.google.common.annotations.VisibleForTesting;
-import java.sql.SQLException;
-import org.sonar.db.Database;
-import org.sonar.server.platform.db.migration.sql.CreateTableBuilder;
-import org.sonar.server.platform.db.migration.step.CreateTableChange;
-
-import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.USER_UUID_SIZE;
-import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;
-
-public class CreateScmAccountsTable extends CreateTableChange {
-  static final String TABLE_NAME = "scm_accounts";
-  static final String USER_UUID_COLUMN_NAME = "user_uuid";
-  static final String SCM_ACCOUNT_COLUMN_NAME = "scm_account";
-
-  @VisibleForTesting
-  static final int SCM_ACCOUNT_SIZE = 255;
-
-  public CreateScmAccountsTable(Database db) {
-    super(db, TABLE_NAME);
-  }
-
-  @Override
-  public void execute(Context context, String tableName) throws SQLException {
-    context.execute(new CreateTableBuilder(getDialect(), tableName)
-      .addPkColumn(newVarcharColumnDefBuilder().setColumnName(USER_UUID_COLUMN_NAME).setIsNullable(false).setLimit(USER_UUID_SIZE).build())
-      .addPkColumn(newVarcharColumnDefBuilder().setColumnName(SCM_ACCOUNT_COLUMN_NAME).setIsNullable(false).setLimit(SCM_ACCOUNT_SIZE).build())
-      .build());
-  }
-}
index 01e9385c4a9011096b5079d90bc4b238145036fe..ba6d6a3169f4edfb9c7119ca0416026f808ce520 100644 (file)
@@ -58,10 +58,6 @@ public class DbVersion100 implements DbVersion {
       .add(10_0_014, "Drop any SCIM User provisioning, turning all users local", DropScimUserProvisioning.class)
       .add(10_0_015, "Add ncloc to 'Projects' table", AddNclocToProjects.class)
       .add(10_0_016, "Populate ncloc in 'Projects' table", PopulateNclocForForProjects.class)
-      .add(10_0_015, "Add 'scm_accounts' table", CreateScmAccountsTable.class)
-      .add(10_0_016, "Migrate scm accounts from 'users' to 'scm_accounts' table", MigrateScmAccountsFromUsersToScmAccounts.class)
-      .add(10_0_017, "Add index on 'scm_accounts.scm_account'", CreateIndexForScmAccountOnScmAccountsTable.class)
-      .add(10_0_018, "Add index on 'users.email'", CreateIndexForEmailOnUsersTable.class)
     ;
   }
 }
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v100/MigrateScmAccountsFromUsersToScmAccounts.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v100/MigrateScmAccountsFromUsersToScmAccounts.java
deleted file mode 100644 (file)
index 455a4db..0000000
+++ /dev/null
@@ -1,80 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2023 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.server.platform.db.migration.version.v100;
-
-import com.google.common.annotations.VisibleForTesting;
-import java.sql.SQLException;
-import java.util.Arrays;
-import java.util.Set;
-import org.apache.commons.lang.StringUtils;
-import org.sonar.db.Database;
-import org.sonar.server.platform.db.migration.step.DataChange;
-import org.sonar.server.platform.db.migration.step.MassRowSplitter;
-import org.sonar.server.platform.db.migration.step.Select;
-
-import static java.util.Collections.emptySet;
-import static java.util.stream.Collectors.toSet;
-
-public class MigrateScmAccountsFromUsersToScmAccounts extends DataChange {
-
-  @VisibleForTesting
-  static final char SCM_ACCOUNTS_SEPARATOR_CHAR = '\n';
-
-  public MigrateScmAccountsFromUsersToScmAccounts(Database db) {
-    super(db);
-  }
-
-  @Override
-  protected void execute(Context context) throws SQLException {
-    MassRowSplitter<ScmAccountRow> massRowSplitter = context.prepareMassRowSplitter();
-
-    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)")
-      .setBoolean(1, true);
-
-    massRowSplitter.insert("insert into scm_accounts (user_uuid, scm_account) values (?, ?)");
-
-    massRowSplitter.splitRow(MigrateScmAccountsFromUsersToScmAccounts::toScmAccountRows);
-
-    massRowSplitter.execute((scmAccountRow, insert) -> {
-      insert.setString(1, scmAccountRow.userUuid());
-      insert.setString(2, scmAccountRow.scmAccount());
-      return true;
-    });
-  }
-
-  private static Set<ScmAccountRow> toScmAccountRows(Select.Row row) {
-    try {
-      String userUuid = row.getString(1);
-      String[] scmAccounts = StringUtils.split(row.getString(2), SCM_ACCOUNTS_SEPARATOR_CHAR);
-      if (scmAccounts == null) {
-        return emptySet();
-      }
-      return Arrays.stream(scmAccounts)
-        .map(scmAccount -> new ScmAccountRow(userUuid, scmAccount))
-        .collect(toSet());
-    } catch (SQLException sqlException) {
-      throw new RuntimeException(sqlException);
-    }
-  }
-
-  @VisibleForTesting
-  record ScmAccountRow(String userUuid, String scmAccount) {
-  }
-}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v110/CreateIndexForEmailOnUsersTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v110/CreateIndexForEmailOnUsersTable.java
new file mode 100644 (file)
index 0000000..34930f1
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform.db.migration.version.v110;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.sql.Connection;
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.db.DatabaseUtils;
+import org.sonar.server.platform.db.migration.sql.CreateIndexBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+class CreateIndexForEmailOnUsersTable extends DdlChange {
+
+  @VisibleForTesting
+  static final String INDEX_NAME = "users_email";
+  @VisibleForTesting
+  static final String TABLE_NAME = "users";
+  @VisibleForTesting
+  static final String COLUMN_NAME = "email";
+
+  public CreateIndexForEmailOnUsersTable(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    try (Connection connection = getDatabase().getDataSource().getConnection()) {
+      createIndex(context, connection);
+    }
+  }
+
+  private static void createIndex(Context context, Connection connection) {
+    if (!DatabaseUtils.indexExistsIgnoreCase(TABLE_NAME, INDEX_NAME, connection)) {
+      context.execute(new CreateIndexBuilder()
+        .setTable(TABLE_NAME)
+        .setName(INDEX_NAME)
+        .addColumn(COLUMN_NAME)
+        .setUnique(false)
+        .build());
+    }
+  }
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v110/CreateIndexForScmAccountOnScmAccountsTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v110/CreateIndexForScmAccountOnScmAccountsTable.java
new file mode 100644 (file)
index 0000000..eff8f4b
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform.db.migration.version.v110;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.sql.Connection;
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.db.DatabaseUtils;
+import org.sonar.server.platform.db.migration.sql.CreateIndexBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+import static org.sonar.server.platform.db.migration.version.v110.CreateScmAccountsTable.SCM_ACCOUNT_COLUMN_NAME;
+import static org.sonar.server.platform.db.migration.version.v110.CreateScmAccountsTable.TABLE_NAME;
+
+class CreateIndexForScmAccountOnScmAccountsTable extends DdlChange {
+
+  @VisibleForTesting
+  static final String INDEX_NAME = "scm_accounts_scm_account";
+
+  public CreateIndexForScmAccountOnScmAccountsTable(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    try (Connection connection = getDatabase().getDataSource().getConnection()) {
+      createIndex(context, connection);
+    }
+  }
+
+  private static void createIndex(Context context, Connection connection) {
+    if (!DatabaseUtils.indexExistsIgnoreCase(TABLE_NAME, INDEX_NAME, connection)) {
+      context.execute(new CreateIndexBuilder()
+        .setTable(TABLE_NAME)
+        .setName(INDEX_NAME)
+        .addColumn(SCM_ACCOUNT_COLUMN_NAME)
+        .setUnique(false)
+        .build());
+    }
+  }
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v110/CreateScmAccountsTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v110/CreateScmAccountsTable.java
new file mode 100644 (file)
index 0000000..8ce71c7
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform.db.migration.version.v110;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.sql.CreateTableBuilder;
+import org.sonar.server.platform.db.migration.step.CreateTableChange;
+
+import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.USER_UUID_SIZE;
+import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;
+
+class CreateScmAccountsTable extends CreateTableChange {
+  static final String TABLE_NAME = "scm_accounts";
+  static final String USER_UUID_COLUMN_NAME = "user_uuid";
+  static final String SCM_ACCOUNT_COLUMN_NAME = "scm_account";
+
+  @VisibleForTesting
+  static final int SCM_ACCOUNT_SIZE = 255;
+
+  public CreateScmAccountsTable(Database db) {
+    super(db, TABLE_NAME);
+  }
+
+  @Override
+  public void execute(Context context, String tableName) throws SQLException {
+    context.execute(new CreateTableBuilder(getDialect(), tableName)
+      .addPkColumn(newVarcharColumnDefBuilder().setColumnName(USER_UUID_COLUMN_NAME).setIsNullable(false).setLimit(USER_UUID_SIZE).build())
+      .addPkColumn(newVarcharColumnDefBuilder().setColumnName(SCM_ACCOUNT_COLUMN_NAME).setIsNullable(false).setLimit(SCM_ACCOUNT_SIZE).build())
+      .build());
+  }
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v110/DbVersion110.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v110/DbVersion110.java
new file mode 100644 (file)
index 0000000..92bb13b
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform.db.migration.version.v110;
+
+import org.sonar.server.platform.db.migration.step.MigrationStepRegistry;
+import org.sonar.server.platform.db.migration.version.DbVersion;
+
+// ignoring bad number formatting, as it's indented that we align the migration numbers to SQ versions
+@SuppressWarnings("java:S3937")
+public class DbVersion110 implements DbVersion {
+
+  /**
+   * We use the start of the 10.X cycle as an opportunity to align migration numbers with the SQ version number.
+   * Please follow this pattern:
+   * 10_0_000
+   * 10_0_001
+   * 10_0_002
+   * 10_1_000
+   * 10_1_001
+   * 10_1_002
+   * 10_2_000
+   */
+
+  @Override
+  public void addSteps(MigrationStepRegistry registry) {
+    registry
+      .add(10_1_000, "Add 'scm_accounts' table", CreateScmAccountsTable.class)
+      .add(10_1_001, "Migrate scm accounts from 'users' to 'scm_accounts' table", MigrateScmAccountsFromUsersToScmAccounts.class)
+      .add(10_1_002, "Add index on 'scm_accounts.scm_account'", CreateIndexForScmAccountOnScmAccountsTable.class)
+      .add(10_1_003, "Add index on 'users.email'", CreateIndexForEmailOnUsersTable.class)
+      .add(10_1_004, "Drop 'scm_accounts' column in 'users' table", DropScmAccountsInUsers.class)
+    ;
+  }
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v110/DropScmAccountsInUsers.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v110/DropScmAccountsInUsers.java
new file mode 100644 (file)
index 0000000..ecbdbd0
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform.db.migration.version.v110;
+
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.step.DropColumnChange;
+
+class DropScmAccountsInUsers extends DropColumnChange {
+  static final String TABLE_NAME = "users";
+  static final String COLUMN_NAME = "scm_accounts";
+
+  public DropScmAccountsInUsers(Database db) {
+    super(db, TABLE_NAME, COLUMN_NAME);
+  }
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v110/MigrateScmAccountsFromUsersToScmAccounts.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v110/MigrateScmAccountsFromUsersToScmAccounts.java
new file mode 100644 (file)
index 0000000..1a96820
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform.db.migration.version.v110;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.Set;
+import org.apache.commons.lang.StringUtils;
+import org.sonar.db.Database;
+import org.sonar.db.DatabaseUtils;
+import org.sonar.server.platform.db.migration.step.DataChange;
+import org.sonar.server.platform.db.migration.step.MassRowSplitter;
+import org.sonar.server.platform.db.migration.step.Select;
+
+import static java.util.Collections.emptySet;
+import static java.util.stream.Collectors.toSet;
+
+class MigrateScmAccountsFromUsersToScmAccounts extends DataChange {
+
+  @VisibleForTesting
+  static final char SCM_ACCOUNTS_SEPARATOR_CHAR = '\n';
+
+  public MigrateScmAccountsFromUsersToScmAccounts(Database db) {
+    super(db);
+  }
+
+  @Override
+  protected void execute(Context context) throws SQLException {
+    if (isScmColumnDropped()) {
+      return;
+    }
+    migrateData(context);
+  }
+
+  private boolean isScmColumnDropped() throws SQLException {
+    try (var connection = getDatabase().getDataSource().getConnection()) {
+      return !DatabaseUtils.tableColumnExists(connection, DropScmAccountsInUsers.TABLE_NAME, DropScmAccountsInUsers.COLUMN_NAME);
+    }
+  }
+
+  private static void migrateData(Context context) throws SQLException {
+    MassRowSplitter<ScmAccountRow> massRowSplitter = context.prepareMassRowSplitter();
+
+    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)")
+      .setBoolean(1, true);
+
+    massRowSplitter.insert("insert into scm_accounts (user_uuid, scm_account) values (?, ?)");
+
+    massRowSplitter.splitRow(MigrateScmAccountsFromUsersToScmAccounts::toScmAccountRows);
+
+    massRowSplitter.execute((scmAccountRow, insert) -> {
+      insert.setString(1, scmAccountRow.userUuid());
+      insert.setString(2, scmAccountRow.scmAccount());
+      return true;
+    });
+  }
+
+  private static Set<ScmAccountRow> toScmAccountRows(Select.Row row) {
+    try {
+      String userUuid = row.getString(1);
+      String[] scmAccounts = StringUtils.split(row.getString(2), SCM_ACCOUNTS_SEPARATOR_CHAR);
+      if (scmAccounts == null) {
+        return emptySet();
+      }
+      return Arrays.stream(scmAccounts)
+        .map(scmAccount -> new ScmAccountRow(userUuid, scmAccount))
+        .collect(toSet());
+    } catch (SQLException sqlException) {
+      throw new RuntimeException(sqlException);
+    }
+  }
+
+  @VisibleForTesting
+  record ScmAccountRow(String userUuid, String scmAccount) {
+  }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v100/CreateIndexForEmailOnUsersTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v100/CreateIndexForEmailOnUsersTableTest.java
deleted file mode 100644 (file)
index 02b3683..0000000
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2023 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.server.platform.db.migration.version.v100;
-
-import java.sql.SQLException;
-import org.junit.Rule;
-import org.junit.Test;
-import org.sonar.db.CoreDbTester;
-
-import static org.sonar.server.platform.db.migration.version.v100.CreateIndexForEmailOnUsersTable.COLUMN_NAME;
-import static org.sonar.server.platform.db.migration.version.v100.CreateIndexForEmailOnUsersTable.INDEX_NAME;
-import static org.sonar.server.platform.db.migration.version.v100.CreateIndexForEmailOnUsersTable.TABLE_NAME;
-
-public class CreateIndexForEmailOnUsersTableTest {
-  @Rule
-  public final CoreDbTester db = CoreDbTester.createForSchema(CreateIndexForEmailOnUsersTableTest.class, "schema.sql");
-
-  private final CreateIndexForEmailOnUsersTable createIndexForEmailOnUsersTable = new CreateIndexForEmailOnUsersTable(db.database());
-
-  @Test
-  public void migration_should_create_index() throws SQLException {
-    db.assertIndexDoesNotExist(TABLE_NAME, INDEX_NAME);
-
-    createIndexForEmailOnUsersTable.execute();
-
-    db.assertIndex(TABLE_NAME, INDEX_NAME, COLUMN_NAME);
-  }
-
-  @Test
-  public void migration_should_be_reentrant() throws SQLException {
-    createIndexForEmailOnUsersTable.execute();
-    createIndexForEmailOnUsersTable.execute();
-
-    db.assertIndex(TABLE_NAME, INDEX_NAME, COLUMN_NAME);
-  }
-}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v100/CreateIndexForScmAccountOnScmAccountsTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v100/CreateIndexForScmAccountOnScmAccountsTableTest.java
deleted file mode 100644 (file)
index 3a34cfb..0000000
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2023 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.server.platform.db.migration.version.v100;
-
-import java.sql.SQLException;
-import org.junit.Rule;
-import org.junit.Test;
-import org.sonar.db.CoreDbTester;
-
-import static org.sonar.server.platform.db.migration.version.v100.CreateIndexForScmAccountOnScmAccountsTable.INDEX_NAME;
-import static org.sonar.server.platform.db.migration.version.v100.CreateScmAccountsTable.SCM_ACCOUNT_COLUMN_NAME;
-import static org.sonar.server.platform.db.migration.version.v100.CreateScmAccountsTable.TABLE_NAME;
-
-public class CreateIndexForScmAccountOnScmAccountsTableTest {
-  @Rule
-  public final CoreDbTester db = CoreDbTester.createForSchema(CreateIndexForScmAccountOnScmAccountsTableTest.class, "schema.sql");
-
-  private final CreateIndexForScmAccountOnScmAccountsTable createIndexForScmAccountOnScmAccountsTable = new CreateIndexForScmAccountOnScmAccountsTable(db.database());
-
-  @Test
-  public void migration_should_create_index() throws SQLException {
-    db.assertIndexDoesNotExist(TABLE_NAME, INDEX_NAME);
-
-    createIndexForScmAccountOnScmAccountsTable.execute();
-
-    db.assertIndex(TABLE_NAME, INDEX_NAME, SCM_ACCOUNT_COLUMN_NAME);
-  }
-
-  @Test
-  public void migration_should_be_reentrant() throws SQLException {
-    createIndexForScmAccountOnScmAccountsTable.execute();
-    createIndexForScmAccountOnScmAccountsTable.execute();
-
-    db.assertIndex(TABLE_NAME, INDEX_NAME, SCM_ACCOUNT_COLUMN_NAME);
-  }
-}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v100/CreateScmAccountsTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v100/CreateScmAccountsTableTest.java
deleted file mode 100644 (file)
index 467606b..0000000
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2023 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.server.platform.db.migration.version.v100;
-
-import java.sql.SQLException;
-import java.sql.Types;
-import org.junit.Rule;
-import org.junit.Test;
-import org.sonar.db.CoreDbTester;
-import org.sonar.server.platform.db.migration.step.DdlChange;
-
-import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.USER_UUID_SIZE;
-import static org.sonar.server.platform.db.migration.version.v100.CreateScmAccountsTable.SCM_ACCOUNT_COLUMN_NAME;
-import static org.sonar.server.platform.db.migration.version.v100.CreateScmAccountsTable.SCM_ACCOUNT_SIZE;
-import static org.sonar.server.platform.db.migration.version.v100.CreateScmAccountsTable.TABLE_NAME;
-import static org.sonar.server.platform.db.migration.version.v100.CreateScmAccountsTable.USER_UUID_COLUMN_NAME;
-
-public class CreateScmAccountsTableTest {
-  @Rule
-  public final CoreDbTester db = CoreDbTester.createEmpty();
-
-  private final DdlChange createScmAccountsTable = new CreateScmAccountsTable(db.database());
-
-  @Test
-  public void migration_should_create_a_table() throws SQLException {
-    db.assertTableDoesNotExist(TABLE_NAME);
-
-    createScmAccountsTable.execute();
-
-    db.assertTableExists(TABLE_NAME);
-    db.assertColumnDefinition(TABLE_NAME, USER_UUID_COLUMN_NAME, Types.VARCHAR, USER_UUID_SIZE, false);
-    db.assertColumnDefinition(TABLE_NAME, SCM_ACCOUNT_COLUMN_NAME, Types.VARCHAR, SCM_ACCOUNT_SIZE, false);
-    db.assertPrimaryKey(TABLE_NAME, "pk_scm_accounts", USER_UUID_COLUMN_NAME, SCM_ACCOUNT_COLUMN_NAME);
-  }
-
-  @Test
-  public void migration_should_be_reentrant() throws SQLException {
-    db.assertTableDoesNotExist(TABLE_NAME);
-
-    createScmAccountsTable.execute();
-    // re-entrant
-    createScmAccountsTable.execute();
-
-    db.assertTableExists(TABLE_NAME);
-  }
-}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v100/MigrateScmAccountsFromUsersToScmAccountsTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v100/MigrateScmAccountsFromUsersToScmAccountsTest.java
deleted file mode 100644 (file)
index bc5bfd9..0000000
+++ /dev/null
@@ -1,176 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2023 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.server.platform.db.migration.version.v100;
-
-import java.sql.SQLException;
-import java.util.HashMap;
-import java.util.Locale;
-import java.util.Map;
-import java.util.Set;
-import javax.annotation.Nullable;
-import org.junit.Rule;
-import org.junit.Test;
-import org.sonar.core.util.UuidFactory;
-import org.sonar.core.util.UuidFactoryFast;
-import org.sonar.db.CoreDbTester;
-import org.sonar.server.platform.db.migration.step.DataChange;
-import org.sonar.server.platform.db.migration.version.v100.MigrateScmAccountsFromUsersToScmAccounts.ScmAccountRow;
-
-import static java.lang.String.format;
-import static java.util.stream.Collectors.toSet;
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.sonar.server.platform.db.migration.version.v100.MigrateScmAccountsFromUsersToScmAccounts.SCM_ACCOUNTS_SEPARATOR_CHAR;
-
-public class MigrateScmAccountsFromUsersToScmAccountsTest {
-
-  private static final UuidFactory UUID_FACTORY = UuidFactoryFast.getInstance();
-  private static final String SCM_ACCOUNT1 = "scmaccount";
-  private static final String SCM_ACCOUNT2 = "scmaccount2";
-  private static final String SCM_ACCOUNT_CAMELCASE = "scmAccount3";
-
-  @Rule
-  public final CoreDbTester db = CoreDbTester.createForSchema(MigrateScmAccountsFromUsersToScmAccountsTest.class, "schema.sql");
-
-  private final DataChange migrateScmAccountsFromUsersToScmAccounts = new MigrateScmAccountsFromUsersToScmAccounts(db.database());
-
-  @Test
-  public void execute_whenUserHasNullScmAccounts_doNotInsertInScmAccounts() throws SQLException {
-    insertUserAndGetUuid(null);
-
-    migrateScmAccountsFromUsersToScmAccounts.execute();
-
-    Set<ScmAccountRow> scmAccounts = findAllScmAccounts();
-    assertThat(scmAccounts).isEmpty();
-  }
-
-  @Test
-  public void execute_whenUserHasEmptyScmAccounts_doNotInsertInScmAccounts() throws SQLException {
-    insertUserAndGetUuid("");
-
-    migrateScmAccountsFromUsersToScmAccounts.execute();
-
-    Set<ScmAccountRow> scmAccounts = findAllScmAccounts();
-    assertThat(scmAccounts).isEmpty();
-  }
-
-  @Test
-  public void execute_whenUserHasEmptyScmAccountsWithOneSeparator_doNotInsertInScmAccounts() throws SQLException {
-    insertUserAndGetUuid(String.valueOf(SCM_ACCOUNTS_SEPARATOR_CHAR));
-
-    migrateScmAccountsFromUsersToScmAccounts.execute();
-
-    Set<ScmAccountRow> scmAccounts = findAllScmAccounts();
-    assertThat(scmAccounts).isEmpty();
-  }
-
-  @Test
-  public void execute_whenUserHasEmptyScmAccountsWithTwoSeparators_doNotInsertInScmAccounts() throws SQLException {
-    insertUserAndGetUuid(SCM_ACCOUNTS_SEPARATOR_CHAR + String.valueOf(SCM_ACCOUNTS_SEPARATOR_CHAR));
-
-    migrateScmAccountsFromUsersToScmAccounts.execute();
-
-    Set<ScmAccountRow> scmAccounts = findAllScmAccounts();
-    assertThat(scmAccounts).isEmpty();
-  }
-
-  @Test
-  public void execute_whenUserHasOneScmAccountWithoutSeparator_insertsInScmAccounts() throws SQLException {
-    String userUuid = insertUserAndGetUuid(SCM_ACCOUNT1);
-
-    migrateScmAccountsFromUsersToScmAccounts.execute();
-
-    Set<ScmAccountRow> scmAccounts = findAllScmAccounts();
-    assertThat(scmAccounts).containsExactly(new ScmAccountRow(userUuid, SCM_ACCOUNT1));
-  }
-
-  @Test
-  public void execute_whenUserHasOneScmAccountWithSeparators_insertsInScmAccounts() throws SQLException {
-    String userUuid = insertUserAndGetUuid(format("%s%s%s", SCM_ACCOUNTS_SEPARATOR_CHAR, SCM_ACCOUNT1, SCM_ACCOUNTS_SEPARATOR_CHAR));
-
-    migrateScmAccountsFromUsersToScmAccounts.execute();
-
-    Set<ScmAccountRow> scmAccounts = findAllScmAccounts();
-    assertThat(scmAccounts).containsExactly(new ScmAccountRow(userUuid, SCM_ACCOUNT1));
-  }
-
-  @Test
-  public void execute_whenUserHasOneScmAccountWithMixedCase_insertsInScmAccountsInLowerCase() throws SQLException {
-    String userUuid = insertUserAndGetUuid(format("%s%s%s", SCM_ACCOUNTS_SEPARATOR_CHAR, SCM_ACCOUNT_CAMELCASE, SCM_ACCOUNTS_SEPARATOR_CHAR));
-
-    migrateScmAccountsFromUsersToScmAccounts.execute();
-
-    Set<ScmAccountRow> scmAccounts = findAllScmAccounts();
-    assertThat(scmAccounts).containsExactly(new ScmAccountRow(userUuid, SCM_ACCOUNT_CAMELCASE.toLowerCase(Locale.ENGLISH)));
-  }
-
-  @Test
-  public void execute_whenUserHasTwoScmAccount_insertsInScmAccounts() throws SQLException {
-    String userUuid = insertUserAndGetUuid(format("%s%s%s%s%s",
-      SCM_ACCOUNTS_SEPARATOR_CHAR, SCM_ACCOUNT1, SCM_ACCOUNTS_SEPARATOR_CHAR, SCM_ACCOUNT2, SCM_ACCOUNTS_SEPARATOR_CHAR));
-
-    migrateScmAccountsFromUsersToScmAccounts.execute();
-
-    Set<ScmAccountRow> scmAccounts = findAllScmAccounts();
-    assertThat(scmAccounts).containsExactlyInAnyOrder(
-      new ScmAccountRow(userUuid, SCM_ACCOUNT1),
-      new ScmAccountRow(userUuid, SCM_ACCOUNT2)
-    );
-  }
-
-  @Test
-  public void migration_should_be_reentrant() throws SQLException {
-    String userUuid = insertUserAndGetUuid(SCM_ACCOUNT1);
-
-    migrateScmAccountsFromUsersToScmAccounts.execute();
-    migrateScmAccountsFromUsersToScmAccounts.execute();
-
-    Set<ScmAccountRow> scmAccounts = findAllScmAccounts();
-    assertThat(scmAccounts).containsExactly(new ScmAccountRow(userUuid, SCM_ACCOUNT1));
-  }
-
-
-  private Set<ScmAccountRow> findAllScmAccounts() {
-    Set<ScmAccountRow> scmAccounts = db.select("select user_uuid USERUUID, scm_account SCMACCOUNT from scm_accounts")
-      .stream()
-      .map(row -> new ScmAccountRow((String) row.get("USERUUID"), (String) row.get("SCMACCOUNT")))
-      .collect(toSet());
-    return scmAccounts;
-  }
-
-  private String insertUserAndGetUuid(@Nullable String scmAccounts) {
-
-    Map<String, Object> map = new HashMap<>();
-    String uuid = UUID_FACTORY.create();
-    String login = "login_" + uuid;
-    map.put("UUID", uuid);
-    map.put("LOGIN", login);
-    map.put("HASH_METHOD", "tagada");
-    map.put("EXTERNAL_LOGIN", login);
-    map.put("EXTERNAL_IDENTITY_PROVIDER", "sonarqube");
-    map.put("EXTERNAL_ID", login);
-    map.put("CREATED_AT", System.currentTimeMillis());
-    map.put("RESET_PASSWORD", false);
-    map.put("USER_LOCAL", true);
-    map.put("SCM_ACCOUNTS", scmAccounts);
-    db.executeInsert("users", map);
-    return uuid;
-  }
-
-}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v110/CreateIndexForEmailOnUsersTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v110/CreateIndexForEmailOnUsersTableTest.java
new file mode 100644 (file)
index 0000000..2e2287e
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform.db.migration.version.v110;
+
+import java.sql.SQLException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+
+import static org.sonar.server.platform.db.migration.version.v110.CreateIndexForEmailOnUsersTable.COLUMN_NAME;
+import static org.sonar.server.platform.db.migration.version.v110.CreateIndexForEmailOnUsersTable.INDEX_NAME;
+import static org.sonar.server.platform.db.migration.version.v110.CreateIndexForEmailOnUsersTable.TABLE_NAME;
+
+public class CreateIndexForEmailOnUsersTableTest {
+  @Rule
+  public final CoreDbTester db = CoreDbTester.createForSchema(CreateIndexForEmailOnUsersTableTest.class, "schema.sql");
+
+  private final CreateIndexForEmailOnUsersTable createIndexForEmailOnUsersTable = new CreateIndexForEmailOnUsersTable(db.database());
+
+  @Test
+  public void migration_should_create_index() throws SQLException {
+    db.assertIndexDoesNotExist(TABLE_NAME, INDEX_NAME);
+
+    createIndexForEmailOnUsersTable.execute();
+
+    db.assertIndex(TABLE_NAME, INDEX_NAME, COLUMN_NAME);
+  }
+
+  @Test
+  public void migration_should_be_reentrant() throws SQLException {
+    createIndexForEmailOnUsersTable.execute();
+    createIndexForEmailOnUsersTable.execute();
+
+    db.assertIndex(TABLE_NAME, INDEX_NAME, COLUMN_NAME);
+  }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v110/CreateIndexForScmAccountOnScmAccountsTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v110/CreateIndexForScmAccountOnScmAccountsTableTest.java
new file mode 100644 (file)
index 0000000..8ccc06c
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform.db.migration.version.v110;
+
+import java.sql.SQLException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+
+import static org.sonar.server.platform.db.migration.version.v110.CreateIndexForScmAccountOnScmAccountsTable.INDEX_NAME;
+import static org.sonar.server.platform.db.migration.version.v110.CreateScmAccountsTable.SCM_ACCOUNT_COLUMN_NAME;
+import static org.sonar.server.platform.db.migration.version.v110.CreateScmAccountsTable.TABLE_NAME;
+
+public class CreateIndexForScmAccountOnScmAccountsTableTest {
+  @Rule
+  public final CoreDbTester db = CoreDbTester.createForSchema(CreateIndexForScmAccountOnScmAccountsTableTest.class, "schema.sql");
+
+  private final CreateIndexForScmAccountOnScmAccountsTable createIndexForScmAccountOnScmAccountsTable = new CreateIndexForScmAccountOnScmAccountsTable(db.database());
+
+  @Test
+  public void migration_should_create_index() throws SQLException {
+    db.assertIndexDoesNotExist(TABLE_NAME, INDEX_NAME);
+
+    createIndexForScmAccountOnScmAccountsTable.execute();
+
+    db.assertIndex(TABLE_NAME, INDEX_NAME, SCM_ACCOUNT_COLUMN_NAME);
+  }
+
+  @Test
+  public void migration_should_be_reentrant() throws SQLException {
+    createIndexForScmAccountOnScmAccountsTable.execute();
+    createIndexForScmAccountOnScmAccountsTable.execute();
+
+    db.assertIndex(TABLE_NAME, INDEX_NAME, SCM_ACCOUNT_COLUMN_NAME);
+  }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v110/CreateScmAccountsTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v110/CreateScmAccountsTableTest.java
new file mode 100644 (file)
index 0000000..815d0e5
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform.db.migration.version.v110;
+
+import java.sql.SQLException;
+import java.sql.Types;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+import org.sonar.server.platform.db.migration.version.v110.CreateScmAccountsTable;
+
+import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.USER_UUID_SIZE;
+import static org.sonar.server.platform.db.migration.version.v110.CreateScmAccountsTable.SCM_ACCOUNT_COLUMN_NAME;
+import static org.sonar.server.platform.db.migration.version.v110.CreateScmAccountsTable.SCM_ACCOUNT_SIZE;
+import static org.sonar.server.platform.db.migration.version.v110.CreateScmAccountsTable.TABLE_NAME;
+import static org.sonar.server.platform.db.migration.version.v110.CreateScmAccountsTable.USER_UUID_COLUMN_NAME;
+
+public class CreateScmAccountsTableTest {
+  @Rule
+  public final CoreDbTester db = CoreDbTester.createEmpty();
+
+  private final DdlChange createScmAccountsTable = new CreateScmAccountsTable(db.database());
+
+  @Test
+  public void migration_should_create_a_table() throws SQLException {
+    db.assertTableDoesNotExist(TABLE_NAME);
+
+    createScmAccountsTable.execute();
+
+    db.assertTableExists(TABLE_NAME);
+    db.assertColumnDefinition(TABLE_NAME, USER_UUID_COLUMN_NAME, Types.VARCHAR, USER_UUID_SIZE, false);
+    db.assertColumnDefinition(TABLE_NAME, SCM_ACCOUNT_COLUMN_NAME, Types.VARCHAR, SCM_ACCOUNT_SIZE, false);
+    db.assertPrimaryKey(TABLE_NAME, "pk_scm_accounts", USER_UUID_COLUMN_NAME, SCM_ACCOUNT_COLUMN_NAME);
+  }
+
+  @Test
+  public void migration_should_be_reentrant() throws SQLException {
+    db.assertTableDoesNotExist(TABLE_NAME);
+
+    createScmAccountsTable.execute();
+    // re-entrant
+    createScmAccountsTable.execute();
+
+    db.assertTableExists(TABLE_NAME);
+  }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v110/DbVersion110Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v110/DbVersion110Test.java
new file mode 100644 (file)
index 0000000..6cff42c
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform.db.migration.version.v110;
+
+import org.junit.Test;
+
+import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMigrationNotEmpty;
+import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMinimumMigrationNumber;
+
+public class DbVersion110Test {
+  private final DbVersion110 underTest = new DbVersion110();
+
+  @Test
+  public void migrationNumber_starts_at_10_1_000() {
+    verifyMinimumMigrationNumber(underTest, 10_1_000);
+  }
+
+  @Test
+  public void verify_migration_is_not_empty() {
+    verifyMigrationNotEmpty(underTest);
+  }
+
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v110/DropScmAccountsInUsersTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v110/DropScmAccountsInUsersTest.java
new file mode 100644 (file)
index 0000000..db354f3
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform.db.migration.version.v110;
+
+import java.sql.SQLException;
+import java.sql.Types;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+import static org.sonar.server.platform.db.migration.version.v110.DropScmAccountsInUsers.COLUMN_NAME;
+import static org.sonar.server.platform.db.migration.version.v110.DropScmAccountsInUsers.TABLE_NAME;
+
+public class DropScmAccountsInUsersTest {
+
+  @Rule
+  public final CoreDbTester db = CoreDbTester.createForSchema(DropScmAccountsInUsersTest.class, "schema.sql");
+  private final DdlChange dropScmAccountsInUsers = new DropScmAccountsInUsers(db.database());
+
+  @Test
+  public void drops_column() throws SQLException {
+    db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, Types.VARCHAR, 4000, true);
+    dropScmAccountsInUsers.execute();
+    db.assertColumnDoesNotExist(TABLE_NAME, COLUMN_NAME);
+  }
+
+  @Test
+  public void migration_is_reentrant() throws SQLException {
+    db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, Types.VARCHAR, 4000, true);
+    dropScmAccountsInUsers.execute();
+    dropScmAccountsInUsers.execute();
+    db.assertColumnDoesNotExist(TABLE_NAME, COLUMN_NAME);
+  }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v110/MigrateScmAccountsFromUsersToScmAccountsTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v110/MigrateScmAccountsFromUsersToScmAccountsTest.java
new file mode 100644 (file)
index 0000000..c5be86e
--- /dev/null
@@ -0,0 +1,184 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform.db.migration.version.v110;
+
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+import javax.annotation.Nullable;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.core.util.UuidFactory;
+import org.sonar.core.util.UuidFactoryFast;
+import org.sonar.db.CoreDbTester;
+import org.sonar.server.platform.db.migration.step.DataChange;
+import org.sonar.server.platform.db.migration.version.v110.MigrateScmAccountsFromUsersToScmAccounts.ScmAccountRow;
+
+import static java.lang.String.format;
+import static java.util.stream.Collectors.toSet;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatNoException;
+import static org.sonar.server.platform.db.migration.version.v110.MigrateScmAccountsFromUsersToScmAccounts.SCM_ACCOUNTS_SEPARATOR_CHAR;
+
+public class MigrateScmAccountsFromUsersToScmAccountsTest {
+
+  private static final UuidFactory UUID_FACTORY = UuidFactoryFast.getInstance();
+  private static final String SCM_ACCOUNT1 = "scmaccount";
+  private static final String SCM_ACCOUNT2 = "scmaccount2";
+  private static final String SCM_ACCOUNT_CAMELCASE = "scmAccount3";
+
+  @Rule
+  public final CoreDbTester db = CoreDbTester.createForSchema(MigrateScmAccountsFromUsersToScmAccountsTest.class, "schema.sql");
+
+  private final DataChange migrateScmAccountsFromUsersToScmAccounts = new MigrateScmAccountsFromUsersToScmAccounts(db.database());
+
+  @Test
+  public void execute_whenUserHasNullScmAccounts_doNotInsertInScmAccounts() throws SQLException {
+    insertUserAndGetUuid(null);
+
+    migrateScmAccountsFromUsersToScmAccounts.execute();
+
+    Set<ScmAccountRow> scmAccounts = findAllScmAccounts();
+    assertThat(scmAccounts).isEmpty();
+  }
+
+  @Test
+  public void execute_whenUserHasEmptyScmAccounts_doNotInsertInScmAccounts() throws SQLException {
+    insertUserAndGetUuid("");
+
+    migrateScmAccountsFromUsersToScmAccounts.execute();
+
+    Set<ScmAccountRow> scmAccounts = findAllScmAccounts();
+    assertThat(scmAccounts).isEmpty();
+  }
+
+  @Test
+  public void execute_whenUserHasEmptyScmAccountsWithOneSeparator_doNotInsertInScmAccounts() throws SQLException {
+    insertUserAndGetUuid(String.valueOf(SCM_ACCOUNTS_SEPARATOR_CHAR));
+
+    migrateScmAccountsFromUsersToScmAccounts.execute();
+
+    Set<ScmAccountRow> scmAccounts = findAllScmAccounts();
+    assertThat(scmAccounts).isEmpty();
+  }
+
+  @Test
+  public void execute_whenUserHasEmptyScmAccountsWithTwoSeparators_doNotInsertInScmAccounts() throws SQLException {
+    insertUserAndGetUuid(SCM_ACCOUNTS_SEPARATOR_CHAR + String.valueOf(SCM_ACCOUNTS_SEPARATOR_CHAR));
+
+    migrateScmAccountsFromUsersToScmAccounts.execute();
+
+    Set<ScmAccountRow> scmAccounts = findAllScmAccounts();
+    assertThat(scmAccounts).isEmpty();
+  }
+
+  @Test
+  public void execute_whenUserHasOneScmAccountWithoutSeparator_insertsInScmAccounts() throws SQLException {
+    String userUuid = insertUserAndGetUuid(SCM_ACCOUNT1);
+
+    migrateScmAccountsFromUsersToScmAccounts.execute();
+
+    Set<ScmAccountRow> scmAccounts = findAllScmAccounts();
+    assertThat(scmAccounts).containsExactly(new ScmAccountRow(userUuid, SCM_ACCOUNT1));
+  }
+
+  @Test
+  public void execute_whenUserHasOneScmAccountWithSeparators_insertsInScmAccounts() throws SQLException {
+    String userUuid = insertUserAndGetUuid(format("%s%s%s", SCM_ACCOUNTS_SEPARATOR_CHAR, SCM_ACCOUNT1, SCM_ACCOUNTS_SEPARATOR_CHAR));
+
+    migrateScmAccountsFromUsersToScmAccounts.execute();
+
+    Set<ScmAccountRow> scmAccounts = findAllScmAccounts();
+    assertThat(scmAccounts).containsExactly(new ScmAccountRow(userUuid, SCM_ACCOUNT1));
+  }
+
+  @Test
+  public void execute_whenUserHasOneScmAccountWithMixedCase_insertsInScmAccountsInLowerCase() throws SQLException {
+    String userUuid = insertUserAndGetUuid(format("%s%s%s", SCM_ACCOUNTS_SEPARATOR_CHAR, SCM_ACCOUNT_CAMELCASE, SCM_ACCOUNTS_SEPARATOR_CHAR));
+
+    migrateScmAccountsFromUsersToScmAccounts.execute();
+
+    Set<ScmAccountRow> scmAccounts = findAllScmAccounts();
+    assertThat(scmAccounts).containsExactly(new ScmAccountRow(userUuid, SCM_ACCOUNT_CAMELCASE.toLowerCase(Locale.ENGLISH)));
+  }
+
+  @Test
+  public void execute_whenUserHasTwoScmAccount_insertsInScmAccounts() throws SQLException {
+    String userUuid = insertUserAndGetUuid(format("%s%s%s%s%s",
+      SCM_ACCOUNTS_SEPARATOR_CHAR, SCM_ACCOUNT1, SCM_ACCOUNTS_SEPARATOR_CHAR, SCM_ACCOUNT2, SCM_ACCOUNTS_SEPARATOR_CHAR));
+
+    migrateScmAccountsFromUsersToScmAccounts.execute();
+
+    Set<ScmAccountRow> scmAccounts = findAllScmAccounts();
+    assertThat(scmAccounts).containsExactlyInAnyOrder(
+      new ScmAccountRow(userUuid, SCM_ACCOUNT1),
+      new ScmAccountRow(userUuid, SCM_ACCOUNT2)
+    );
+  }
+
+  @Test
+  public void migration_should_be_reentrant() throws SQLException {
+    String userUuid = insertUserAndGetUuid(SCM_ACCOUNT1);
+
+    migrateScmAccountsFromUsersToScmAccounts.execute();
+    migrateScmAccountsFromUsersToScmAccounts.execute();
+
+    Set<ScmAccountRow> scmAccounts = findAllScmAccounts();
+    assertThat(scmAccounts).containsExactly(new ScmAccountRow(userUuid, SCM_ACCOUNT1));
+  }
+
+  @Test
+  public void migration_should_be_reentrant_if_scm_account_column_dropped() {
+    db.executeDdl("alter table users drop column scm_accounts");
+
+    assertThatNoException().isThrownBy(migrateScmAccountsFromUsersToScmAccounts::execute);
+  }
+
+
+  private Set<ScmAccountRow> findAllScmAccounts() {
+    Set<ScmAccountRow> scmAccounts = db.select("select user_uuid USERUUID, scm_account SCMACCOUNT from scm_accounts")
+      .stream()
+      .map(row -> new ScmAccountRow((String) row.get("USERUUID"), (String) row.get("SCMACCOUNT")))
+      .collect(toSet());
+    return scmAccounts;
+  }
+
+  private String insertUserAndGetUuid(@Nullable String scmAccounts) {
+
+    Map<String, Object> map = new HashMap<>();
+    String uuid = UUID_FACTORY.create();
+    String login = "login_" + uuid;
+    map.put("UUID", uuid);
+    map.put("LOGIN", login);
+    map.put("HASH_METHOD", "tagada");
+    map.put("EXTERNAL_LOGIN", login);
+    map.put("EXTERNAL_IDENTITY_PROVIDER", "sonarqube");
+    map.put("EXTERNAL_ID", login);
+    map.put("CREATED_AT", System.currentTimeMillis());
+    map.put("RESET_PASSWORD", false);
+    map.put("USER_LOCAL", true);
+    map.put("SCM_ACCOUNTS", scmAccounts);
+    db.executeInsert("users", map);
+    return uuid;
+  }
+
+}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v100/CreateIndexForEmailOnUsersTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v100/CreateIndexForEmailOnUsersTableTest/schema.sql
deleted file mode 100644 (file)
index f2af204..0000000
+++ /dev/null
@@ -1,27 +0,0 @@
-CREATE TABLE "USERS"(
-    "UUID" CHARACTER VARYING(255) NOT NULL,
-    "LOGIN" CHARACTER VARYING(255) NOT NULL,
-    "NAME" CHARACTER VARYING(200),
-    "EMAIL" CHARACTER VARYING(100),
-    "CRYPTED_PASSWORD" CHARACTER VARYING(100),
-    "SALT" CHARACTER VARYING(40),
-    "HASH_METHOD" CHARACTER VARYING(10),
-    "ACTIVE" BOOLEAN DEFAULT TRUE,
-    "SCM_ACCOUNTS" CHARACTER VARYING(4000),
-    "EXTERNAL_LOGIN" CHARACTER VARYING(255) NOT NULL,
-    "EXTERNAL_IDENTITY_PROVIDER" CHARACTER VARYING(100) NOT NULL,
-    "EXTERNAL_ID" CHARACTER VARYING(255) NOT NULL,
-    "USER_LOCAL" BOOLEAN NOT NULL,
-    "HOMEPAGE_TYPE" CHARACTER VARYING(40),
-    "HOMEPAGE_PARAMETER" CHARACTER VARYING(40),
-    "LAST_CONNECTION_DATE" BIGINT,
-    "CREATED_AT" BIGINT,
-    "UPDATED_AT" BIGINT,
-    "RESET_PASSWORD" BOOLEAN NOT NULL,
-    "LAST_SONARLINT_CONNECTION" BIGINT
-);
-ALTER TABLE "USERS" ADD CONSTRAINT "PK_USERS" PRIMARY KEY("UUID");
-CREATE UNIQUE INDEX "USERS_LOGIN" ON "USERS"("LOGIN" NULLS FIRST);
-CREATE INDEX "USERS_UPDATED_AT" ON "USERS"("UPDATED_AT" NULLS FIRST);
-CREATE UNIQUE INDEX "UNIQ_EXTERNAL_ID" ON "USERS"("EXTERNAL_IDENTITY_PROVIDER" NULLS FIRST, "EXTERNAL_ID" NULLS FIRST);
-CREATE UNIQUE INDEX "UNIQ_EXTERNAL_LOGIN" ON "USERS"("EXTERNAL_IDENTITY_PROVIDER" NULLS FIRST, "EXTERNAL_LOGIN" NULLS FIRST);
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v100/CreateIndexForScmAccountOnScmAccountsTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v100/CreateIndexForScmAccountOnScmAccountsTableTest/schema.sql
deleted file mode 100644 (file)
index 7a113bb..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-CREATE TABLE "SCM_ACCOUNTS"(
-    "USER_UUID" CHARACTER VARYING(255) NOT NULL,
-    "SCM_ACCOUNT" CHARACTER VARYING(255) NOT NULL
-);
-ALTER TABLE "SCM_ACCOUNTS" ADD CONSTRAINT "PK_SCM_ACCOUNTS" PRIMARY KEY("USER_UUID", "SCM_ACCOUNT");
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v100/CreateIndexesForScmAccountsTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v100/CreateIndexesForScmAccountsTest/schema.sql
deleted file mode 100644 (file)
index 7a113bb..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-CREATE TABLE "SCM_ACCOUNTS"(
-    "USER_UUID" CHARACTER VARYING(255) NOT NULL,
-    "SCM_ACCOUNT" CHARACTER VARYING(255) NOT NULL
-);
-ALTER TABLE "SCM_ACCOUNTS" ADD CONSTRAINT "PK_SCM_ACCOUNTS" PRIMARY KEY("USER_UUID", "SCM_ACCOUNT");
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v100/MigrateScmAccountsFromUsersToScmAccountsTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v100/MigrateScmAccountsFromUsersToScmAccountsTest/schema.sql
deleted file mode 100644 (file)
index 2f34178..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-CREATE TABLE "USERS"(
-    "UUID" CHARACTER VARYING(255) NOT NULL,
-    "LOGIN" CHARACTER VARYING(255) NOT NULL,
-    "NAME" CHARACTER VARYING(200),
-    "EMAIL" CHARACTER VARYING(100),
-    "CRYPTED_PASSWORD" CHARACTER VARYING(100),
-    "SALT" CHARACTER VARYING(40),
-    "HASH_METHOD" CHARACTER VARYING(10),
-    "ACTIVE" BOOLEAN DEFAULT TRUE,
-    "SCM_ACCOUNTS" CHARACTER VARYING(4000),
-    "EXTERNAL_LOGIN" CHARACTER VARYING(255) NOT NULL,
-    "EXTERNAL_IDENTITY_PROVIDER" CHARACTER VARYING(100) NOT NULL,
-    "EXTERNAL_ID" CHARACTER VARYING(255) NOT NULL,
-    "USER_LOCAL" BOOLEAN NOT NULL,
-    "HOMEPAGE_TYPE" CHARACTER VARYING(40),
-    "HOMEPAGE_PARAMETER" CHARACTER VARYING(40),
-    "LAST_CONNECTION_DATE" BIGINT,
-    "CREATED_AT" BIGINT,
-    "UPDATED_AT" BIGINT,
-    "RESET_PASSWORD" BOOLEAN NOT NULL,
-    "LAST_SONARLINT_CONNECTION" BIGINT
-);
-ALTER TABLE "USERS" ADD CONSTRAINT "PK_USERS" PRIMARY KEY("UUID");
-CREATE UNIQUE INDEX "USERS_LOGIN" ON "USERS"("LOGIN" NULLS FIRST);
-CREATE INDEX "USERS_UPDATED_AT" ON "USERS"("UPDATED_AT" NULLS FIRST);
-CREATE UNIQUE INDEX "UNIQ_EXTERNAL_ID" ON "USERS"("EXTERNAL_IDENTITY_PROVIDER" NULLS FIRST, "EXTERNAL_ID" NULLS FIRST);
-CREATE UNIQUE INDEX "UNIQ_EXTERNAL_LOGIN" ON "USERS"("EXTERNAL_IDENTITY_PROVIDER" NULLS FIRST, "EXTERNAL_LOGIN" NULLS FIRST);
-
-CREATE TABLE "SCM_ACCOUNTS"(
-    "USER_UUID" CHARACTER VARYING(255) NOT NULL,
-    "SCM_ACCOUNT" CHARACTER VARYING(255) NOT NULL
-);
-ALTER TABLE "SCM_ACCOUNTS" ADD CONSTRAINT "PK_SCM_ACCOUNTS" PRIMARY KEY("USER_UUID", "SCM_ACCOUNT");
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v110/CreateIndexForEmailOnUsersTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v110/CreateIndexForEmailOnUsersTableTest/schema.sql
new file mode 100644 (file)
index 0000000..f2af204
--- /dev/null
@@ -0,0 +1,27 @@
+CREATE TABLE "USERS"(
+    "UUID" CHARACTER VARYING(255) NOT NULL,
+    "LOGIN" CHARACTER VARYING(255) NOT NULL,
+    "NAME" CHARACTER VARYING(200),
+    "EMAIL" CHARACTER VARYING(100),
+    "CRYPTED_PASSWORD" CHARACTER VARYING(100),
+    "SALT" CHARACTER VARYING(40),
+    "HASH_METHOD" CHARACTER VARYING(10),
+    "ACTIVE" BOOLEAN DEFAULT TRUE,
+    "SCM_ACCOUNTS" CHARACTER VARYING(4000),
+    "EXTERNAL_LOGIN" CHARACTER VARYING(255) NOT NULL,
+    "EXTERNAL_IDENTITY_PROVIDER" CHARACTER VARYING(100) NOT NULL,
+    "EXTERNAL_ID" CHARACTER VARYING(255) NOT NULL,
+    "USER_LOCAL" BOOLEAN NOT NULL,
+    "HOMEPAGE_TYPE" CHARACTER VARYING(40),
+    "HOMEPAGE_PARAMETER" CHARACTER VARYING(40),
+    "LAST_CONNECTION_DATE" BIGINT,
+    "CREATED_AT" BIGINT,
+    "UPDATED_AT" BIGINT,
+    "RESET_PASSWORD" BOOLEAN NOT NULL,
+    "LAST_SONARLINT_CONNECTION" BIGINT
+);
+ALTER TABLE "USERS" ADD CONSTRAINT "PK_USERS" PRIMARY KEY("UUID");
+CREATE UNIQUE INDEX "USERS_LOGIN" ON "USERS"("LOGIN" NULLS FIRST);
+CREATE INDEX "USERS_UPDATED_AT" ON "USERS"("UPDATED_AT" NULLS FIRST);
+CREATE UNIQUE INDEX "UNIQ_EXTERNAL_ID" ON "USERS"("EXTERNAL_IDENTITY_PROVIDER" NULLS FIRST, "EXTERNAL_ID" NULLS FIRST);
+CREATE UNIQUE INDEX "UNIQ_EXTERNAL_LOGIN" ON "USERS"("EXTERNAL_IDENTITY_PROVIDER" NULLS FIRST, "EXTERNAL_LOGIN" NULLS FIRST);
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v110/CreateIndexForScmAccountOnScmAccountsTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v110/CreateIndexForScmAccountOnScmAccountsTableTest/schema.sql
new file mode 100644 (file)
index 0000000..7a113bb
--- /dev/null
@@ -0,0 +1,5 @@
+CREATE TABLE "SCM_ACCOUNTS"(
+    "USER_UUID" CHARACTER VARYING(255) NOT NULL,
+    "SCM_ACCOUNT" CHARACTER VARYING(255) NOT NULL
+);
+ALTER TABLE "SCM_ACCOUNTS" ADD CONSTRAINT "PK_SCM_ACCOUNTS" PRIMARY KEY("USER_UUID", "SCM_ACCOUNT");
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v110/CreateIndexesForScmAccountsTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v110/CreateIndexesForScmAccountsTest/schema.sql
new file mode 100644 (file)
index 0000000..7a113bb
--- /dev/null
@@ -0,0 +1,5 @@
+CREATE TABLE "SCM_ACCOUNTS"(
+    "USER_UUID" CHARACTER VARYING(255) NOT NULL,
+    "SCM_ACCOUNT" CHARACTER VARYING(255) NOT NULL
+);
+ALTER TABLE "SCM_ACCOUNTS" ADD CONSTRAINT "PK_SCM_ACCOUNTS" PRIMARY KEY("USER_UUID", "SCM_ACCOUNT");
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v110/DropScmAccountsInUsersTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v110/DropScmAccountsInUsersTest/schema.sql
new file mode 100644 (file)
index 0000000..f2af204
--- /dev/null
@@ -0,0 +1,27 @@
+CREATE TABLE "USERS"(
+    "UUID" CHARACTER VARYING(255) NOT NULL,
+    "LOGIN" CHARACTER VARYING(255) NOT NULL,
+    "NAME" CHARACTER VARYING(200),
+    "EMAIL" CHARACTER VARYING(100),
+    "CRYPTED_PASSWORD" CHARACTER VARYING(100),
+    "SALT" CHARACTER VARYING(40),
+    "HASH_METHOD" CHARACTER VARYING(10),
+    "ACTIVE" BOOLEAN DEFAULT TRUE,
+    "SCM_ACCOUNTS" CHARACTER VARYING(4000),
+    "EXTERNAL_LOGIN" CHARACTER VARYING(255) NOT NULL,
+    "EXTERNAL_IDENTITY_PROVIDER" CHARACTER VARYING(100) NOT NULL,
+    "EXTERNAL_ID" CHARACTER VARYING(255) NOT NULL,
+    "USER_LOCAL" BOOLEAN NOT NULL,
+    "HOMEPAGE_TYPE" CHARACTER VARYING(40),
+    "HOMEPAGE_PARAMETER" CHARACTER VARYING(40),
+    "LAST_CONNECTION_DATE" BIGINT,
+    "CREATED_AT" BIGINT,
+    "UPDATED_AT" BIGINT,
+    "RESET_PASSWORD" BOOLEAN NOT NULL,
+    "LAST_SONARLINT_CONNECTION" BIGINT
+);
+ALTER TABLE "USERS" ADD CONSTRAINT "PK_USERS" PRIMARY KEY("UUID");
+CREATE UNIQUE INDEX "USERS_LOGIN" ON "USERS"("LOGIN" NULLS FIRST);
+CREATE INDEX "USERS_UPDATED_AT" ON "USERS"("UPDATED_AT" NULLS FIRST);
+CREATE UNIQUE INDEX "UNIQ_EXTERNAL_ID" ON "USERS"("EXTERNAL_IDENTITY_PROVIDER" NULLS FIRST, "EXTERNAL_ID" NULLS FIRST);
+CREATE UNIQUE INDEX "UNIQ_EXTERNAL_LOGIN" ON "USERS"("EXTERNAL_IDENTITY_PROVIDER" NULLS FIRST, "EXTERNAL_LOGIN" NULLS FIRST);
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v110/MigrateScmAccountsFromUsersToScmAccountsTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v110/MigrateScmAccountsFromUsersToScmAccountsTest/schema.sql
new file mode 100644 (file)
index 0000000..2f34178
--- /dev/null
@@ -0,0 +1,33 @@
+CREATE TABLE "USERS"(
+    "UUID" CHARACTER VARYING(255) NOT NULL,
+    "LOGIN" CHARACTER VARYING(255) NOT NULL,
+    "NAME" CHARACTER VARYING(200),
+    "EMAIL" CHARACTER VARYING(100),
+    "CRYPTED_PASSWORD" CHARACTER VARYING(100),
+    "SALT" CHARACTER VARYING(40),
+    "HASH_METHOD" CHARACTER VARYING(10),
+    "ACTIVE" BOOLEAN DEFAULT TRUE,
+    "SCM_ACCOUNTS" CHARACTER VARYING(4000),
+    "EXTERNAL_LOGIN" CHARACTER VARYING(255) NOT NULL,
+    "EXTERNAL_IDENTITY_PROVIDER" CHARACTER VARYING(100) NOT NULL,
+    "EXTERNAL_ID" CHARACTER VARYING(255) NOT NULL,
+    "USER_LOCAL" BOOLEAN NOT NULL,
+    "HOMEPAGE_TYPE" CHARACTER VARYING(40),
+    "HOMEPAGE_PARAMETER" CHARACTER VARYING(40),
+    "LAST_CONNECTION_DATE" BIGINT,
+    "CREATED_AT" BIGINT,
+    "UPDATED_AT" BIGINT,
+    "RESET_PASSWORD" BOOLEAN NOT NULL,
+    "LAST_SONARLINT_CONNECTION" BIGINT
+);
+ALTER TABLE "USERS" ADD CONSTRAINT "PK_USERS" PRIMARY KEY("UUID");
+CREATE UNIQUE INDEX "USERS_LOGIN" ON "USERS"("LOGIN" NULLS FIRST);
+CREATE INDEX "USERS_UPDATED_AT" ON "USERS"("UPDATED_AT" NULLS FIRST);
+CREATE UNIQUE INDEX "UNIQ_EXTERNAL_ID" ON "USERS"("EXTERNAL_IDENTITY_PROVIDER" NULLS FIRST, "EXTERNAL_ID" NULLS FIRST);
+CREATE UNIQUE INDEX "UNIQ_EXTERNAL_LOGIN" ON "USERS"("EXTERNAL_IDENTITY_PROVIDER" NULLS FIRST, "EXTERNAL_LOGIN" NULLS FIRST);
+
+CREATE TABLE "SCM_ACCOUNTS"(
+    "USER_UUID" CHARACTER VARYING(255) NOT NULL,
+    "SCM_ACCOUNT" CHARACTER VARYING(255) NOT NULL
+);
+ALTER TABLE "SCM_ACCOUNTS" ADD CONSTRAINT "PK_SCM_ACCOUNTS" PRIMARY KEY("USER_UUID", "SCM_ACCOUNT");