]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15407 Re-creating of PK on Issues.Kee will only happen on Oracle DB
authorLukasz Jarocki <lukasz.jarocki@sonarsource.com>
Wed, 8 Jun 2022 13:52:12 +0000 (15:52 +0200)
committersonartech <sonartech@sonarsource.com>
Mon, 13 Jun 2022 20:03:03 +0000 (20:03 +0000)
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v90/AddPrimaryKeyOnKeeColumnOfIssuesTable.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v90/DropPrimaryKeyOnKeeColumnOfIssuesTable.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v90/AddPrimaryKeyOnKeeColumnOfIssuesTableTest.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v90/DropPrimaryKeyOnKeeColumnOfIssuesTableTest.java
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v90/AddPrimaryKeyOnKeeColumnOfIssuesTableTest/schema.sql

index 9e4e799c6a27995a28ef6c42b549122a0a814d68..6e1c4b00eca4b92e8bcffefd5a96dec1b5b9d81b 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.server.platform.db.migration.version.v90;
 
 import java.sql.SQLException;
 import org.sonar.db.Database;
+import org.sonar.db.dialect.Oracle;
 import org.sonar.server.platform.db.migration.sql.AddPrimaryKeyBuilder;
 import org.sonar.server.platform.db.migration.step.DdlChange;
 
@@ -33,7 +34,9 @@ public class AddPrimaryKeyOnKeeColumnOfIssuesTable extends DdlChange {
 
   @Override
   public void execute(Context context) throws SQLException {
-    context.execute(new AddPrimaryKeyBuilder(TABLE_NAME, "kee").build());
+    if (Oracle.ID.equals(getDialect().getId())) {
+      context.execute(new AddPrimaryKeyBuilder(TABLE_NAME, "kee").build());
+    }
   }
 
 }
index d49b2ced3a5b0884e13b1047ae98de224f096f17..d7a23f30cf93f7d20b99cbebeea02a3bc6e3265b 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.server.platform.db.migration.version.v90;
 
 import java.sql.SQLException;
 import org.sonar.db.Database;
+import org.sonar.db.dialect.Oracle;
 import org.sonar.server.platform.db.migration.sql.DropPrimaryKeySqlGenerator;
 import org.sonar.server.platform.db.migration.step.DdlChange;
 
@@ -35,6 +36,8 @@ public class DropPrimaryKeyOnKeeColumnOfIssuesTable extends DdlChange {
 
   @Override
   public void execute(Context context) throws SQLException {
-    context.execute(dropPrimaryKeySqlGenerator.generate(TABLE_NAME, "kee", false));
+    if (Oracle.ID.equals(getDialect().getId())) {
+      context.execute(dropPrimaryKeySqlGenerator.generate(TABLE_NAME, "kee", false));
+    }
   }
 }
index 841fa4fe4d1d52f93e2e7e589cce7de0dbddaf56..861d23f2874d373748819443e9e10aadc8915851 100644 (file)
@@ -23,6 +23,16 @@ import java.sql.SQLException;
 import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.db.CoreDbTester;
