]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-13221 Set primary key of 'EVENTS' table to column 'UUID' and drop 'ID'
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 27 Mar 2020 09:51:09 +0000 (10:51 +0100)
committersonartech <sonartech@sonarsource.com>
Mon, 25 May 2020 20:05:18 +0000 (20:05 +0000)
24 files changed:
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistEventsStep.java
server/sonar-db-core/src/testFixtures/java/org/sonar/db/AbstractDbTester.java
server/sonar-db-dao/src/main/java/org/sonar/db/event/EventDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/event/EventDto.java
server/sonar-db-dao/src/main/java/org/sonar/db/event/EventMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/event/EventMapper.xml
server/sonar-db-dao/src/schema/schema-sq.ddl
server/sonar-db-dao/src/test/java/org/sonar/db/event/EventDaoTest.java
server/sonar-db-dao/src/test/java/org/sonar/db/event/EventDtoTest.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v00/CreateInitialSchema.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/AddPrimaryKeyOnUuidColumnOfEventsTable.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/DbVersion83.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/DropIdColumnOfEventsTable.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/DropPrimaryKeyOnIdColumnOfEventsTable.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/util/AddPrimaryKeyBuilder.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/util/DropPrimaryKeySqlGenerator.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/AddPrimaryKeyOnUuidColumnOfEventsTableTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/DbVersion83Test.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/DropIdColumnOfEventsTableTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/DropPrimaryKeyOnIdColumnOfEventsTableTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/util/AddPrimaryKeyBuilderTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/AddPrimaryKeyOnUuidColumnOfEventsTableTest/schema.sql [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/DropIdColumnOfEventsTableTest/schema.sql [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/DropPrimaryKeyOnIdColumnOfEventsTableTest/schema.sql [new file with mode: 0644]

index 4c838922469ffe8aab4a049bd81a7e2d2bc22231..fbfcbd5fbc39950d0684c26f1a77e5b358ba374b 100644 (file)
@@ -111,7 +111,7 @@ public class PersistEventsStep implements ComputationStep {
     private void deletePreviousEventsHavingSameVersion(DbSession session, String version, Component component) {
       for (EventDto dto : dbClient.eventDao().selectByComponentUuid(session, component.getUuid())) {
         if (Objects.equals(dto.getCategory(), EventDto.CATEGORY_VERSION) && Objects.equals(dto.getName(), version)) {
-          dbClient.eventDao().delete(session, dto.getId());
+          dbClient.eventDao().delete(session, dto.getUuid());
         }
       }
     }
index 4daa76127146d972b7502c1af294979f24be3152..2595ae70ae3018a56132db7e690d2cc2e0e780a8 100644 (file)
@@ -357,6 +357,18 @@ public class AbstractDbTester<T extends TestDb> extends ExternalResource {
     }
   }
 
+  public void assertNoPrimaryKey(String tableName) {
+    try (Connection connection = getConnection()) {
+      PK pk = pkOf(connection, tableName.toUpperCase(Locale.ENGLISH));
+      if (pk == null) {
+        pkOf(connection, tableName.toLowerCase(Locale.ENGLISH));
+      }
+      assertThat(pk).as("Primary key is still defined on table %s", tableName).isNull();
+    } catch (SQLException e) {
+      throw new IllegalStateException("Fail to check primary key", e);
+    }
+  }
+
   @CheckForNull
   private PK pkOf(Connection connection, String tableName) throws SQLException {
     try (ResultSet resultSet = connection.getMetaData().getPrimaryKeys(null, null, tableName)) {
index 291acbdae22bf968f94b4d5f23e1b16bc6402ac0..12f49fdb399977868b5ba4f8487fe6965c97f87f 100644 (file)
@@ -59,10 +59,6 @@ public class EventDao implements Dao {
     mapper(dbSession).update(uuid, name, description);
   }
 
-  public void delete(DbSession session, Long id) {
-    mapper(session).deleteById(id);
-  }
-
   public void delete(DbSession session, String uuid) {
     mapper(session).deleteByUuid(uuid);
   }
index 448b0b8430fe939a6973562fd8311ed74fe27c90..bcf5962da10796d5cdeda262b7493df14752e228 100644 (file)
@@ -33,7 +33,6 @@ public class EventDto {
   public static final String CATEGORY_PROFILE = "Profile";
   public static final String CATEGORY_DEFINITION_CHANGE = "Definition change";
 
-  private Long id;
   private String uuid;
   private String analysisUuid;
   private String componentUuid;
@@ -44,15 +43,6 @@ public class EventDto {
   private Long createdAt;
   private String data;
 
-  public Long getId() {
-    return id;
-  }
-
-  public EventDto setId(Long id) {
-    this.id = id;
-    return this;
-  }
-
   public String getUuid() {
     return uuid;
   }
index c492f5a82d9827c7f2c6839c55bf2886d0628d6d..290b4ded99406dc9b99c211b3b8112b9f396a900 100644 (file)
@@ -39,7 +39,5 @@ public interface EventMapper {
 
   void update(@Param("uuid") String uuid, @Param("name") @Nullable String name, @Param("description") @Nullable String description);
 
-  void deleteById(long id);
-
   void deleteByUuid(String uuid);
 }
index 4bab83a1b92c092a97dc0d8e634dfd934735399e..9463f0c9e2c890c1db2fe22daffa110633bd837d 100644 (file)
@@ -3,7 +3,6 @@
 <mapper namespace="org.sonar.db.event.EventMapper">
 
   <sql id="eventColumns">
-    e.id,
     e.uuid,
     e.analysis_uuid as "analysisUuid",
     e.component_uuid as "componentUuid",
@@ -65,7 +64,7 @@
       e.event_date desc
   </select>
 
-  <insert id="insert" parameterType="Event" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
+  <insert id="insert" parameterType="Event">
     INSERT INTO events (uuid, analysis_uuid, component_uuid, name, category, description, event_data, event_date, created_at)
     VALUES (
       #{uuid, jdbcType=VARCHAR},
     where uuid = #{uuid}
   </update>
 
-  <delete id="deleteById" parameterType="Long">
-    DELETE FROM events WHERE id=#{id}
-  </delete>
-
   <delete id="deleteByUuid" parameterType="String">
     DELETE FROM events WHERE uuid=#{uuid}
   </delete>
index 986e1367bdd29af0080a70dddc5bb2b9af1be58c..5ef948d76ba9db47ea8d628add68c5c6b8ac2a22 100644 (file)
@@ -280,7 +280,6 @@ CREATE INDEX "EVENT_CPNT_CHANGES_CPNT" ON "EVENT_COMPONENT_CHANGES"("EVENT_COMPO
 CREATE INDEX "EVENT_CPNT_CHANGES_ANALYSIS" ON "EVENT_COMPONENT_CHANGES"("EVENT_ANALYSIS_UUID");
 
 CREATE TABLE "EVENTS"(
-    "ID" INTEGER NOT NULL AUTO_INCREMENT (1,1),
     "UUID" VARCHAR(40) NOT NULL,
     "ANALYSIS_UUID" VARCHAR(50) NOT NULL,
     "NAME" VARCHAR(400),
@@ -291,7 +290,7 @@ CREATE TABLE "EVENTS"(
     "CREATED_AT" BIGINT NOT NULL,
     "COMPONENT_UUID" VARCHAR(50) NOT NULL
 );
-ALTER TABLE "EVENTS" ADD CONSTRAINT "PK_EVENTS" PRIMARY KEY("ID");
+ALTER TABLE "EVENTS" ADD CONSTRAINT "PK_EVENTS" PRIMARY KEY("UUID");
 CREATE UNIQUE INDEX "EVENTS_UUID" ON "EVENTS"("UUID");
 CREATE INDEX "EVENTS_ANALYSIS" ON "EVENTS"("ANALYSIS_UUID");
 CREATE INDEX "EVENTS_COMPONENT_UUID" ON "EVENTS"("COMPONENT_UUID");
index bac7617e6b9bc87f0d81c184e1d507e451833de1..e536b4b21287c696401717ff01016cb0457fb711 100644 (file)
@@ -207,17 +207,6 @@ public class EventDaoTest {
     assertThat(result.getDescription()).isEqualTo("New Description");
   }
 
-  @Test
-  public void delete_by_id() {
-    SnapshotDto analysis = dbTester.components().insertProjectAndSnapshot(ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert()));
-    EventDto eventDto = dbTester.events().insertEvent(newEvent(analysis).setUuid("A1"));
-
-    underTest.delete(dbTester.getSession(), eventDto.getId());
-    dbTester.getSession().commit();
-
-    assertThat(dbTester.countRowsOfTable("events")).isEqualTo(0);
-  }
-
   @Test
   public void delete_by_uuid() {
     dbTester.events().insertEvent(newEvent(newAnalysis(ComponentTesting.newPrivateProjectDto(dbTester.getDefaultOrganization()))).setUuid("E1"));
index 31f481a6bbab8aabe9444def57f279eb80560341..8b299d338538dc446f20557c79385bed6f841b2c 100644 (file)
@@ -28,7 +28,6 @@ public class EventDtoTest {
   @Test
   public void test_getters_and_setters() {
     EventDto dto = new EventDto()
-      .setId(1L)
       .setAnalysisUuid("uuid_1")
       .setComponentUuid("ABCD")
       .setName("1.0")
@@ -38,7 +37,6 @@ public class EventDtoTest {
       .setDate(1413407091086L)
       .setCreatedAt(1225630680000L);
 
-    assertThat(dto.getId()).isEqualTo(1L);
     assertThat(dto.getAnalysisUuid()).isEqualTo("uuid_1");
     assertThat(dto.getComponentUuid()).isEqualTo("ABCD");
     assertThat(dto.getName()).isEqualTo("1.0");
index ac409f4e2d278e0b499c459f82401c65f56a912a..c22374418c137e0b7c4ec2a723ad9ef252719336 100644 (file)
@@ -46,6 +46,7 @@ import static org.sonar.server.platform.db.migration.def.TinyIntColumnDef.newTin
 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.MAX_SIZE;
 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE;
 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;
+import static org.sonar.server.platform.db.migration.sql.CreateTableBuilder.PRIMARY_KEY_PREFIX;
 import static org.sonar.server.platform.db.migration.sql.CreateTableBuilder.ColumnFlag.AUTO_INCREMENT;
 
 public class CreateInitialSchema extends DdlChange {
@@ -1176,7 +1177,7 @@ public class CreateInitialSchema extends DdlChange {
       .addColumn(newTinyIntColumnDefBuilder().setColumnName("ad_hoc_type").setIsNullable(true).build())
       .addColumn(TECHNICAL_CREATED_AT_COL)
       .addColumn(TECHNICAL_UPDATED_AT_COL)
-      .withPkConstraintName("pk_" + tableName)
+      .withPkConstraintName(PRIMARY_KEY_PREFIX + tableName)
       .build());
   }
 
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/AddPrimaryKeyOnUuidColumnOfEventsTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/AddPrimaryKeyOnUuidColumnOfEventsTable.java
new file mode 100644 (file)
index 0000000..a615d0d
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2020 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.v83;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+import org.sonar.server.platform.db.migration.version.v83.util.AddPrimaryKeyBuilder;
+
+public class AddPrimaryKeyOnUuidColumnOfEventsTable extends DdlChange {
+
+  public AddPrimaryKeyOnUuidColumnOfEventsTable(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    context.execute(new AddPrimaryKeyBuilder("events", "uuid").build());
+  }
+
+}
index 4f105cf4c8f48ff2af72f674c093852b6c5073c4..258e92cfae82b75d321a1a3f17d4b1688f4522dd 100644 (file)
@@ -39,6 +39,11 @@ public class DbVersion83 implements DbVersion {
       .add(3310, "Remove column 'resource_id' in 'user_roles'", DropResourceIdFromUserRolesTable.class)
       .add(3311, "Remove column 'id' in 'components'", DropIdFromComponentsTable.class)
 
+      // Migration on EVENTS table
+      .add(3400, "Drop primary key on 'ID' column of 'EVENTS' table", DropPrimaryKeyOnIdColumnOfEventsTable.class)
+      .add(3401, "Add primary key on 'UUID' column of 'EVENTS' table", AddPrimaryKeyOnUuidColumnOfEventsTable.class)
+      .add(3402, "Drop column 'ID' of 'EVENTS' table", DropIdColumnOfEventsTable.class);
+
     ;
   }
 }
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/DropIdColumnOfEventsTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/DropIdColumnOfEventsTable.java
new file mode 100644 (file)
index 0000000..0755264
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2020 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.v83;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.sql.DropColumnsBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+public class DropIdColumnOfEventsTable extends DdlChange {
+
+  private Database db;
+
+  public DropIdColumnOfEventsTable(Database db) {
+    super(db);
+    this.db = db;
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    context.execute(new DropColumnsBuilder(db.getDialect(), "events", "id").build());
+  }
+
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/DropPrimaryKeyOnIdColumnOfEventsTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/DropPrimaryKeyOnIdColumnOfEventsTable.java
new file mode 100644 (file)
index 0000000..a5ca4b0
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2020 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.v83;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+import org.sonar.server.platform.db.migration.version.v83.util.DropPrimaryKeySqlGenerator;
+
+public class DropPrimaryKeyOnIdColumnOfEventsTable extends DdlChange {
+
+  private final DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator;
+
+  public DropPrimaryKeyOnIdColumnOfEventsTable(Database db, DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator) {
+    super(db);
+    this.dropPrimaryKeySqlGenerator = dropPrimaryKeySqlGenerator;
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    context.execute(dropPrimaryKeySqlGenerator.generate("events", "events", "id"));
+  }
+
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/util/AddPrimaryKeyBuilder.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/util/AddPrimaryKeyBuilder.java
new file mode 100644 (file)
index 0000000..fad7651
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2020 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.v83.util;
+
+import static com.google.common.base.Preconditions.checkState;
+import static java.lang.String.format;
+import static org.sonar.server.platform.db.migration.def.Validations.validateTableName;
+import static org.sonar.server.platform.db.migration.sql.CreateTableBuilder.PRIMARY_KEY_PREFIX;
+
+public class AddPrimaryKeyBuilder {
+
+  private final String tableName;
+  private final String primaryKey;
+
+  public AddPrimaryKeyBuilder(String tableName, String column) {
+    this.tableName = validateTableName(tableName);
+    this.primaryKey = column;
+  }
+
+  public String build() {
+    checkState(primaryKey != null, "Primary key is missing");
+    return format("ALTER TABLE %s ADD CONSTRAINT %s%s PRIMARY KEY (%s)", tableName, PRIMARY_KEY_PREFIX, tableName, primaryKey);
+  }
+
+}
index ea4112520f28b8a0a8a4ec51c38501f53ec6e469..35c2f51d2e5f698d0891b60d3e270648c4ee6bfd 100644 (file)
@@ -36,8 +36,9 @@ import static java.util.Locale.ENGLISH;
 import static org.sonar.server.platform.db.migration.sql.CreateTableBuilder.PRIMARY_KEY_PREFIX;
 
 public class DropPrimaryKeySqlGenerator {
+
   private final Database db;
-  private final GetConstraintHelper getConstraintHelper;
+  private GetConstraintHelper getConstraintHelper;
 
   public DropPrimaryKeySqlGenerator(Database db, GetConstraintHelper getConstraintHelper) {
     this.db = db;
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/AddPrimaryKeyOnUuidColumnOfEventsTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/AddPrimaryKeyOnUuidColumnOfEventsTableTest.java
new file mode 100644 (file)
index 0000000..d8eb67b
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2020 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.v83;
+
+import java.sql.SQLException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+public class AddPrimaryKeyOnUuidColumnOfEventsTableTest {
+
+  @Rule
+  public CoreDbTester db = CoreDbTester.createForSchema(AddPrimaryKeyOnUuidColumnOfEventsTableTest.class, "schema.sql");
+
+  private AddPrimaryKeyOnUuidColumnOfEventsTable underTest = new AddPrimaryKeyOnUuidColumnOfEventsTable(db.database());
+
+  @Test
+  public void execute() throws SQLException {
+    underTest.execute();
+
+    db.assertPrimaryKey("events", "pk_events", "uuid");
+  }
+
+  @Test
+  public void migration_is_not_re_entrant() throws SQLException {
+    underTest.execute();
+
+    assertThatThrownBy(() -> underTest.execute()).isInstanceOf(IllegalStateException.class);
+  }
+}
index ad946da34cceb817add17daa3902859388501699..6fd177bf8e9bd26b8a573f6255f84cf7f2ae313c 100644 (file)
@@ -36,7 +36,7 @@ public class DbVersion83Test {
 
   @Test
   public void verify_migration_count() {
-    verifyMigrationCount(underTest, 12);
+    verifyMigrationCount(underTest, 15);
   }
 
 }
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/DropIdColumnOfEventsTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/DropIdColumnOfEventsTableTest.java
new file mode 100644 (file)
index 0000000..e82c457
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2020 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.v83;
+
+import java.sql.SQLException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+public class DropIdColumnOfEventsTableTest {
+
+  @Rule
+  public CoreDbTester db = CoreDbTester.createForSchema(DropIdColumnOfEventsTableTest.class, "schema.sql");
+
+  private DropIdColumnOfEventsTable underTest = new DropIdColumnOfEventsTable(db.database());
+
+  @Test
+  public void execute() throws SQLException {
+    underTest.execute();
+
+    db.assertColumnDoesNotExist("events", "id");
+  }
+
+  @Test
+  public void migration_is_not_re_entrant() throws SQLException {
+    underTest.execute();
+
+    assertThatThrownBy(() -> underTest.execute()).isInstanceOf(IllegalStateException.class);
+  }
+
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/DropPrimaryKeyOnIdColumnOfEventsTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/DropPrimaryKeyOnIdColumnOfEventsTableTest.java
new file mode 100644 (file)
index 0000000..17e262b
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2020 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.v83;
+
+import java.sql.SQLException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+import org.sonar.server.platform.db.migration.version.v83.util.DropPrimaryKeySqlGenerator;
+import org.sonar.server.platform.db.migration.version.v83.util.GetConstraintHelper;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+public class DropPrimaryKeyOnIdColumnOfEventsTableTest {
+
+  private static final String TABLE_NAME = "events";
+  @Rule
+  public CoreDbTester db = CoreDbTester.createForSchema(DropPrimaryKeyOnIdColumnOfEventsTableTest.class, "schema.sql");
+
+  private DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator = new DropPrimaryKeySqlGenerator(db.database(), new GetConstraintHelper(db.database()));
+
+  private DropPrimaryKeyOnIdColumnOfEventsTable underTest = new DropPrimaryKeyOnIdColumnOfEventsTable(db.database(), dropPrimaryKeySqlGenerator);
+
+  @Test
+  public void execute() throws SQLException {
+    underTest.execute();
+
+    db.assertNoPrimaryKey(TABLE_NAME);
+  }
+
+  @Test
+  public void migration_is_not_re_entrant() throws SQLException {
+    underTest.execute();
+
+    assertThatThrownBy(() -> underTest.execute()).isInstanceOf(IllegalStateException.class);
+  }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/util/AddPrimaryKeyBuilderTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/util/AddPrimaryKeyBuilderTest.java
new file mode 100644 (file)
index 0000000..3e99cda
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2020 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.v83.util;
+
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+public class AddPrimaryKeyBuilderTest {
+
+  private static final String TABLE_NAME = "issues";
+
+  @Test
+  public void generate() {
+    String sql = new AddPrimaryKeyBuilder(TABLE_NAME, "id").build();
+
+    assertThat(sql).isEqualTo("ALTER TABLE issues ADD CONSTRAINT pk_issues PRIMARY KEY (id)");
+  }
+
+  @Test
+  public void fail_when_table_name_is_invalid() {
+    assertThatThrownBy(() -> new AddPrimaryKeyBuilder("abcdefghijklmnopqrstuvwxyz", "id").build())
+      .isInstanceOf(IllegalArgumentException.class);
+  }
+}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/AddPrimaryKeyOnUuidColumnOfEventsTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/AddPrimaryKeyOnUuidColumnOfEventsTableTest/schema.sql
new file mode 100644 (file)
index 0000000..bd6b897
--- /dev/null
@@ -0,0 +1,15 @@
+CREATE TABLE "EVENTS"(
+    "ID" INTEGER NOT NULL,
+    "UUID" VARCHAR(40) NOT NULL,
+    "ANALYSIS_UUID" VARCHAR(50) NOT NULL,
+    "NAME" VARCHAR(400),
+    "CATEGORY" VARCHAR(50),
+    "DESCRIPTION" VARCHAR(4000),
+    "EVENT_DATA" VARCHAR(4000),
+    "EVENT_DATE" BIGINT NOT NULL,
+    "CREATED_AT" BIGINT NOT NULL,
+    "COMPONENT_UUID" VARCHAR(50) NOT NULL
+);
+CREATE UNIQUE INDEX "EVENTS_UUID" ON "EVENTS"("UUID");
+CREATE INDEX "EVENTS_ANALYSIS" ON "EVENTS"("ANALYSIS_UUID");
+CREATE INDEX "EVENTS_COMPONENT_UUID" ON "EVENTS"("COMPONENT_UUID");
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/DropIdColumnOfEventsTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/DropIdColumnOfEventsTableTest/schema.sql
new file mode 100644 (file)
index 0000000..ac79cfa
--- /dev/null
@@ -0,0 +1,16 @@
+CREATE TABLE "EVENTS"(
+    "ID" INTEGER NOT NULL,
+    "UUID" VARCHAR(40) NOT NULL,
+    "ANALYSIS_UUID" VARCHAR(50) NOT NULL,
+    "NAME" VARCHAR(400),
+    "CATEGORY" VARCHAR(50),
+    "DESCRIPTION" VARCHAR(4000),
+    "EVENT_DATA" VARCHAR(4000),
+    "EVENT_DATE" BIGINT NOT NULL,
+    "CREATED_AT" BIGINT NOT NULL,
+    "COMPONENT_UUID" VARCHAR(50) NOT NULL
+);
+ALTER TABLE "EVENTS" ADD CONSTRAINT "PK_EVENTS" PRIMARY KEY("UUID");
+CREATE UNIQUE INDEX "EVENTS_UUID" ON "EVENTS"("UUID");
+CREATE INDEX "EVENTS_ANALYSIS" ON "EVENTS"("ANALYSIS_UUID");
+CREATE INDEX "EVENTS_COMPONENT_UUID" ON "EVENTS"("COMPONENT_UUID");
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/DropPrimaryKeyOnIdColumnOfEventsTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/DropPrimaryKeyOnIdColumnOfEventsTableTest/schema.sql
new file mode 100644 (file)
index 0000000..2f03d9d
--- /dev/null
@@ -0,0 +1,16 @@
+CREATE TABLE "EVENTS"(
+    "ID" INTEGER NOT NULL AUTO_INCREMENT (1,1),
+    "UUID" VARCHAR(40) NOT NULL,
+    "ANALYSIS_UUID" VARCHAR(50) NOT NULL,
+    "NAME" VARCHAR(400),
+    "CATEGORY" VARCHAR(50),
+    "DESCRIPTION" VARCHAR(4000),
+    "EVENT_DATA" VARCHAR(4000),
+    "EVENT_DATE" BIGINT NOT NULL,
+    "CREATED_AT" BIGINT NOT NULL,
+    "COMPONENT_UUID" VARCHAR(50) NOT NULL
+);
+ALTER TABLE "EVENTS" ADD CONSTRAINT "PK_EVENTS" PRIMARY KEY("ID");
+CREATE UNIQUE INDEX "EVENTS_UUID" ON "EVENTS"("UUID");
+CREATE INDEX "EVENTS_ANALYSIS" ON "EVENTS"("ANALYSIS_UUID");
+CREATE INDEX "EVENTS_COMPONENT_UUID" ON "EVENTS"("COMPONENT_UUID");