]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11791 truncate content of table ES_QUEUE
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 8 Mar 2019 09:40:29 +0000 (10:40 +0100)
committerSonarTech <sonartech@sonarsource.com>
Tue, 19 Mar 2019 19:21:19 +0000 (20:21 +0100)
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v77/DbVersion77.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v77/TruncateEsQueue.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v77/DbVersion77Test.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v77/TruncateEsQueueTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v77/TruncateEsQueueTest/es_queue.sql [new file with mode: 0644]

index c03988be19046eaa8f81efe3ca480afcdbe4640b..e4d052bb8d1b433371956e3bd5f815edf45fbc82 100644 (file)
@@ -36,6 +36,9 @@ public class DbVersion77 implements DbVersion {
       .add(2606, "Drop DATA_TYPE column from FILE_SOURCES table", DropDataTypeFromFileSources.class)
       .add(2607, "Add MEMBERS_SYNC_ENABLED column to ORGANIZATIONS_ALM_BINDING table", AddMembersSyncFlagToOrgAlmBinding.class)
       .add(2608, "Delete favorites on not supported components", DeleteFavouritesOnNotSupportedComponentQualifiers.class)
-      .add(2609, "Delete exceeding favorites when there are more than 100 for a user", DeleteFavoritesExceedingOneHundred.class);
+      .add(2609, "Delete exceeding favorites when there are more than 100 for a user", DeleteFavoritesExceedingOneHundred.class)
+      .add(2610, "Truncate ES_QUEUE table content", TruncateEsQueue.class)
+
+    ;
   }
 }
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v77/TruncateEsQueue.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v77/TruncateEsQueue.java
new file mode 100644 (file)
index 0000000..b0c1650
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.v77;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.step.DataChange;
+
+/**
+ * This DB migration comes with the change of ES indices structures and must not be flagged as compatible with BlueGreen
+ * deployment because ES indices must be recreated and this is not compatible with BlueGreen.
+ */
+public class TruncateEsQueue extends DataChange {
+  public TruncateEsQueue(Database db) {
+    super(db);
+  }
+
+  @Override
+  protected void execute(Context context) throws SQLException {
+    context.prepareUpsert("truncate table es_queue").execute().commit();
+  }
+}
index 2732fa61caa163eced2926b5be061dba7e2c1293..001eef0e5214f0c815ecd86066363b6e6355d883 100644 (file)
@@ -36,7 +36,7 @@ public class DbVersion77Test {
 
   @Test
   public void verify_migration_count() {
-    verifyMigrationCount(underTest, 10);
+    verifyMigrationCount(underTest, 11);
   }
 
 }
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v77/TruncateEsQueueTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v77/TruncateEsQueueTest.java
new file mode 100644 (file)
index 0000000..db62732
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.v77;
+
+import java.sql.SQLException;
+import java.util.Random;
+import java.util.stream.IntStream;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.db.CoreDbTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class TruncateEsQueueTest {
+  private static final String ES_QUEUE_TABLE = "es_queue";
+
+  @Rule
+  public final CoreDbTester db = CoreDbTester.createForSchema(TruncateEsQueueTest.class, "es_queue.sql");
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  private TruncateEsQueue underTest = new TruncateEsQueue(db.database());
+
+  @Test
+  public void migration_does_not_fail_on_empty_table() throws SQLException {
+    assertThat(db.countRowsOfTable(ES_QUEUE_TABLE)).isZero();
+
+    underTest.execute();
+
+    assertThat(db.countRowsOfTable(ES_QUEUE_TABLE)).isZero();
+  }
+
+  @Test
+  public void migration_truncates_content_of_table_es_queue() throws SQLException {
+    int count = insertData();
+    assertThat(db.countRowsOfTable(ES_QUEUE_TABLE)).isEqualTo(count);
+
+    underTest.execute();
+
+    assertThat(db.countRowsOfTable(ES_QUEUE_TABLE)).isZero();
+  }
+
+  @Test
+  public void migration_is_reentrant() throws SQLException {
+    int count = insertData();
+    assertThat(db.countRowsOfTable(ES_QUEUE_TABLE)).isEqualTo(count);
+
+    underTest.execute();
+    assertThat(db.countRowsOfTable(ES_QUEUE_TABLE)).isZero();
+
+    underTest.execute();
+    assertThat(db.countRowsOfTable(ES_QUEUE_TABLE)).isZero();
+  }
+
+  private int insertData() {
+    int count = 5 + new Random().nextInt(200);
+    IntStream.range(0, count)
+      .forEach(i -> db.executeInsert(
+        ES_QUEUE_TABLE,
+        "UUID", "uuid_" + i,
+        "DOC_TYPE", "doc_type_" + i,
+        "DOC_ID", "doc_id_" + i,
+        "CREATED_AT", String.valueOf(i)
+      ));
+    return count;
+  }
+}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v77/TruncateEsQueueTest/es_queue.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v77/TruncateEsQueueTest/es_queue.sql
new file mode 100644 (file)
index 0000000..d110642
--- /dev/null
@@ -0,0 +1,11 @@
+CREATE TABLE "ES_QUEUE" (
+  "UUID" VARCHAR(40) NOT NULL,
+  "DOC_TYPE" VARCHAR(40) NOT NULL,
+  "DOC_ID" VARCHAR(4000) NOT NULL,
+  "DOC_ID_TYPE" VARCHAR(20),
+  "DOC_ROUTING" VARCHAR(4000),
+  "CREATED_AT" BIGINT NOT NULL,
+
+  CONSTRAINT "PK_ES_QUEUE" PRIMARY KEY ("UUID")
+);
+CREATE INDEX "ES_QUEUE_CREATED_AT" ON "ES_QUEUE" ("CREATED_AT");