]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9880 Fix missing migrations
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 19 Oct 2017 12:28:44 +0000 (14:28 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 19 Oct 2017 13:46:28 +0000 (15:46 +0200)
14 files changed:
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/PopulateAnalysisUuidColumnOnWebhookDeliveries.java [deleted file]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveries.java [deleted file]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/DbVersion67.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/PopulateAnalysisUuidColumnOnWebhookDeliveries.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveries.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/PopulateAnalysisUuidColumnOnWebhookDeliveriesTest.java [deleted file]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveriesTest.java [deleted file]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/DbVersion67Test.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/PopulateAnalysisUuidColumnOnWebhookDeliveriesTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveriesTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/PopulateAnalysisUuidColumnOnWebhookDeliveriesTest/initial.sql [deleted file]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveriesTest/initial.sql [deleted file]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v67/PopulateAnalysisUuidColumnOnWebhookDeliveriesTest/initial.sql [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v67/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveriesTest/initial.sql [new file with mode: 0644]

diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/PopulateAnalysisUuidColumnOnWebhookDeliveries.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/PopulateAnalysisUuidColumnOnWebhookDeliveries.java
deleted file mode 100644 (file)
index 05b4bd7..0000000
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2017 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.v66;
-
-import java.sql.SQLException;
-import org.sonar.db.Database;
-import org.sonar.server.platform.db.migration.step.DataChange;
-import org.sonar.server.platform.db.migration.step.MassUpdate;
-import org.sonar.server.platform.db.migration.step.Select;
-import org.sonar.server.platform.db.migration.step.SqlStatement;
-
-public class PopulateAnalysisUuidColumnOnWebhookDeliveries extends DataChange {
-
-  public PopulateAnalysisUuidColumnOnWebhookDeliveries(Database db) {
-    super(db);
-  }
-
-  @Override
-  public void execute(Context context) throws SQLException {
-    MassUpdate massUpdate = context.prepareMassUpdate();
-    massUpdate.select("SELECT wd.uuid, ca.analysis_uuid " +
-      " FROM webhook_deliveries wd " +
-      " INNER JOIN ce_activity ca ON ca.uuid=wd.ce_task_uuid " +
-      " WHERE wd.analysis_uuid IS NULL AND ca.analysis_uuid IS NOT NULL");
-    massUpdate.update("UPDATE webhook_deliveries SET analysis_uuid=? WHERE uuid=?");
-    massUpdate.rowPluralName("webhook_deliveries");
-    massUpdate.execute(PopulateAnalysisUuidColumnOnWebhookDeliveries::handle);
-  }
-
-  private static boolean handle(Select.Row row, SqlStatement update) throws SQLException {
-    String uuid = row.getString(1);
-    String analysisUuid = row.getString(2);
-
-    update.setString(1, analysisUuid);
-    update.setString(2, uuid);
-
-    return true;
-  }
-}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveries.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveries.java
deleted file mode 100644 (file)
index 236b3cb..0000000
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2017 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.v66;
-
-import java.sql.SQLException;
-import org.sonar.db.Database;
-import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
-import org.sonar.server.platform.db.migration.sql.AlterColumnsBuilder;
-import org.sonar.server.platform.db.migration.step.DdlChange;
-
-public class UpdateCeTaskUuidColumnToNullableOnWebhookDeliveries extends DdlChange {
-
-  public static final String TABLE_WEBHOOK_DELIVERIES = "webhook_deliveries";
-
-  public UpdateCeTaskUuidColumnToNullableOnWebhookDeliveries(Database db) {
-    super(db);
-  }
-
-  @Override
-  public void execute(Context context) throws SQLException {
-    context.execute(new AlterColumnsBuilder(getDialect(), TABLE_WEBHOOK_DELIVERIES)
-      .updateColumn(VarcharColumnDef.newVarcharColumnDefBuilder()
-        .setColumnName("ce_task_uuid")
-        .setLimit(40)
-        .setIsNullable(true)
-        .build())
-      .build());
-  }
-}
index 0a367054e1857d4f9cddd3f26c29a3df08d034bb..c6eddef0edab8259af5486e127c0fd0cf88e2bac 100644 (file)
@@ -30,6 +30,8 @@ public class DbVersion67 implements DbVersion {
       .add(1831, "Add webhook_deliveries.analysis_uuid", AddAnalysisUuidToWebhookDeliveries.class)
       .add(1832, "Create table ANALYSIS_PROPERTIES", CreateTableAnalysisProperties.class)
       .add(1833, "Cleanup disabled users", CleanupDisabledUsers.class)
+      .add(1834, "Set WEBHOOK_DELIVERIES.CE_TASK_UUID as nullable", UpdateCeTaskUuidColumnToNullableOnWebhookDeliveries.class)
+      .add(1835, "Populate WEBHOOK_DELIVERIES.ANALYSIS_UUID", PopulateAnalysisUuidColumnOnWebhookDeliveries.class)
     ;
   }
 }
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/PopulateAnalysisUuidColumnOnWebhookDeliveries.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/PopulateAnalysisUuidColumnOnWebhookDeliveries.java
new file mode 100644 (file)
index 0000000..7e7881c
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 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.v67;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.step.DataChange;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+import org.sonar.server.platform.db.migration.step.Select;
+import org.sonar.server.platform.db.migration.step.SqlStatement;
+
+public class PopulateAnalysisUuidColumnOnWebhookDeliveries extends DataChange {
+
+  public PopulateAnalysisUuidColumnOnWebhookDeliveries(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    MassUpdate massUpdate = context.prepareMassUpdate();
+    massUpdate.select("SELECT wd.uuid, ca.analysis_uuid " +
+      " FROM webhook_deliveries wd " +
+      " INNER JOIN ce_activity ca ON ca.uuid=wd.ce_task_uuid " +
+      " WHERE wd.analysis_uuid IS NULL AND ca.analysis_uuid IS NOT NULL");
+    massUpdate.update("UPDATE webhook_deliveries SET analysis_uuid=? WHERE uuid=?");
+    massUpdate.rowPluralName("webhook_deliveries");
+    massUpdate.execute(PopulateAnalysisUuidColumnOnWebhookDeliveries::handle);
+  }
+
+  private static boolean handle(Select.Row row, SqlStatement update) throws SQLException {
+    String uuid = row.getString(1);
+    String analysisUuid = row.getString(2);
+
+    update.setString(1, analysisUuid);
+    update.setString(2, uuid);
+
+    return true;
+  }
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveries.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveries.java
new file mode 100644 (file)
index 0000000..a53ffdc
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 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.v67;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
+import org.sonar.server.platform.db.migration.sql.AlterColumnsBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+public class UpdateCeTaskUuidColumnToNullableOnWebhookDeliveries extends DdlChange {
+
+  public static final String TABLE_WEBHOOK_DELIVERIES = "webhook_deliveries";
+
+  public UpdateCeTaskUuidColumnToNullableOnWebhookDeliveries(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    context.execute(new AlterColumnsBuilder(getDialect(), TABLE_WEBHOOK_DELIVERIES)
+      .updateColumn(VarcharColumnDef.newVarcharColumnDefBuilder()
+        .setColumnName("ce_task_uuid")
+        .setLimit(40)
+        .setIsNullable(true)
+        .build())
+      .build());
+  }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/PopulateAnalysisUuidColumnOnWebhookDeliveriesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/PopulateAnalysisUuidColumnOnWebhookDeliveriesTest.java
deleted file mode 100644 (file)
index a8d73b2..0000000
+++ /dev/null
@@ -1,109 +0,0 @@
-package org.sonar.server.platform.db.migration.version.v66;/*
- * SonarQube
- * Copyright (C) 2009-2017 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.
- */
-
-import java.sql.SQLException;
-import java.util.List;
-import java.util.Map;
-import javax.annotation.Nullable;
-import org.apache.commons.lang.math.RandomUtils;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.sonar.db.CoreDbTester;
-
-import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.entry;
-
-public class PopulateAnalysisUuidColumnOnWebhookDeliveriesTest {
-  private static final String TABLE_WEBHOOK_DELIVERY = "webhook_deliveries";
-  private static final String TABLE_CE_ACTIVITIY = "ce_activity";
-
-  @Rule
-  public final CoreDbTester db = CoreDbTester.createForSchema(PopulateAnalysisUuidColumnOnWebhookDeliveriesTest.class, "initial.sql");
-  @Rule
-  public ExpectedException expectedException = ExpectedException.none();
-
-  private PopulateAnalysisUuidColumnOnWebhookDeliveries underTest = new PopulateAnalysisUuidColumnOnWebhookDeliveries(db.database());
-
-  @Test
-  public void migration_must_set_analysis_uuid() throws SQLException {
-    String ceTaskUuid = randomAlphanumeric(40);
-    String analysisUuid = randomAlphanumeric(40);
-    String webhookDeliveryUuid = randomAlphanumeric(40);
-    insertWebhookDelivery(webhookDeliveryUuid,null, ceTaskUuid);
-    insertCeActivity(ceTaskUuid, analysisUuid);
-
-    assertThat(db.countRowsOfTable(TABLE_WEBHOOK_DELIVERY)).isEqualTo(1);
-    assertThat(db.countRowsOfTable(TABLE_CE_ACTIVITIY)).isEqualTo(1);
-    underTest.execute();
-
-    List<Map<String, Object>> maps = selectAllWebhookDeliveries();
-    assertThat(maps).hasSize(1);
-    assertThat(maps.get(0)).containsExactly(
-      entry("ANALYSIS_UUID", analysisUuid), entry("UUID", webhookDeliveryUuid), entry("CE_TASK_UUID", ceTaskUuid));
-  }
-
-  @Test
-  public void migration_should_be_reentrant() throws SQLException {
-    for (int i = 0; i < 10; i++) {
-      insertWebhookDelivery(randomAlphanumeric(40),null, randomAlphanumeric(40));
-      insertCeActivity(randomAlphanumeric(40), randomAlphanumeric(40));
-    }
-
-    underTest.execute();
-    List<Map<String, Object>> firstExecutionResult = selectAllWebhookDeliveries();
-    underTest.execute();
-    assertThat(selectAllWebhookDeliveries()).isEqualTo(firstExecutionResult);
-  }
-
-  private List<Map<String, Object>> selectAllWebhookDeliveries() {
-    return db.select("select uuid, ce_task_uuid, analysis_uuid from webhook_deliveries");
-  }
-
-  private void insertCeActivity(String uuid, String analysisUuid) {
-    db.executeInsert(TABLE_CE_ACTIVITIY,
-      "UUID", uuid,
-      "TASK_TYPE", randomAlphanumeric(5),
-      "ANALYSIS_UUID", analysisUuid,
-      "STATUS", randomAlphanumeric(5),
-      "IS_LAST", RandomUtils.nextBoolean(),
-      "IS_LAST_KEY", randomAlphanumeric(50),
-      "EXECUTION_COUNT", RandomUtils.nextInt(10),
-      "SUBMITTED_AT", RandomUtils.nextInt(),
-      "CREATED_AT", RandomUtils.nextInt(),
-      "UPDATED_AT", RandomUtils.nextInt()
-    );
-  }
-
-  private void insertWebhookDelivery(String uuid, @Nullable String analysisUuid, String ceTaskUuid) {
-    db.executeInsert(TABLE_WEBHOOK_DELIVERY,
-      "UUID", uuid,
-      "COMPONENT_UUID", randomAlphanumeric(30),
-      "ANALYSIS_UUID", analysisUuid,
-      "CE_TASK_UUID", ceTaskUuid,
-      "NAME", randomAlphanumeric(15),
-      "URL", randomAlphanumeric(15),
-      "SUCCESS", RandomUtils.nextBoolean(),
-      "PAYLOAD", randomAlphanumeric(200),
-      "CREATED_AT", RandomUtils.nextInt()
-    );
-  }
-}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveriesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveriesTest.java
deleted file mode 100644 (file)
index f2d234c..0000000
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2017 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.v66;
-
-import java.sql.SQLException;
-import java.sql.Types;
-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 UpdateCeTaskUuidColumnToNullableOnWebhookDeliveriesTest {
-  private static final String TABLE = "webhook_deliveries";
-
-  @Rule
-  public final CoreDbTester db = CoreDbTester.createForSchema(UpdateCeTaskUuidColumnToNullableOnWebhookDeliveriesTest.class, "initial.sql");
-  @Rule
-  public ExpectedException expectedException = ExpectedException.none();
-
-  private UpdateCeTaskUuidColumnToNullableOnWebhookDeliveries underTest = new UpdateCeTaskUuidColumnToNullableOnWebhookDeliveries(db.database());
-
-  @Test
-  public void update_column() throws SQLException {
-    underTest.execute();
-
-    assertThat(db.countRowsOfTable(TABLE)).isEqualTo(0);
-    db.assertColumnDefinition(TABLE, "ce_task_uuid", Types.VARCHAR, 40, true);
-    db.assertColumnDefinition(TABLE, "analysis_uuid", Types.VARCHAR, 40, true);
-  }
-
-  @Test
-  public void migration_is_reentrant() throws SQLException {
-    underTest.execute();
-    underTest.execute();
-  }
-}
index 2d59608cc874a382928518f26a5f531bf4213ffe..dc54ab65e84fed6685f4ba384e5819b197896c44 100644 (file)
@@ -36,7 +36,7 @@ public class DbVersion67Test {
 
   @Test
   public void verify_migration_count() {
-    verifyMigrationCount(underTest, 4);
+    verifyMigrationCount(underTest, 6);
   }
 
 }
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/PopulateAnalysisUuidColumnOnWebhookDeliveriesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/PopulateAnalysisUuidColumnOnWebhookDeliveriesTest.java
new file mode 100644 (file)
index 0000000..9c64b10
--- /dev/null
@@ -0,0 +1,109 @@
+package org.sonar.server.platform.db.migration.version.v67;/*
+ * SonarQube
+ * Copyright (C) 2009-2017 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.
+ */
+
+import java.sql.SQLException;
+import java.util.List;
+import java.util.Map;
+import javax.annotation.Nullable;
+import org.apache.commons.lang.math.RandomUtils;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.db.CoreDbTester;
+
+import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.entry;
+
+public class PopulateAnalysisUuidColumnOnWebhookDeliveriesTest {
+  private static final String TABLE_WEBHOOK_DELIVERY = "webhook_deliveries";
+  private static final String TABLE_CE_ACTIVITIY = "ce_activity";
+
+  @Rule
+  public final CoreDbTester db = CoreDbTester.createForSchema(PopulateAnalysisUuidColumnOnWebhookDeliveriesTest.class, "initial.sql");
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  private PopulateAnalysisUuidColumnOnWebhookDeliveries underTest = new PopulateAnalysisUuidColumnOnWebhookDeliveries(db.database());
+
+  @Test
+  public void migration_must_set_analysis_uuid() throws SQLException {
+    String ceTaskUuid = randomAlphanumeric(40);
+    String analysisUuid = randomAlphanumeric(40);
+    String webhookDeliveryUuid = randomAlphanumeric(40);
+    insertWebhookDelivery(webhookDeliveryUuid,null, ceTaskUuid);
+    insertCeActivity(ceTaskUuid, analysisUuid);
+
+    assertThat(db.countRowsOfTable(TABLE_WEBHOOK_DELIVERY)).isEqualTo(1);
+    assertThat(db.countRowsOfTable(TABLE_CE_ACTIVITIY)).isEqualTo(1);
+    underTest.execute();
+
+    List<Map<String, Object>> maps = selectAllWebhookDeliveries();
+    assertThat(maps).hasSize(1);
+    assertThat(maps.get(0)).containsExactly(
+      entry("ANALYSIS_UUID", analysisUuid), entry("UUID", webhookDeliveryUuid), entry("CE_TASK_UUID", ceTaskUuid));
+  }
+
+  @Test
+  public void migration_should_be_reentrant() throws SQLException {
+    for (int i = 0; i < 10; i++) {
+      insertWebhookDelivery(randomAlphanumeric(40),null, randomAlphanumeric(40));
+      insertCeActivity(randomAlphanumeric(40), randomAlphanumeric(40));
+    }
+
+    underTest.execute();
+    List<Map<String, Object>> firstExecutionResult = selectAllWebhookDeliveries();
+    underTest.execute();
+    assertThat(selectAllWebhookDeliveries()).isEqualTo(firstExecutionResult);
+  }
+
+  private List<Map<String, Object>> selectAllWebhookDeliveries() {
+    return db.select("select uuid, ce_task_uuid, analysis_uuid from webhook_deliveries");
+  }
+
+  private void insertCeActivity(String uuid, String analysisUuid) {
+    db.executeInsert(TABLE_CE_ACTIVITIY,
+      "UUID", uuid,
+      "TASK_TYPE", randomAlphanumeric(5),
+      "ANALYSIS_UUID", analysisUuid,
+      "STATUS", randomAlphanumeric(5),
+      "IS_LAST", RandomUtils.nextBoolean(),
+      "IS_LAST_KEY", randomAlphanumeric(50),
+      "EXECUTION_COUNT", RandomUtils.nextInt(10),
+      "SUBMITTED_AT", RandomUtils.nextInt(),
+      "CREATED_AT", RandomUtils.nextInt(),
+      "UPDATED_AT", RandomUtils.nextInt()
+    );
+  }
+
+  private void insertWebhookDelivery(String uuid, @Nullable String analysisUuid, String ceTaskUuid) {
+    db.executeInsert(TABLE_WEBHOOK_DELIVERY,
+      "UUID", uuid,
+      "COMPONENT_UUID", randomAlphanumeric(30),
+      "ANALYSIS_UUID", analysisUuid,
+      "CE_TASK_UUID", ceTaskUuid,
+      "NAME", randomAlphanumeric(15),
+      "URL", randomAlphanumeric(15),
+      "SUCCESS", RandomUtils.nextBoolean(),
+      "PAYLOAD", randomAlphanumeric(200),
+      "CREATED_AT", RandomUtils.nextInt()
+    );
+  }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveriesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveriesTest.java
new file mode 100644 (file)
index 0000000..93ed836
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 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.v67;
+
+import java.sql.SQLException;
+import java.sql.Types;
+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 UpdateCeTaskUuidColumnToNullableOnWebhookDeliveriesTest {
+  private static final String TABLE = "webhook_deliveries";
+
+  @Rule
+  public final CoreDbTester db = CoreDbTester.createForSchema(UpdateCeTaskUuidColumnToNullableOnWebhookDeliveriesTest.class, "initial.sql");
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  private UpdateCeTaskUuidColumnToNullableOnWebhookDeliveries underTest = new UpdateCeTaskUuidColumnToNullableOnWebhookDeliveries(db.database());
+
+  @Test
+  public void update_column() throws SQLException {
+    underTest.execute();
+
+    assertThat(db.countRowsOfTable(TABLE)).isEqualTo(0);
+    db.assertColumnDefinition(TABLE, "ce_task_uuid", Types.VARCHAR, 40, true);
+    db.assertColumnDefinition(TABLE, "analysis_uuid", Types.VARCHAR, 40, true);
+  }
+
+  @Test
+  public void migration_is_reentrant() throws SQLException {
+    underTest.execute();
+    underTest.execute();
+  }
+}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/PopulateAnalysisUuidColumnOnWebhookDeliveriesTest/initial.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/PopulateAnalysisUuidColumnOnWebhookDeliveriesTest/initial.sql
deleted file mode 100644 (file)
index de52090..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-CREATE TABLE "CE_ACTIVITY" (
-  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
-  "UUID" VARCHAR(40) NOT NULL,
-  "TASK_TYPE" VARCHAR(15) NOT NULL,
-  "COMPONENT_UUID" VARCHAR(40) NULL,
-  "ANALYSIS_UUID" VARCHAR(50) NULL,
-  "STATUS" VARCHAR(15) NOT NULL,
-  "IS_LAST" BOOLEAN NOT NULL,
-  "IS_LAST_KEY" VARCHAR(55) NOT NULL,
-  "SUBMITTER_LOGIN" VARCHAR(255) NULL,
-  "WORKER_UUID" VARCHAR(40) NULL,
-  "EXECUTION_COUNT" INTEGER NOT NULL,
-  "SUBMITTED_AT" BIGINT NOT NULL,
-  "STARTED_AT" BIGINT NULL,
-  "EXECUTED_AT" BIGINT NULL,
-  "CREATED_AT" BIGINT NOT NULL,
-  "UPDATED_AT" BIGINT NOT NULL,
-  "EXECUTION_TIME_MS" BIGINT NULL,
-  "ERROR_MESSAGE" VARCHAR(1000),
-  "ERROR_STACKTRACE" CLOB(2147483647)
-);
-
-CREATE UNIQUE INDEX "CE_ACTIVITY_UUID" ON "CE_ACTIVITY" ("UUID");
-CREATE INDEX "CE_ACTIVITY_COMPONENT_UUID" ON "CE_ACTIVITY" ("COMPONENT_UUID");
-CREATE INDEX "CE_ACTIVITY_ISLASTKEY" ON "CE_ACTIVITY" ("IS_LAST_KEY");
-CREATE INDEX "CE_ACTIVITY_ISLAST_STATUS" ON "CE_ACTIVITY" ("IS_LAST", "STATUS");
-
-
-CREATE TABLE "WEBHOOK_DELIVERIES" (
-  "UUID" VARCHAR(40) NOT NULL PRIMARY KEY,
-  "COMPONENT_UUID" VARCHAR(40) NOT NULL,
-  "ANALYSIS_UUID" VARCHAR(40),
-  "CE_TASK_UUID" VARCHAR(40),
-  "NAME" VARCHAR(100) NOT NULL,
-  "URL" VARCHAR(2000) NOT NULL,
-  "SUCCESS" BOOLEAN NOT NULL,
-  "HTTP_STATUS" INT,
-  "DURATION_MS" INT,
-  "PAYLOAD" CLOB NOT NULL,
-  "ERROR_STACKTRACE" CLOB,
-  "CREATED_AT" BIGINT NOT NULL
-);
-CREATE UNIQUE INDEX "PK_WEBHOOK_DELIVERIES" ON "WEBHOOK_DELIVERIES" ("UUID");
-CREATE INDEX "COMPONENT_UUID" ON "WEBHOOK_DELIVERIES" ("COMPONENT_UUID");
-CREATE INDEX "CE_TASK_UUID" ON "WEBHOOK_DELIVERIES" ("CE_TASK_UUID");
-CREATE INDEX "ANALYSES_UUID" ON "WEBHOOK_DELIVERIES" ("ANALYSIS_UUID");
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveriesTest/initial.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveriesTest/initial.sql
deleted file mode 100644 (file)
index 0e120b5..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-CREATE TABLE "WEBHOOK_DELIVERIES" (
-  "UUID" VARCHAR(40) NOT NULL PRIMARY KEY,
-  "COMPONENT_UUID" VARCHAR(40) NOT NULL,
-  "ANALYSIS_UUID" VARCHAR(40) NULL,
-  "CE_TASK_UUID" VARCHAR(40) NOT NULL,
-  "NAME" VARCHAR(100) NOT NULL,
-  "URL" VARCHAR(2000) NOT NULL,
-  "SUCCESS" BOOLEAN NOT NULL,
-  "HTTP_STATUS" INT,
-  "DURATION_MS" INT,
-  "PAYLOAD" CLOB NOT NULL,
-  "ERROR_STACKTRACE" CLOB,
-  "CREATED_AT" BIGINT NOT NULL
-);
-CREATE UNIQUE INDEX "PK_WEBHOOK_DELIVERIES" ON "WEBHOOK_DELIVERIES" ("UUID");
-CREATE INDEX "COMPONENT_UUID" ON "WEBHOOK_DELIVERIES" ("COMPONENT_UUID");
-CREATE INDEX "CE_TASK_UUID" ON "WEBHOOK_DELIVERIES" ("CE_TASK_UUID");
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v67/PopulateAnalysisUuidColumnOnWebhookDeliveriesTest/initial.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v67/PopulateAnalysisUuidColumnOnWebhookDeliveriesTest/initial.sql
new file mode 100644 (file)
index 0000000..de52090
--- /dev/null
@@ -0,0 +1,46 @@
+CREATE TABLE "CE_ACTIVITY" (
+  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "UUID" VARCHAR(40) NOT NULL,
+  "TASK_TYPE" VARCHAR(15) NOT NULL,
+  "COMPONENT_UUID" VARCHAR(40) NULL,
+  "ANALYSIS_UUID" VARCHAR(50) NULL,
+  "STATUS" VARCHAR(15) NOT NULL,
+  "IS_LAST" BOOLEAN NOT NULL,
+  "IS_LAST_KEY" VARCHAR(55) NOT NULL,
+  "SUBMITTER_LOGIN" VARCHAR(255) NULL,
+  "WORKER_UUID" VARCHAR(40) NULL,
+  "EXECUTION_COUNT" INTEGER NOT NULL,
+  "SUBMITTED_AT" BIGINT NOT NULL,
+  "STARTED_AT" BIGINT NULL,
+  "EXECUTED_AT" BIGINT NULL,
+  "CREATED_AT" BIGINT NOT NULL,
+  "UPDATED_AT" BIGINT NOT NULL,
+  "EXECUTION_TIME_MS" BIGINT NULL,
+  "ERROR_MESSAGE" VARCHAR(1000),
+  "ERROR_STACKTRACE" CLOB(2147483647)
+);
+
+CREATE UNIQUE INDEX "CE_ACTIVITY_UUID" ON "CE_ACTIVITY" ("UUID");
+CREATE INDEX "CE_ACTIVITY_COMPONENT_UUID" ON "CE_ACTIVITY" ("COMPONENT_UUID");
+CREATE INDEX "CE_ACTIVITY_ISLASTKEY" ON "CE_ACTIVITY" ("IS_LAST_KEY");
+CREATE INDEX "CE_ACTIVITY_ISLAST_STATUS" ON "CE_ACTIVITY" ("IS_LAST", "STATUS");
+
+
+CREATE TABLE "WEBHOOK_DELIVERIES" (
+  "UUID" VARCHAR(40) NOT NULL PRIMARY KEY,
+  "COMPONENT_UUID" VARCHAR(40) NOT NULL,
+  "ANALYSIS_UUID" VARCHAR(40),
+  "CE_TASK_UUID" VARCHAR(40),
+  "NAME" VARCHAR(100) NOT NULL,
+  "URL" VARCHAR(2000) NOT NULL,
+  "SUCCESS" BOOLEAN NOT NULL,
+  "HTTP_STATUS" INT,
+  "DURATION_MS" INT,
+  "PAYLOAD" CLOB NOT NULL,
+  "ERROR_STACKTRACE" CLOB,
+  "CREATED_AT" BIGINT NOT NULL
+);
+CREATE UNIQUE INDEX "PK_WEBHOOK_DELIVERIES" ON "WEBHOOK_DELIVERIES" ("UUID");
+CREATE INDEX "COMPONENT_UUID" ON "WEBHOOK_DELIVERIES" ("COMPONENT_UUID");
+CREATE INDEX "CE_TASK_UUID" ON "WEBHOOK_DELIVERIES" ("CE_TASK_UUID");
+CREATE INDEX "ANALYSES_UUID" ON "WEBHOOK_DELIVERIES" ("ANALYSIS_UUID");
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v67/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveriesTest/initial.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v67/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveriesTest/initial.sql
new file mode 100644 (file)
index 0000000..0e120b5
--- /dev/null
@@ -0,0 +1,17 @@
+CREATE TABLE "WEBHOOK_DELIVERIES" (
+  "UUID" VARCHAR(40) NOT NULL PRIMARY KEY,
+  "COMPONENT_UUID" VARCHAR(40) NOT NULL,
+  "ANALYSIS_UUID" VARCHAR(40) NULL,
+  "CE_TASK_UUID" VARCHAR(40) NOT NULL,
+  "NAME" VARCHAR(100) NOT NULL,
+  "URL" VARCHAR(2000) NOT NULL,
+  "SUCCESS" BOOLEAN NOT NULL,
+  "HTTP_STATUS" INT,
+  "DURATION_MS" INT,
+  "PAYLOAD" CLOB NOT NULL,
+  "ERROR_STACKTRACE" CLOB,
+  "CREATED_AT" BIGINT NOT NULL
+);
+CREATE UNIQUE INDEX "PK_WEBHOOK_DELIVERIES" ON "WEBHOOK_DELIVERIES" ("UUID");
+CREATE INDEX "COMPONENT_UUID" ON "WEBHOOK_DELIVERIES" ("COMPONENT_UUID");
+CREATE INDEX "CE_TASK_UUID" ON "WEBHOOK_DELIVERIES" ("CE_TASK_UUID");