+import org.sonar.db.Database;
+import org.sonar.db.dialect.Dialect;
+import org.sonar.db.dialect.Oracle;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.only;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 public class AddPrimaryKeyOnKeeColumnOfIssuesTableTest {
   @Rule
@@ -31,9 +41,36 @@ public class AddPrimaryKeyOnKeeColumnOfIssuesTableTest {
   private final AddPrimaryKeyOnKeeColumnOfIssuesTable underTest = new AddPrimaryKeyOnKeeColumnOfIssuesTable(db.database());
 
   @Test
-  public void migration_should_drop_PK_on_ce_activity() throws SQLException {
-    db.assertNoPrimaryKey("issues");
+  public void migration_should_not_create_another_PK_on_issues_when_using_H2_database() throws SQLException {
+    db.assertPrimaryKey("issues", "pk_issues", "kee");
+
     underTest.execute();
+
+    db.assertPrimaryKey("issues", "pk_issues", "kee");
+  }
+
+  @Test
+  public void migration_should_have_no_effect_when_using_h2_database_and_running_it_twice() throws SQLException {
     db.assertPrimaryKey("issues", "pk_issues", "kee");
+
+    underTest.execute();
+    underTest.execute();
+
+    db.assertPrimaryKey("issues", "pk_issues", "kee");
+  }
+
+  @Test
+  public void migration_should_execute_when_using_oracle_database() throws SQLException {
+    Database mockedDb = mock(Database.class);
+    AddPrimaryKeyOnKeeColumnOfIssuesTable underTest = new AddPrimaryKeyOnKeeColumnOfIssuesTable(mockedDb);
+
+    DdlChange.Context context = mock(DdlChange.Context.class);
+    Dialect dialect = mock(Dialect.class);
+    when(dialect.getId()).thenReturn(Oracle.ID);
+    when(mockedDb.getDialect()).thenReturn(dialect);
+
+    underTest.execute(context);
+
+    verify(context, only()).execute("ALTER TABLE issues ADD CONSTRAINT pk_issues PRIMARY KEY (kee)");
   }
 }
index 67a683b61f5bce4cbe4f92c90e1f88519850b1e6..faca1e711aeabc83fc5c74f6932a4dc7e045bd78 100644 (file)
@@ -23,8 +23,17 @@ import java.sql.SQLException;
 import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.db.CoreDbTester;
+import org.sonar.db.Database;
+import org.sonar.db.dialect.Dialect;
+import org.sonar.db.dialect.Oracle;
 import org.sonar.server.platform.db.migration.sql.DbPrimaryKeyConstraintFinder;
 import org.sonar.server.platform.db.migration.sql.DropPrimaryKeySqlGenerator;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.only;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 public class DropPrimaryKeyOnKeeColumnOfIssuesTableTest {
 
@@ -36,10 +45,27 @@ public class DropPrimaryKeyOnKeeColumnOfIssuesTableTest {
     new DropPrimaryKeySqlGenerator(db.database(), new DbPrimaryKeyConstraintFinder(db.database())));
 
   @Test
-  public void migration_should_drop_PK_on_ce_activity() throws SQLException {
+  public void migration_should_have_no_effect_when_using_h2_database() throws SQLException {
     db.assertPrimaryKey("issues", "pk_issues", "kee");
     underTest.execute();
-    db.assertNoPrimaryKey("issues");
+    db.assertPrimaryKey("issues", "pk_issues", "kee");
+  }
+
+  @Test
+  public void migration_should_call_generator_when_using_oracle_database() throws SQLException {
+    DropPrimaryKeySqlGenerator generator = mock(DropPrimaryKeySqlGenerator.class);
+    Database mockedDb = mock(Database.class);
+    DropPrimaryKeyOnKeeColumnOfIssuesTable underTest = new DropPrimaryKeyOnKeeColumnOfIssuesTable(
+      mockedDb, generator);
+
+    DdlChange.Context context = mock(DdlChange.Context.class);
+    Dialect dialect = mock(Dialect.class);
+    when(dialect.getId()).thenReturn(Oracle.ID);
+    when(mockedDb.getDialect()).thenReturn(dialect);
+
+    underTest.execute(context);
+
+    verify(generator, only()).generate("issues", "kee", false);
   }
 
   @Test
@@ -48,6 +74,6 @@ public class DropPrimaryKeyOnKeeColumnOfIssuesTableTest {
     underTest.execute();
     // re-entrant
     underTest.execute();
-    db.assertNoPrimaryKey("issues");
+    db.assertPrimaryKey("issues", "pk_issues", "kee");
   }
 }
index ed89fdd9c210fc34a574c08378657f7a37802524..64e792047676f842a2fa28587d02fbdbbf43e780 100644 (file)
@@ -25,7 +25,8 @@ CREATE TABLE "ISSUES"(
     "PROJECT_UUID" VARCHAR(50),
     "LOCATIONS" BLOB,
     "ISSUE_TYPE" TINYINT,
-    "FROM_HOTSPOT" BOOLEAN
+    "FROM_HOTSPOT" BOOLEAN,
+     CONSTRAINT pk_issues PRIMARY KEY (KEE)
 );
 CREATE INDEX "ISSUES_ASSIGNEE" ON "ISSUES"("ASSIGNEE");
 CREATE INDEX "ISSUES_COMPONENT_UUID" ON "ISSUES"("COMPONENT_UUID");