From 638b7fd2ad828c4e0776d0a5332d248ddfd22390 Mon Sep 17 00:00:00 2001 From: Lukasz Jarocki Date: Thu, 22 Jun 2023 12:16:00 +0200 Subject: [PATCH] SONAR-19556 updated webhooks to not rely on component_uuid --- .../java/org/sonar/db/purge/PurgeDaoIT.java | 4 +- .../org/sonar/db/webhook/WebhookDaoIT.java | 2 +- .../db/webhook/WebhookDeliveryDaoIT.java | 89 +++++++++---------- .../sonar/db/webhook/WebhookDeliveryDao.java | 14 +-- .../sonar/db/webhook/WebhookDeliveryDto.java | 2 +- .../db/webhook/WebhookDeliveryLiteDto.java | 14 +-- .../db/webhook/WebhookDeliveryMapper.java | 6 +- .../org/sonar/db/purge/PurgeMapper.xml | 2 +- .../db/webhook/WebhookDeliveryMapper.xml | 18 ++-- server/sonar-db-dao/src/schema/schema-sq.ddl | 4 +- .../db/webhook/WebhookDeliveryDbTester.java | 2 +- .../db/webhook/WebhookDeliveryTesting.java | 6 +- ...teIndexProjectUuidInWebhookDeliveries.java | 41 +++++++++ .../migration/version/v102/DbVersion102.java | 6 ++ ...IndexComponentUuidInWebhookDeliveries.java | 33 +++++++ ...enameComponentUuidInWebhookDeliveries.java | 34 +++++++ .../RenameVarcharColumnAbstractTest.java | 58 ++++++++++++ ...dexProjectUuidInWebhookDeliveriesTest.java | 49 ++++++++++ ...xComponentUuidInWebhookDeliveriesTest.java | 56 ++++++++++++ ...eComponentUuidInWebhookDeliveriesTest.java | 48 ++++++++++ .../schema.sql | 18 ++++ .../schema.sql | 19 ++++ .../schema.sql | 19 ++++ .../webhook/WebhookDeliveryStorageIT.java | 4 +- .../org/sonar/server/webhook/Webhook.java | 10 +-- .../webhook/WebhookDeliveryStorage.java | 6 +- .../org/sonar/server/webhook/WebhookTest.java | 6 +- .../server/webhook/ws/CreateActionIT.java | 6 +- .../server/webhook/ws/DeleteActionIT.java | 2 +- .../sonar/server/webhook/ws/ListActionIT.java | 2 +- .../server/webhook/ws/UpdateActionIT.java | 2 +- .../webhook/ws/WebhookDeliveriesActionIT.java | 40 ++++----- .../webhook/ws/WebhookDeliveryActionIT.java | 17 ++-- .../webhook/ws/WebhookDeliveriesAction.java | 8 +- .../webhook/ws/WebhookDeliveryAction.java | 2 +- 35 files changed, 514 insertions(+), 135 deletions(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexProjectUuidInWebhookDeliveries.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/DropIndexComponentUuidInWebhookDeliveries.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/RenameComponentUuidInWebhookDeliveries.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/RenameVarcharColumnAbstractTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexProjectUuidInWebhookDeliveriesTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/DropIndexComponentUuidInWebhookDeliveriesTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/RenameComponentUuidInWebhookDeliveriesTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/CreateIndexProjectUuidInWebhookDeliveriesTest/schema.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/DropIndexComponentUuidInWebhookDeliveriesTest/schema.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/RenameComponentUuidInWebhookDeliveriesTest/schema.sql diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeDaoIT.java index 36562a42570..ed9bfdc1289 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeDaoIT.java @@ -1446,8 +1446,8 @@ public class PurgeDaoIT { @Test public void deleteProject_deletes_webhook_deliveries() { ComponentDto project = db.components().insertPublicProject().getMainBranchComponent(); - dbClient.webhookDeliveryDao().insert(dbSession, newDto().setComponentUuid(project.uuid()).setUuid("D1").setDurationMs(1000).setWebhookUuid("webhook-uuid")); - dbClient.webhookDeliveryDao().insert(dbSession, newDto().setComponentUuid("P2").setUuid("D2").setDurationMs(1000).setWebhookUuid("webhook-uuid")); + dbClient.webhookDeliveryDao().insert(dbSession, newDto().setProjectUuid(project.uuid()).setUuid("D1").setDurationMs(1000).setWebhookUuid("webhook-uuid")); + dbClient.webhookDeliveryDao().insert(dbSession, newDto().setProjectUuid("P2").setUuid("D2").setDurationMs(1000).setWebhookUuid("webhook-uuid")); underTest.deleteProject(dbSession, project.uuid(), project.qualifier(), project.name(), project.getKey()); diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/webhook/WebhookDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/webhook/WebhookDaoIT.java index d957f15bb4e..bdaf37fb2e6 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/webhook/WebhookDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/webhook/WebhookDaoIT.java @@ -36,7 +36,7 @@ import static org.assertj.core.api.Assertions.assertThat; public class WebhookDaoIT { @Rule - public final DbTester dbTester = DbTester.create(System2.INSTANCE); + public final DbTester dbTester = DbTester.create(System2.INSTANCE, true); private final System2 system2 = System2.INSTANCE; private final DbClient dbClient = dbTester.getDbClient(); diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/webhook/WebhookDeliveryDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/webhook/WebhookDeliveryDaoIT.java index 84bb7cb39c9..ac9abf20819 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/webhook/WebhookDeliveryDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/webhook/WebhookDeliveryDaoIT.java @@ -39,8 +39,7 @@ public class WebhookDeliveryDaoIT { private static final long BEFORE = NOW - 1_000L; @Rule - public final DbTester dbTester = DbTester.create(System2.INSTANCE); - + public final DbTester dbTester = DbTester.create(System2.INSTANCE, true); private final DbClient dbClient = dbTester.getDbClient(); private final DbSession dbSession = dbTester.getSession(); @@ -55,30 +54,30 @@ public class WebhookDeliveryDaoIT { @Test public void selectOrderedByComponentUuid_returns_empty_if_no_records() { - underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1")); + underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D1", "WEBHOOK_UUID_1", "PROJECT_1", "TASK_1")); - List deliveries = underTest.selectOrderedByComponentUuid(dbSession, "ANOTHER_COMPONENT", 0, 10); + List deliveries = underTest.selectOrderedByProjectUuid(dbSession, "ANOTHER_PROJECT", 0, 10); assertThat(deliveries).isEmpty(); } @Test public void selectOrderedByComponentUuid_returns_records_ordered_by_date() { - WebhookDeliveryDto dto1 = WebhookDeliveryTesting.newDto("D1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1").setCreatedAt(BEFORE); - WebhookDeliveryDto dto2 = WebhookDeliveryTesting.newDto("D2", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1").setCreatedAt(NOW); - WebhookDeliveryDto dto3 = WebhookDeliveryTesting.newDto("D3", "WEBHOOK_UUID_1", "COMPONENT_2", "TASK_1").setCreatedAt(NOW); + WebhookDeliveryDto dto1 = WebhookDeliveryTesting.newDto("D1", "WEBHOOK_UUID_1", "PROJECT_1", "TASK_1").setCreatedAt(BEFORE); + WebhookDeliveryDto dto2 = WebhookDeliveryTesting.newDto("D2", "WEBHOOK_UUID_1", "PROJECT_1", "TASK_1").setCreatedAt(NOW); + WebhookDeliveryDto dto3 = WebhookDeliveryTesting.newDto("D3", "WEBHOOK_UUID_1", "PROJECT_2", "TASK_1").setCreatedAt(NOW); underTest.insert(dbSession, dto3); underTest.insert(dbSession, dto2); underTest.insert(dbSession, dto1); - List deliveries = underTest.selectOrderedByComponentUuid(dbSession, "COMPONENT_1", 0, 10); + List deliveries = underTest.selectOrderedByProjectUuid(dbSession, "PROJECT_1", 0, 10); assertThat(deliveries).extracting(WebhookDeliveryLiteDto::getUuid).containsExactly("D2", "D1"); } @Test public void selectOrderedByCeTaskUuid_returns_empty_if_no_records() { - underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1")); + underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D1", "WEBHOOK_UUID_1", "PROJECT_1", "TASK_1")); List deliveries = underTest.selectOrderedByCeTaskUuid(dbSession, "ANOTHER_TASK", 0, 10); @@ -87,9 +86,9 @@ public class WebhookDeliveryDaoIT { @Test public void selectOrderedByCeTaskUuid_returns_records_ordered_by_date() { - WebhookDeliveryDto dto1 = WebhookDeliveryTesting.newDto("D1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1").setCreatedAt(BEFORE); - WebhookDeliveryDto dto2 = WebhookDeliveryTesting.newDto("D2", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1").setCreatedAt(NOW); - WebhookDeliveryDto dto3 = WebhookDeliveryTesting.newDto("D3", "WEBHOOK_UUID_1", "COMPONENT_2", "TASK_2").setCreatedAt(NOW); + WebhookDeliveryDto dto1 = WebhookDeliveryTesting.newDto("D1", "WEBHOOK_UUID_1", "PROJECT_1", "TASK_1").setCreatedAt(BEFORE); + WebhookDeliveryDto dto2 = WebhookDeliveryTesting.newDto("D2", "WEBHOOK_UUID_1", "PROJECT_1", "TASK_1").setCreatedAt(NOW); + WebhookDeliveryDto dto3 = WebhookDeliveryTesting.newDto("D3", "WEBHOOK_UUID_1", "PROJECT_2", "TASK_2").setCreatedAt(NOW); underTest.insert(dbSession, dto3); underTest.insert(dbSession, dto2); underTest.insert(dbSession, dto1); @@ -102,7 +101,7 @@ public class WebhookDeliveryDaoIT { @Test public void selectByWebhookUuid_returns_empty_if_no_records() { - underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1")); + underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D1", "WEBHOOK_UUID_1", "PROJECT_1", "TASK_1")); List deliveries = underTest.selectByWebhookUuid(dbSession, "a-webhook-uuid", 0, 10); @@ -111,10 +110,10 @@ public class WebhookDeliveryDaoIT { @Test public void selectByWebhookUuid_returns_records_ordered_by_date() { - WebhookDto webhookDto = dbWebhooks.insert(WebhookTesting.newProjectWebhook("COMPONENT_1"), "COMPONENT_KEY", "COMPONENT_NAME"); - WebhookDeliveryDto dto1 = WebhookDeliveryTesting.newDto("D1", webhookDto.getUuid(), "COMPONENT_1", "TASK_1").setCreatedAt(BEFORE); - WebhookDeliveryDto dto2 = WebhookDeliveryTesting.newDto("D2", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW); - WebhookDeliveryDto dto3 = WebhookDeliveryTesting.newDto("D3", "fake-webhook-uuid", "COMPONENT_2", "TASK_1").setCreatedAt(NOW); + WebhookDto webhookDto = dbWebhooks.insert(WebhookTesting.newProjectWebhook("PROJECT_1"), "PROJECT_KEY", "PROJECT_NAME"); + WebhookDeliveryDto dto1 = WebhookDeliveryTesting.newDto("D1", webhookDto.getUuid(), "PROJECT_1", "TASK_1").setCreatedAt(BEFORE); + WebhookDeliveryDto dto2 = WebhookDeliveryTesting.newDto("D2", webhookDto.getUuid(), "PROJECT_1", "TASK_2").setCreatedAt(NOW); + WebhookDeliveryDto dto3 = WebhookDeliveryTesting.newDto("D3", "fake-webhook-uuid", "PROJECT_2", "TASK_1").setCreatedAt(NOW); underTest.insert(dbSession, dto3); underTest.insert(dbSession, dto2); underTest.insert(dbSession, dto1); @@ -126,13 +125,13 @@ public class WebhookDeliveryDaoIT { @Test public void selectByWebhookUuid_returns_records_according_to_pagination() { - WebhookDto webhookDto = dbWebhooks.insert(WebhookTesting.newProjectWebhook("COMPONENT_1"), "COMPONENT_KEY", "COMPONENT_NAME"); - underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D1", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW - 5_000L)); - underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D2", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW - 4_000L)); - underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D3", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW - 3_000L)); - underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D4", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW - 2_000L)); - underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D5", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW - 1_000L)); - underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D6", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW)); + WebhookDto webhookDto = dbWebhooks.insert(WebhookTesting.newProjectWebhook("PROJECT_1"), "PROJECT_KEY", "PROJECT_NAME"); + underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D1", webhookDto.getUuid(), "PROJECT_1", "TASK_2").setCreatedAt(NOW - 5_000L)); + underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D2", webhookDto.getUuid(), "PROJECT_1", "TASK_2").setCreatedAt(NOW - 4_000L)); + underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D3", webhookDto.getUuid(), "PROJECT_1", "TASK_2").setCreatedAt(NOW - 3_000L)); + underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D4", webhookDto.getUuid(), "PROJECT_1", "TASK_2").setCreatedAt(NOW - 2_000L)); + underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D5", webhookDto.getUuid(), "PROJECT_1", "TASK_2").setCreatedAt(NOW - 1_000L)); + underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D6", webhookDto.getUuid(), "PROJECT_1", "TASK_2").setCreatedAt(NOW)); List deliveries = underTest.selectByWebhookUuid(dbSession, webhookDto.getUuid(), 2, 2); @@ -141,13 +140,13 @@ public class WebhookDeliveryDaoIT { @Test public void selectLatestDelivery_of_a_webhook() { - WebhookDto webhook1 = dbWebhooks.insert(newProjectWebhook("COMPONENT_1"), "COMPONENT_KEY", "COMPONENT_NAME"); - underTest.insert(dbSession, WebhookDeliveryTesting.newDto("WH1-DELIVERY-1-UUID", webhook1.getUuid(), "COMPONENT_1", "TASK_1").setCreatedAt(BEFORE)); - underTest.insert(dbSession, WebhookDeliveryTesting.newDto("WH1-DELIVERY-2-UUID", webhook1.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW)); + WebhookDto webhook1 = dbWebhooks.insert(newProjectWebhook("PROJECT_1"), "PROJECT_KEY", "PROJECT_NAME"); + underTest.insert(dbSession, WebhookDeliveryTesting.newDto("WH1-DELIVERY-1-UUID", webhook1.getUuid(), "PROJECT_1", "TASK_1").setCreatedAt(BEFORE)); + underTest.insert(dbSession, WebhookDeliveryTesting.newDto("WH1-DELIVERY-2-UUID", webhook1.getUuid(), "PROJECT_1", "TASK_2").setCreatedAt(NOW)); - WebhookDto webhook2 = dbWebhooks.insert(newProjectWebhook("COMPONENT_1"), "COMPONENT_KEY", "COMPONENT_NAME"); - underTest.insert(dbSession, WebhookDeliveryTesting.newDto("WH2-DELIVERY-1-UUID", webhook2.getUuid(), "COMPONENT_1", "TASK_1").setCreatedAt(BEFORE)); - underTest.insert(dbSession, WebhookDeliveryTesting.newDto("WH2-DELIVERY-2-UUID", webhook2.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW)); + WebhookDto webhook2 = dbWebhooks.insert(newProjectWebhook("PROJECT_1"), "PROJECT_KEY", "PROJECT_NAME"); + underTest.insert(dbSession, WebhookDeliveryTesting.newDto("WH2-DELIVERY-1-UUID", webhook2.getUuid(), "PROJECT_1", "TASK_1").setCreatedAt(BEFORE)); + underTest.insert(dbSession, WebhookDeliveryTesting.newDto("WH2-DELIVERY-2-UUID", webhook2.getUuid(), "PROJECT_1", "TASK_2").setCreatedAt(NOW)); Map map = underTest.selectLatestDeliveries(dbSession, of(webhook1, webhook2)); @@ -160,7 +159,7 @@ public class WebhookDeliveryDaoIT { @Test public void insert_row_with_only_mandatory_columns() { - WebhookDeliveryDto dto = WebhookDeliveryTesting.newDto("DELIVERY_1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1") + WebhookDeliveryDto dto = WebhookDeliveryTesting.newDto("DELIVERY_1", "WEBHOOK_UUID_1", "PROJECT_1", "TASK_1") .setDurationMs(1000) .setHttpStatus(null) .setErrorStacktrace(null); @@ -179,7 +178,7 @@ public class WebhookDeliveryDaoIT { @Test public void insert_row_with_all_columns() { - WebhookDeliveryDto dto = WebhookDeliveryTesting.newDto("DELIVERY_1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1"); + WebhookDeliveryDto dto = WebhookDeliveryTesting.newDto("DELIVERY_1", "WEBHOOK_UUID_1", "PROJECT_1", "TASK_1"); underTest.insert(dbSession, dto); @@ -194,11 +193,11 @@ public class WebhookDeliveryDaoIT { @Test public void deleteByWebhook() { - WebhookDto webhookDto = dbWebhooks.insert(WebhookTesting.newProjectWebhook("COMPONENT_1"), "COMPONENT_KEY", "COMPONENT_NAME"); + WebhookDto webhookDto = dbWebhooks.insert(WebhookTesting.newProjectWebhook("PROJECT_1"), "PROJECT_KEY", "PROJECT_NAME"); - underTest.insert(dbSession, WebhookDeliveryTesting.newDto("DELIVERY_1", webhookDto.getUuid(), "COMPONENT_1", "TASK_1").setCreatedAt(1_000_000L)); - underTest.insert(dbSession, WebhookDeliveryTesting.newDto("DELIVERY_2", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(2_000_000L)); - underTest.insert(dbSession, WebhookDeliveryTesting.newDto("DELIVERY_3", "WONT BE DELETED WEBHOOK_UUID_2", "COMPONENT_2", "TASK_3").setCreatedAt(1_000_000L)); + underTest.insert(dbSession, WebhookDeliveryTesting.newDto("DELIVERY_1", webhookDto.getUuid(), "PROJECT_1", "TASK_1").setCreatedAt(1_000_000L)); + underTest.insert(dbSession, WebhookDeliveryTesting.newDto("DELIVERY_2", webhookDto.getUuid(), "PROJECT_1", "TASK_2").setCreatedAt(2_000_000L)); + underTest.insert(dbSession, WebhookDeliveryTesting.newDto("DELIVERY_3", "WONT BE DELETED WEBHOOK_UUID_2", "PROJECT_2", "TASK_3").setCreatedAt(1_000_000L)); underTest.deleteByWebhook(dbSession, webhookDto); @@ -207,12 +206,12 @@ public class WebhookDeliveryDaoIT { @Test public void deleteComponentBeforeDate_deletes_rows_before_date() { - underTest.insert(dbSession, WebhookDeliveryTesting.newDto("DELIVERY_1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1").setCreatedAt(1_000_000L)); - underTest.insert(dbSession, WebhookDeliveryTesting.newDto("DELIVERY_2", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_2").setCreatedAt(2_000_000L)); - underTest.insert(dbSession, WebhookDeliveryTesting.newDto("DELIVERY_3", "WEBHOOK_UUID_1", "COMPONENT_2", "TASK_3").setCreatedAt(1_000_000L)); + underTest.insert(dbSession, WebhookDeliveryTesting.newDto("DELIVERY_1", "WEBHOOK_UUID_1", "PROJECT_1", "TASK_1").setCreatedAt(1_000_000L)); + underTest.insert(dbSession, WebhookDeliveryTesting.newDto("DELIVERY_2", "WEBHOOK_UUID_1", "PROJECT_1", "TASK_2").setCreatedAt(2_000_000L)); + underTest.insert(dbSession, WebhookDeliveryTesting.newDto("DELIVERY_3", "WEBHOOK_UUID_1", "PROJECT_2", "TASK_3").setCreatedAt(1_000_000L)); - // should delete the old delivery on COMPONENT_1 and keep the one of COMPONENT_2 - underTest.deleteComponentBeforeDate(dbSession, "COMPONENT_1", 1_500_000L); + // should delete the old delivery on PROJECT_1 and keep the one of PROJECT_2 + underTest.deleteProjectBeforeDate(dbSession, "PROJECT_1", 1_500_000L); List> uuids = dbTester.select(dbSession, "select uuid as \"uuid\" from webhook_deliveries"); assertThat(uuids).extracting(column -> column.get("uuid")).containsOnly("DELIVERY_2", "DELIVERY_3"); @@ -220,23 +219,23 @@ public class WebhookDeliveryDaoIT { @Test public void deleteComponentBeforeDate_does_nothing_on_empty_table() { - underTest.deleteComponentBeforeDate(dbSession, "COMPONENT_1", 1_500_000L); + underTest.deleteProjectBeforeDate(dbSession, "PROJECT_1", 1_500_000L); assertThat(dbTester.countRowsOfTable(dbSession, "webhook_deliveries")).isZero(); } @Test public void deleteComponentBeforeDate_does_nothing_on_invalid_uuid() { - underTest.insert(dbSession, WebhookDeliveryTesting.newDto("DELIVERY_1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1").setCreatedAt(1_000_000L)); + underTest.insert(dbSession, WebhookDeliveryTesting.newDto("DELIVERY_1", "WEBHOOK_UUID_1", "PROJECT_1", "TASK_1").setCreatedAt(1_000_000L)); - underTest.deleteComponentBeforeDate(dbSession, "COMPONENT_2", 1_500_000L); + underTest.deleteProjectBeforeDate(dbSession, "PROJECT_2", 1_500_000L); assertThat(dbTester.countRowsOfTable(dbSession, "webhook_deliveries")).isOne(); } private void verifyMandatoryFields(WebhookDeliveryDto expected, WebhookDeliveryDto actual) { assertThat(actual.getUuid()).isEqualTo(expected.getUuid()); - assertThat(actual.getComponentUuid()).isEqualTo(expected.getComponentUuid()); + assertThat(actual.getProjectUuid()).isEqualTo(expected.getProjectUuid()); assertThat(actual.getCeTaskUuid()).isEqualTo(expected.getCeTaskUuid()); assertThat(actual.getName()).isEqualTo(expected.getName()); assertThat(actual.getUrl()).isEqualTo(expected.getUrl()); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDeliveryDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDeliveryDao.java index bba30ed1765..d71ab8370c8 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDeliveryDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDeliveryDao.java @@ -46,15 +46,15 @@ public class WebhookDeliveryDao implements Dao { return mapper(dbSession).selectByWebhookUuid(webhookUuid, new RowBounds(offset, limit)); } - public int countDeliveriesByComponentUuid(DbSession dbSession, String componentUuid) { - return mapper(dbSession).countByComponentUuid(componentUuid); + public int countDeliveriesByProjectUuid(DbSession dbSession, String projectUuid) { + return mapper(dbSession).countByProjectUuid(projectUuid); } /** - * All the deliveries for the specified component. Results are ordered by descending date. + * All the deliveries for the specified project. Results are ordered by descending date. */ - public List selectOrderedByComponentUuid(DbSession dbSession, String componentUuid, int offset, int limit) { - return mapper(dbSession).selectOrderedByComponentUuid(componentUuid, new RowBounds(offset, limit)); + public List selectOrderedByProjectUuid(DbSession dbSession, String projectUuid, int offset, int limit) { + return mapper(dbSession).selectOrderedByProjectUuid(projectUuid, new RowBounds(offset, limit)); } public int countDeliveriesByCeTaskUuid(DbSession dbSession, String ceTaskId) { @@ -72,8 +72,8 @@ public class WebhookDeliveryDao implements Dao { mapper(dbSession).insert(dto); } - public void deleteComponentBeforeDate(DbSession dbSession, String componentUuid, long beforeDate) { - mapper(dbSession).deleteComponentBeforeDate(componentUuid, beforeDate); + public void deleteProjectBeforeDate(DbSession dbSession, String projectUuid, long beforeDate) { + mapper(dbSession).deleteProjectBeforeDate(projectUuid, beforeDate); } public Map selectLatestDeliveries(DbSession dbSession, List webhooks) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDeliveryDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDeliveryDto.java index babab6315cd..0f212cfa83c 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDeliveryDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDeliveryDto.java @@ -52,7 +52,7 @@ public class WebhookDeliveryDto extends WebhookDeliveryLiteDto { protected String uuid; /** Technical unique identifier, can be null for migration */ protected String webhookUuid; - /** Component UUID, can't be null */ - protected String componentUuid; + /** Project UUID, can't be null */ + protected String projectUuid; /** Compute Engine task UUID, can be null */ protected String ceTaskUuid; /** analysis UUID, can be null */ @@ -64,12 +64,12 @@ public class WebhookDeliveryLiteDto { return (T) this; } - public String getComponentUuid() { - return componentUuid; + public String getProjectUuid() { + return projectUuid; } - public T setComponentUuid(String s) { - this.componentUuid = s; + public T setProjectUuid(String s) { + this.projectUuid = s; return (T) this; } @@ -153,7 +153,7 @@ public class WebhookDeliveryLiteDto { public String toString() { return new ToStringBuilder(this) .append("uuid", uuid) - .append("componentUuid", componentUuid) + .append("componentUuid", projectUuid) .append("ceTaskUuid", ceTaskUuid) .append("name", name) .append("success", success) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDeliveryMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDeliveryMapper.java index a19ef41fde0..66f4dead983 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDeliveryMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDeliveryMapper.java @@ -33,9 +33,9 @@ public interface WebhookDeliveryMapper { List selectByWebhookUuid(@Param("webhookUuid") String webhookUuid, RowBounds rowBounds); - int countByComponentUuid(@Param("componentUuid") String componentUuid); + int countByProjectUuid(@Param("projectUuid") String projectUuid); - List selectOrderedByComponentUuid(@Param("componentUuid") String componentUuid, RowBounds rowBounds); + List selectOrderedByProjectUuid(@Param("projectUuid") String projectUuid, RowBounds rowBounds); int countByCeTaskUuid(@Param("ceTaskUuid") String ceTaskId); @@ -43,7 +43,7 @@ public interface WebhookDeliveryMapper { void insert(WebhookDeliveryDto dto); - void deleteComponentBeforeDate(@Param("componentUuid") String componentUuid, @Param("beforeDate") long beforeDate); + void deleteProjectBeforeDate(@Param("projectUuid") String projectUuid, @Param("beforeDate") long beforeDate); void deleteByWebhookUuid(@Param("webhookUuid") String webhookUuid); } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml index 03a38b3e3f5..c4c8bd4b96f 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml @@ -570,7 +570,7 @@ - delete from webhook_deliveries where component_uuid=#{projectUuid,jdbcType=VARCHAR} + delete from webhook_deliveries where project_uuid=#{projectUuid,jdbcType=VARCHAR} diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/webhook/WebhookDeliveryMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/webhook/WebhookDeliveryMapper.xml index 94af29d28f3..cbb17b6a747 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/webhook/WebhookDeliveryMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/webhook/WebhookDeliveryMapper.xml @@ -6,7 +6,7 @@ uuid, - component_uuid as componentUuid, + project_uuid as projectUuid, webhook_uuid as webhookUuid, ce_task_uuid as ceTaskUuid, name, @@ -40,17 +40,17 @@ order by created_at desc - select count(1) from webhook_deliveries - where component_uuid = #{componentUuid,jdbcType=VARCHAR} + where project_uuid = #{projectUuid,jdbcType=VARCHAR} - select from webhook_deliveries - where component_uuid = #{componentUuid,jdbcType=VARCHAR} + where project_uuid = #{projectUuid,jdbcType=VARCHAR} order by created_at desc @@ -72,7 +72,7 @@ insert into webhook_deliveries ( uuid, webhook_uuid, - component_uuid, + project_uuid, ce_task_uuid, analysis_uuid, name, @@ -86,7 +86,7 @@ ) values ( #{uuid,jdbcType=VARCHAR}, #{webhookUuid,jdbcType=VARCHAR}, - #{componentUuid,jdbcType=VARCHAR}, + #{projectUuid,jdbcType=VARCHAR}, #{ceTaskUuid,jdbcType=VARCHAR}, #{analysisUuid,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, @@ -105,10 +105,10 @@ where webhook_uuid = #{webhookUuid,jdbcType=VARCHAR} - + delete from webhook_deliveries where - component_uuid = #{componentUuid,jdbcType=VARCHAR} and + project_uuid = #{projectUuid,jdbcType=VARCHAR} and created_at < #{beforeDate,jdbcType=BIGINT} diff --git a/server/sonar-db-dao/src/schema/schema-sq.ddl b/server/sonar-db-dao/src/schema/schema-sq.ddl index e66274ae90c..7188c4d8c2e 100644 --- a/server/sonar-db-dao/src/schema/schema-sq.ddl +++ b/server/sonar-db-dao/src/schema/schema-sq.ddl @@ -1047,7 +1047,7 @@ CREATE INDEX "USERS_EMAIL" ON "USERS"("EMAIL" NULLS FIRST); CREATE TABLE "WEBHOOK_DELIVERIES"( "UUID" CHARACTER VARYING(40) NOT NULL, "WEBHOOK_UUID" CHARACTER VARYING(40) NOT NULL, - "COMPONENT_UUID" CHARACTER VARYING(40) NOT NULL, + "PROJECT_UUID" CHARACTER VARYING(40) NOT NULL, "CE_TASK_UUID" CHARACTER VARYING(40), "ANALYSIS_UUID" CHARACTER VARYING(40), "NAME" CHARACTER VARYING(100) NOT NULL, @@ -1060,9 +1060,9 @@ CREATE TABLE "WEBHOOK_DELIVERIES"( "CREATED_AT" BIGINT NOT NULL ); ALTER TABLE "WEBHOOK_DELIVERIES" ADD CONSTRAINT "PK_WEBHOOK_DELIVERIES" PRIMARY KEY("UUID"); -CREATE INDEX "COMPONENT_UUID" ON "WEBHOOK_DELIVERIES"("COMPONENT_UUID" NULLS FIRST); CREATE INDEX "CE_TASK_UUID" ON "WEBHOOK_DELIVERIES"("CE_TASK_UUID" NULLS FIRST); CREATE INDEX "IDX_WBHK_DLVRS_WBHK_UUID" ON "WEBHOOK_DELIVERIES"("WEBHOOK_UUID" NULLS FIRST); +CREATE INDEX "WD_PROJECT_UUID" ON "WEBHOOK_DELIVERIES"("PROJECT_UUID" NULLS FIRST); CREATE TABLE "WEBHOOKS"( "UUID" CHARACTER VARYING(40) NOT NULL, diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/webhook/WebhookDeliveryDbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/webhook/WebhookDeliveryDbTester.java index 036872b5be8..59d8de1651f 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/webhook/WebhookDeliveryDbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/webhook/WebhookDeliveryDbTester.java @@ -54,7 +54,7 @@ public class WebhookDeliveryDbTester { WebhookDeliveryDto dto = newDto(); stream(dtoPopulators).forEach(dtoPopulator -> dtoPopulator.accept(dto)); String projectUuid = webhook.getProjectUuid(); - dto.setComponentUuid(Objects.requireNonNull(projectUuid, "Project uuid of webhook cannot be null")); + dto.setProjectUuid(Objects.requireNonNull(projectUuid, "Project uuid of webhook cannot be null")); dto.setWebhookUuid(webhook.getUuid()); dbTester.getDbClient().webhookDeliveryDao().insert(dbTester.getSession(), dto); dbTester.getSession().commit(); diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/webhook/WebhookDeliveryTesting.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/webhook/WebhookDeliveryTesting.java index ae149ffefc9..3df5dae9936 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/webhook/WebhookDeliveryTesting.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/webhook/WebhookDeliveryTesting.java @@ -39,11 +39,11 @@ public class WebhookDeliveryTesting { * Build a {@link WebhookDeliveryDto} with all mandatory fields. * Optional fields are kept null. */ - public static WebhookDeliveryDto newDto(String uuid, String webhookUuid, String componentUuid, String ceTaskUuid) { + public static WebhookDeliveryDto newDto(String uuid, String webhookUuid, String projectUuid, String ceTaskUuid) { return newDto() .setUuid(uuid) .setWebhookUuid(webhookUuid) - .setComponentUuid(componentUuid) + .setProjectUuid(projectUuid) .setCeTaskUuid(ceTaskUuid); } @@ -51,7 +51,7 @@ public class WebhookDeliveryTesting { return new WebhookDeliveryDto() .setUuid(Uuids.createFast()) .setWebhookUuid(randomAlphanumeric(40)) - .setComponentUuid(randomAlphanumeric(40)) + .setProjectUuid(randomAlphanumeric(40)) .setCeTaskUuid(randomAlphanumeric(40)) .setAnalysisUuid(randomAlphanumeric(40)) .setName(randomAlphanumeric(10)) diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexProjectUuidInWebhookDeliveries.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexProjectUuidInWebhookDeliveries.java new file mode 100644 index 00000000000..cadc8fa0869 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexProjectUuidInWebhookDeliveries.java @@ -0,0 +1,41 @@ +/* + * 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.v102; + +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.CreateIndexOnColumn; + +public class CreateIndexProjectUuidInWebhookDeliveries extends CreateIndexOnColumn { + + private static final String TABLE_NAME = "webhook_deliveries"; + private static final String COLUMN_NAME = "project_uuid"; + + public CreateIndexProjectUuidInWebhookDeliveries(Database db) { + super(db, TABLE_NAME, COLUMN_NAME, false); + } + + /** + * There is a limit of 30 characters for index name, that's why this one has a different name + */ + @Override + public String newIndexName() { + return "wd_" + COLUMN_NAME; + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/DbVersion102.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/DbVersion102.java index 55331e975c1..95d4e5aab62 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/DbVersion102.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/DbVersion102.java @@ -62,6 +62,12 @@ public class DbVersion102 implements DbVersion { .add(10_2_013, "Drop index on 'components.main_branch_project_uuid", DropIndexOnMainBranchProjectUuid.class) .add(10_2_014, "Drop column 'main_branch_project_uuid' in the components table", DropMainBranchProjectUuidInComponents.class) + + .add(10_2_015, "Drop index 'component_uuid' in 'webhook_deliveries' table", DropIndexComponentUuidInWebhookDeliveries.class) + .add(10_2_016, "Rename 'component_uuid' in 'webhook_deliveries' table to 'project_uuid'", RenameComponentUuidInWebhookDeliveries.class) + .add(10_2_017, "Create index 'webhook_deliveries_project_uuid' in 'webhook_deliveries' table", CreateIndexProjectUuidInWebhookDeliveries.class) + ; } + } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/DropIndexComponentUuidInWebhookDeliveries.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/DropIndexComponentUuidInWebhookDeliveries.java new file mode 100644 index 00000000000..dbbeedc2a7a --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/DropIndexComponentUuidInWebhookDeliveries.java @@ -0,0 +1,33 @@ +/* + * 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.v102; + +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.DropIndexChange; + +public class DropIndexComponentUuidInWebhookDeliveries extends DropIndexChange { + + private static final String TABLE_NAME = "webhook_deliveries"; + private static final String INDEX_NAME = "component_uuid"; + + public DropIndexComponentUuidInWebhookDeliveries(Database db) { + super(db, INDEX_NAME, TABLE_NAME); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/RenameComponentUuidInWebhookDeliveries.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/RenameComponentUuidInWebhookDeliveries.java new file mode 100644 index 00000000000..79c9d18e8f6 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/RenameComponentUuidInWebhookDeliveries.java @@ -0,0 +1,34 @@ +/* + * 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.v102; + +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.RenameVarcharColumnChange; + +public class RenameComponentUuidInWebhookDeliveries extends RenameVarcharColumnChange { + + private static final String TABLE_NAME = "webhook_deliveries"; + private static final String OLD_COLUMN_NAME = "component_uuid"; + private static final String NEW_COLUMN_NAME = "project_uuid"; + + public RenameComponentUuidInWebhookDeliveries(Database db) { + super(db, TABLE_NAME, OLD_COLUMN_NAME, NEW_COLUMN_NAME); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/RenameVarcharColumnAbstractTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/RenameVarcharColumnAbstractTest.java new file mode 100644 index 00000000000..24caeafd685 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/RenameVarcharColumnAbstractTest.java @@ -0,0 +1,58 @@ +/* + * 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; + +import java.sql.SQLException; +import org.junit.Rule; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.RenameVarcharColumnChange; + +import static java.sql.Types.VARCHAR; + +public abstract class RenameVarcharColumnAbstractTest { + + @Rule + public final CoreDbTester db = CoreDbTester.createForSchema(getClass(), "schema.sql"); + private final String tableName; + private final String columnName; + private final boolean isNullable; + + public RenameVarcharColumnAbstractTest(String tableName, String columnName, boolean isNullable) { + this.tableName = tableName; + this.columnName = columnName; + this.isNullable = isNullable; + } + + protected void verifyMigrationIsReentrant() throws SQLException { + db.assertColumnDoesNotExist(tableName, columnName); + getClassUnderTest().execute(); + getClassUnderTest().execute(); + db.assertColumnDefinition(tableName, columnName, VARCHAR, 40, isNullable); + } + + protected void verifyColumnIsRenamed() throws SQLException { + db.assertColumnDoesNotExist(tableName, columnName); + getClassUnderTest().execute(); + db.assertColumnDefinition(tableName, columnName, VARCHAR, 40, isNullable); + } + + protected abstract RenameVarcharColumnChange getClassUnderTest(); + +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexProjectUuidInWebhookDeliveriesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexProjectUuidInWebhookDeliveriesTest.java new file mode 100644 index 00000000000..51f36d315c4 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateIndexProjectUuidInWebhookDeliveriesTest.java @@ -0,0 +1,49 @@ +/* + * 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.v102; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; + +public class CreateIndexProjectUuidInWebhookDeliveriesTest { + @Rule + public final CoreDbTester db = CoreDbTester.createForSchema(CreateIndexProjectUuidInWebhookDeliveriesTest.class, "schema.sql"); + + private final CreateIndexProjectUuidInWebhookDeliveries createIndex = new CreateIndexProjectUuidInWebhookDeliveries(db.database()); + + @Test + public void migration_should_create_index() throws SQLException { + db.assertIndexDoesNotExist("webhook_deliveries", "wd_project_uuid"); + + createIndex.execute(); + + db.assertIndex("webhook_deliveries", "wd_project_uuid", "project_uuid"); + } + + @Test + public void migration_should_be_reentrant() throws SQLException { + createIndex.execute(); + createIndex.execute(); + + db.assertIndex("webhook_deliveries", "wd_project_uuid", "project_uuid"); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/DropIndexComponentUuidInWebhookDeliveriesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/DropIndexComponentUuidInWebhookDeliveriesTest.java new file mode 100644 index 00000000000..2d1428bbbe4 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/DropIndexComponentUuidInWebhookDeliveriesTest.java @@ -0,0 +1,56 @@ +/* + * 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.v102; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; + +public class DropIndexComponentUuidInWebhookDeliveriesTest { + + private static final String TABLE_NAME = "webhook_deliveries"; + private static final String COLUMN_NAME = "component_uuid"; + private static final String INDEX_NAME = "component_uuid"; + + @Rule + public final CoreDbTester db = CoreDbTester.createForSchema(DropIndexComponentUuidInWebhookDeliveriesTest.class, "schema.sql"); + + private final DropIndexComponentUuidInWebhookDeliveries underTest = new DropIndexComponentUuidInWebhookDeliveries(db.database()); + + @Test + public void index_is_dropped() throws SQLException { + db.assertIndex(TABLE_NAME, INDEX_NAME, COLUMN_NAME); + + underTest.execute(); + + db.assertIndexDoesNotExist(TABLE_NAME, COLUMN_NAME); + } + + @Test + public void migration_is_reentrant() throws SQLException { + db.assertIndex(TABLE_NAME, INDEX_NAME, COLUMN_NAME); + + underTest.execute(); + underTest.execute(); + + db.assertIndexDoesNotExist(TABLE_NAME, COLUMN_NAME); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/RenameComponentUuidInWebhookDeliveriesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/RenameComponentUuidInWebhookDeliveriesTest.java new file mode 100644 index 00000000000..d3c205dd8eb --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/RenameComponentUuidInWebhookDeliveriesTest.java @@ -0,0 +1,48 @@ +/* + * 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.v102; + +import java.sql.SQLException; +import org.junit.Test; +import org.sonar.server.platform.db.migration.step.RenameVarcharColumnChange; +import org.sonar.server.platform.db.migration.version.RenameVarcharColumnAbstractTest; + +public class RenameComponentUuidInWebhookDeliveriesTest extends RenameVarcharColumnAbstractTest { + + public RenameComponentUuidInWebhookDeliveriesTest() { + super("webhook_deliveries", "project_uuid", false); + } + + @Test + public void migration_is_reentrant() throws SQLException { + super.verifyMigrationIsReentrant(); + } + + @Test + public void column_is_renamed() throws SQLException { + super.verifyColumnIsRenamed(); + } + + @Override + protected RenameVarcharColumnChange getClassUnderTest() { + return new RenameComponentUuidInWebhookDeliveries(db.database()); + } + +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/CreateIndexProjectUuidInWebhookDeliveriesTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/CreateIndexProjectUuidInWebhookDeliveriesTest/schema.sql new file mode 100644 index 00000000000..add1899f5cf --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/CreateIndexProjectUuidInWebhookDeliveriesTest/schema.sql @@ -0,0 +1,18 @@ +CREATE TABLE "WEBHOOK_DELIVERIES"( + "UUID" CHARACTER VARYING(40) NOT NULL, + "WEBHOOK_UUID" CHARACTER VARYING(40) NOT NULL, + "PROJECT_UUID" CHARACTER VARYING(40) NOT NULL, + "CE_TASK_UUID" CHARACTER VARYING(40), + "ANALYSIS_UUID" CHARACTER VARYING(40), + "NAME" CHARACTER VARYING(100) NOT NULL, + "URL" CHARACTER VARYING(2000) NOT NULL, + "SUCCESS" BOOLEAN NOT NULL, + "HTTP_STATUS" INTEGER, + "DURATION_MS" BIGINT NOT NULL, + "PAYLOAD" CHARACTER LARGE OBJECT NOT NULL, + "ERROR_STACKTRACE" CHARACTER LARGE OBJECT, + "CREATED_AT" BIGINT NOT NULL +); +ALTER TABLE "WEBHOOK_DELIVERIES" ADD CONSTRAINT "PK_WEBHOOK_DELIVERIES" PRIMARY KEY("UUID"); +CREATE INDEX "CE_TASK_UUID" ON "WEBHOOK_DELIVERIES"("CE_TASK_UUID" NULLS FIRST); +CREATE INDEX "IDX_WBHK_DLVRS_WBHK_UUID" ON "WEBHOOK_DELIVERIES"("WEBHOOK_UUID" NULLS FIRST); \ No newline at end of file diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/DropIndexComponentUuidInWebhookDeliveriesTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/DropIndexComponentUuidInWebhookDeliveriesTest/schema.sql new file mode 100644 index 00000000000..b6b5b09f142 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/DropIndexComponentUuidInWebhookDeliveriesTest/schema.sql @@ -0,0 +1,19 @@ +CREATE TABLE "WEBHOOK_DELIVERIES"( + "UUID" CHARACTER VARYING(40) NOT NULL, + "WEBHOOK_UUID" CHARACTER VARYING(40) NOT NULL, + "COMPONENT_UUID" CHARACTER VARYING(40) NOT NULL, + "CE_TASK_UUID" CHARACTER VARYING(40), + "ANALYSIS_UUID" CHARACTER VARYING(40), + "NAME" CHARACTER VARYING(100) NOT NULL, + "URL" CHARACTER VARYING(2000) NOT NULL, + "SUCCESS" BOOLEAN NOT NULL, + "HTTP_STATUS" INTEGER, + "DURATION_MS" BIGINT NOT NULL, + "PAYLOAD" CHARACTER LARGE OBJECT NOT NULL, + "ERROR_STACKTRACE" CHARACTER LARGE OBJECT, + "CREATED_AT" BIGINT NOT NULL +); +ALTER TABLE "WEBHOOK_DELIVERIES" ADD CONSTRAINT "PK_WEBHOOK_DELIVERIES" PRIMARY KEY("UUID"); +CREATE INDEX "COMPONENT_UUID" ON "WEBHOOK_DELIVERIES"("COMPONENT_UUID" NULLS FIRST); +CREATE INDEX "CE_TASK_UUID" ON "WEBHOOK_DELIVERIES"("CE_TASK_UUID" NULLS FIRST); +CREATE INDEX "IDX_WBHK_DLVRS_WBHK_UUID" ON "WEBHOOK_DELIVERIES"("WEBHOOK_UUID" NULLS FIRST); \ No newline at end of file diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/RenameComponentUuidInWebhookDeliveriesTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/RenameComponentUuidInWebhookDeliveriesTest/schema.sql new file mode 100644 index 00000000000..b6b5b09f142 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/RenameComponentUuidInWebhookDeliveriesTest/schema.sql @@ -0,0 +1,19 @@ +CREATE TABLE "WEBHOOK_DELIVERIES"( + "UUID" CHARACTER VARYING(40) NOT NULL, + "WEBHOOK_UUID" CHARACTER VARYING(40) NOT NULL, + "COMPONENT_UUID" CHARACTER VARYING(40) NOT NULL, + "CE_TASK_UUID" CHARACTER VARYING(40), + "ANALYSIS_UUID" CHARACTER VARYING(40), + "NAME" CHARACTER VARYING(100) NOT NULL, + "URL" CHARACTER VARYING(2000) NOT NULL, + "SUCCESS" BOOLEAN NOT NULL, + "HTTP_STATUS" INTEGER, + "DURATION_MS" BIGINT NOT NULL, + "PAYLOAD" CHARACTER LARGE OBJECT NOT NULL, + "ERROR_STACKTRACE" CHARACTER LARGE OBJECT, + "CREATED_AT" BIGINT NOT NULL +); +ALTER TABLE "WEBHOOK_DELIVERIES" ADD CONSTRAINT "PK_WEBHOOK_DELIVERIES" PRIMARY KEY("UUID"); +CREATE INDEX "COMPONENT_UUID" ON "WEBHOOK_DELIVERIES"("COMPONENT_UUID" NULLS FIRST); +CREATE INDEX "CE_TASK_UUID" ON "WEBHOOK_DELIVERIES"("CE_TASK_UUID" NULLS FIRST); +CREATE INDEX "IDX_WBHK_DLVRS_WBHK_UUID" ON "WEBHOOK_DELIVERIES"("WEBHOOK_UUID" NULLS FIRST); \ No newline at end of file diff --git a/server/sonar-server-common/src/it/java/org/sonar/server/webhook/WebhookDeliveryStorageIT.java b/server/sonar-server-common/src/it/java/org/sonar/server/webhook/WebhookDeliveryStorageIT.java index 6805bfeafc1..e7bde6cbf02 100644 --- a/server/sonar-server-common/src/it/java/org/sonar/server/webhook/WebhookDeliveryStorageIT.java +++ b/server/sonar-server-common/src/it/java/org/sonar/server/webhook/WebhookDeliveryStorageIT.java @@ -64,7 +64,7 @@ public class WebhookDeliveryStorageIT { WebhookDeliveryDto dto = dbClient.webhookDeliveryDao().selectByUuid(dbSession, DELIVERY_UUID).get(); assertThat(dto.getUuid()).isEqualTo(DELIVERY_UUID); assertThat(dto.getWebhookUuid()).isEqualTo("WEBHOOK_UUID_1"); - assertThat(dto.getComponentUuid()).isEqualTo(delivery.getWebhook().getComponentUuid()); + assertThat(dto.getProjectUuid()).isEqualTo(delivery.getWebhook().getProjectUuid()); assertThat(dto.getCeTaskUuid()).isEqualTo(delivery.getWebhook().getCeTaskUuid().get()); assertThat(dto.getName()).isEqualTo(delivery.getWebhook().getName()); assertThat(dto.getUrl()).isEqualTo(delivery.getWebhook().getUrl()); @@ -128,7 +128,7 @@ public class WebhookDeliveryStorageIT { private static WebhookDeliveryDto newDto(String uuid, String componentUuid, long at) { return WebhookDeliveryTesting.newDto() .setUuid(uuid) - .setComponentUuid(componentUuid) + .setProjectUuid(componentUuid) .setCreatedAt(at); } } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/webhook/Webhook.java b/server/sonar-server-common/src/main/java/org/sonar/server/webhook/Webhook.java index 10d3279643d..472859f2a14 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/webhook/Webhook.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/webhook/Webhook.java @@ -30,7 +30,7 @@ import static java.util.Optional.ofNullable; public class Webhook { private final String uuid; - private final String componentUuid; + private final String projectUuid; @Nullable private final String ceTaskUuid; @Nullable @@ -40,10 +40,10 @@ public class Webhook { @Nullable private final String secret; - public Webhook(String uuid, String componentUuid, @Nullable String ceTaskUuid, + public Webhook(String uuid, String projectUuid, @Nullable String ceTaskUuid, @Nullable String analysisUuid, String name, String url, @Nullable String secret) { this.uuid = uuid; - this.componentUuid = requireNonNull(componentUuid); + this.projectUuid = requireNonNull(projectUuid); this.ceTaskUuid = ceTaskUuid; this.analysisUuid = analysisUuid; this.name = requireNonNull(name); @@ -51,8 +51,8 @@ public class Webhook { this.secret = secret; } - public String getComponentUuid() { - return componentUuid; + public String getProjectUuid() { + return projectUuid; } public Optional getCeTaskUuid() { diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/webhook/WebhookDeliveryStorage.java b/server/sonar-server-common/src/main/java/org/sonar/server/webhook/WebhookDeliveryStorage.java index a2c7ade3721..4a0182e84e8 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/webhook/WebhookDeliveryStorage.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/webhook/WebhookDeliveryStorage.java @@ -56,10 +56,10 @@ public class WebhookDeliveryStorage { } } - public void purge(String componentUuid) { + public void purge(String projectUuid) { long beforeDate = system.now() - ALIVE_DELAY_MS; try (DbSession dbSession = dbClient.openSession(false)) { - dbClient.webhookDeliveryDao().deleteComponentBeforeDate(dbSession, componentUuid, beforeDate); + dbClient.webhookDeliveryDao().deleteProjectBeforeDate(dbSession, projectUuid, beforeDate); dbSession.commit(); } } @@ -68,7 +68,7 @@ public class WebhookDeliveryStorage { WebhookDeliveryDto dto = new WebhookDeliveryDto(); dto.setUuid(uuidFactory.create()); dto.setWebhookUuid(delivery.getWebhook().getUuid()); - dto.setComponentUuid(delivery.getWebhook().getComponentUuid()); + dto.setProjectUuid(delivery.getWebhook().getProjectUuid()); delivery.getWebhook().getCeTaskUuid().ifPresent(dto::setCeTaskUuid); delivery.getWebhook().getAnalysisUuid().ifPresent(dto::setAnalysisUuid); dto.setName(delivery.getWebhook().getName()); diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/webhook/WebhookTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/webhook/WebhookTest.java index 189cce58d93..e8f5fd9fb76 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/webhook/WebhookTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/webhook/WebhookTest.java @@ -27,10 +27,10 @@ public class WebhookTest { @Test public void webhook_with_only_required_fields() { - Webhook underTest = new Webhook("a_uuid", "a_component_uuid", null, null, "a_name", "an_url", null); + Webhook underTest = new Webhook("a_uuid", "a_project_uuid", null, null, "a_name", "an_url", null); assertThat(underTest.getUuid()).isEqualTo("a_uuid"); - assertThat(underTest.getComponentUuid()).isEqualTo("a_component_uuid"); + assertThat(underTest.getProjectUuid()).isEqualTo("a_project_uuid"); assertThat(underTest.getCeTaskUuid()).isEmpty(); assertThat(underTest.getAnalysisUuid()).isEmpty(); assertThat(underTest.getName()).isEqualTo("a_name"); @@ -43,7 +43,7 @@ public class WebhookTest { Webhook underTest = new Webhook("a_uuid", "a_component_uuid", "a_task_uuid", "an_analysis", "a_name", "an_url", "a_secret"); assertThat(underTest.getUuid()).isEqualTo("a_uuid"); - assertThat(underTest.getComponentUuid()).isEqualTo("a_component_uuid"); + assertThat(underTest.getProjectUuid()).isEqualTo("a_component_uuid"); assertThat(underTest.getCeTaskUuid()).hasValue("a_task_uuid"); assertThat(underTest.getAnalysisUuid()).hasValue("an_analysis"); assertThat(underTest.getName()).isEqualTo("a_name"); diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/webhook/ws/CreateActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/webhook/ws/CreateActionIT.java index 10bca756e84..c66e27ec981 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/webhook/ws/CreateActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/webhook/ws/CreateActionIT.java @@ -62,7 +62,7 @@ public class CreateActionIT { public UserSessionRule userSession = standalone(); @Rule - public DbTester db = create(); + public DbTester db = create(true); private final DbClient dbClient = db.getDbClient(); private final WebhookDbTester webhookDbTester = db.webhooks(); private final ComponentDbTester componentDbTester = db.components(); @@ -96,7 +96,7 @@ public class CreateActionIT { @Test public void create_a_webhook_with_400_length_project_key() { String longProjectKey = generateStringWithLength(400); - ComponentDto project = componentDbTester.insertPrivateProject(componentDto -> componentDto.setKey(longProjectKey)).getMainBranchComponent(); + ProjectDto project = componentDbTester.insertPrivateProject(componentDto -> componentDto.setKey(longProjectKey)).getProjectDto(); userSession.logIn().addProjectPermission(UserRole.ADMIN, project); @@ -158,7 +158,7 @@ public class CreateActionIT { @Test public void create_a_webhook_on_project() { - ComponentDto project = componentDbTester.insertPrivateProject().getMainBranchComponent(); + ProjectDto project = componentDbTester.insertPrivateProject().getProjectDto(); userSession.logIn().addProjectPermission(UserRole.ADMIN, project); diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/webhook/ws/DeleteActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/webhook/ws/DeleteActionIT.java index 48de6658adb..0f0172c6575 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/webhook/ws/DeleteActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/webhook/ws/DeleteActionIT.java @@ -59,7 +59,7 @@ public class DeleteActionIT { public UserSessionRule userSession = standalone(); @Rule - public DbTester db = create(); + public DbTester db = create(true); private final DbClient dbClient = db.getDbClient(); private final DbSession dbSession = db.getSession(); private final WebhookDbTester webhookDbTester = db.webhooks(); diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/webhook/ws/ListActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/webhook/ws/ListActionIT.java index 1d00b408567..637538187eb 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/webhook/ws/ListActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/webhook/ws/ListActionIT.java @@ -66,7 +66,7 @@ public class ListActionIT { public UserSessionRule userSession = standalone(); @Rule - public DbTester db = create(); + public DbTester db = create(true); private final DbClient dbClient = db.getDbClient(); private final Configuration configuration = mock(Configuration.class); diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/webhook/ws/UpdateActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/webhook/ws/UpdateActionIT.java index ef84925ecda..49c7176841c 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/webhook/ws/UpdateActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/webhook/ws/UpdateActionIT.java @@ -58,7 +58,7 @@ public class UpdateActionIT { public UserSessionRule userSession = standalone(); @Rule - public DbTester db = create(); + public DbTester db = create(true); private final DbClient dbClient = db.getDbClient(); private final WebhookDbTester webhookDbTester = db.webhooks(); private final ComponentDbTester componentDbTester = db.components(); diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/webhook/ws/WebhookDeliveriesActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/webhook/ws/WebhookDeliveriesActionIT.java index 0086cec2480..aeb4c395d5e 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/webhook/ws/WebhookDeliveriesActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/webhook/ws/WebhookDeliveriesActionIT.java @@ -27,7 +27,7 @@ import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; import org.sonar.db.DbClient; import org.sonar.db.DbTester; -import org.sonar.db.component.ComponentDto; +import org.sonar.db.project.ProjectDto; import org.sonar.db.webhook.WebhookDeliveryDbTester; import org.sonar.db.webhook.WebhookDeliveryDto; import org.sonar.server.component.ComponentFinder; @@ -51,22 +51,22 @@ public class WebhookDeliveriesActionIT { public UserSessionRule userSession = UserSessionRule.standalone(); @Rule - public DbTester db = DbTester.create(System2.INSTANCE); + public DbTester db = DbTester.create(System2.INSTANCE, true); private DbClient dbClient = db.getDbClient(); private WebhookDeliveryDbTester webhookDeliveryDbTester = db.webhookDelivery(); private WsActionTester ws; - private ComponentDto project; - private ComponentDto otherProject; + private ProjectDto project; + private ProjectDto otherProject; @Before public void setUp() { ComponentFinder componentFinder = TestComponentFinder.from(db); WebhookDeliveriesAction underTest = new WebhookDeliveriesAction(dbClient, userSession, componentFinder); ws = new WsActionTester(underTest); - project = db.components().insertPrivateProject(c -> c.setKey("my-project")).getMainBranchComponent(); - otherProject = db.components().insertPrivateProject(c -> c.setKey("other-project")).getMainBranchComponent(); + project = db.components().insertPrivateProject(c -> c.setKey("my-project")).getProjectDto(); + otherProject = db.components().insertPrivateProject(c -> c.setKey("other-project")).getProjectDto(); } @Test @@ -121,7 +121,7 @@ public class WebhookDeliveriesActionIT { public void search_by_component_and_return_records_of_example() { WebhookDeliveryDto dto = newDto() .setUuid("d1") - .setComponentUuid(project.uuid()) + .setProjectUuid(project.getUuid()) .setCeTaskUuid("task-1") .setName("Jenkins") .setUrl("http://jenkins") @@ -143,9 +143,9 @@ public class WebhookDeliveriesActionIT { @Test public void search_by_task_and_return_records() { - WebhookDeliveryDto dto1 = newDto().setComponentUuid(project.uuid()).setCeTaskUuid("t1"); - WebhookDeliveryDto dto2 = newDto().setComponentUuid(project.uuid()).setCeTaskUuid("t1"); - WebhookDeliveryDto dto3 = newDto().setComponentUuid(project.uuid()).setCeTaskUuid("t2"); + WebhookDeliveryDto dto1 = newDto().setProjectUuid(project.getUuid()).setCeTaskUuid("t1"); + WebhookDeliveryDto dto2 = newDto().setProjectUuid(project.getUuid()).setCeTaskUuid("t1"); + WebhookDeliveryDto dto3 = newDto().setProjectUuid(project.getUuid()).setCeTaskUuid("t2"); dbClient.webhookDeliveryDao().insert(db.getSession(), dto1); dbClient.webhookDeliveryDao().insert(db.getSession(), dto2); dbClient.webhookDeliveryDao().insert(db.getSession(), dto3); @@ -161,12 +161,12 @@ public class WebhookDeliveriesActionIT { @Test public void search_by_webhook_and_return_records() { - WebhookDeliveryDto dto1 = newDto().setComponentUuid(project.uuid()).setCeTaskUuid("t1").setWebhookUuid("wh-1-uuid"); - WebhookDeliveryDto dto2 = newDto().setComponentUuid(project.uuid()).setCeTaskUuid("t1").setWebhookUuid("wh-1-uuid"); - WebhookDeliveryDto dto3 = newDto().setComponentUuid(project.uuid()).setCeTaskUuid("t2").setWebhookUuid("wh-2-uuid"); + WebhookDeliveryDto dto1 = newDto().setProjectUuid(project.getUuid()).setCeTaskUuid("t1").setWebhookUuid("wh-1-uuid"); + WebhookDeliveryDto dto2 = newDto().setProjectUuid(project.getUuid()).setCeTaskUuid("t1").setWebhookUuid("wh-1-uuid"); + WebhookDeliveryDto dto3 = newDto().setProjectUuid(project.getUuid()).setCeTaskUuid("t2").setWebhookUuid("wh-2-uuid"); - WebhookDeliveryDto dto4 = newDto().setComponentUuid(otherProject.uuid()).setCeTaskUuid("t4").setWebhookUuid("wh-1-uuid"); - WebhookDeliveryDto dto5 = newDto().setComponentUuid(otherProject.uuid()).setCeTaskUuid("t5").setWebhookUuid("wh-1-uuid"); + WebhookDeliveryDto dto4 = newDto().setProjectUuid(otherProject.getUuid()).setCeTaskUuid("t4").setWebhookUuid("wh-1-uuid"); + WebhookDeliveryDto dto5 = newDto().setProjectUuid(otherProject.getUuid()).setCeTaskUuid("t5").setWebhookUuid("wh-1-uuid"); dbClient.webhookDeliveryDao().insert(db.getSession(), dto1); dbClient.webhookDeliveryDao().insert(db.getSession(), dto2); @@ -194,7 +194,7 @@ public class WebhookDeliveriesActionIT { public void validate_default_pagination() { for (int i = 0; i < 15; i++) { - webhookDeliveryDbTester.insert(newDto().setComponentUuid(project.uuid()).setCeTaskUuid("t1").setWebhookUuid("wh-1-uuid")); + webhookDeliveryDbTester.insert(newDto().setProjectUuid(project.getUuid()).setCeTaskUuid("t1").setWebhookUuid("wh-1-uuid")); } userSession.logIn().addProjectPermission(UserRole.ADMIN, project); @@ -211,7 +211,7 @@ public class WebhookDeliveriesActionIT { public void validate_pagination_first_page() { for (int i = 0; i < 12; i++) { - webhookDeliveryDbTester.insert(newDto().setComponentUuid(project.uuid()).setCeTaskUuid("t1").setWebhookUuid("wh-1-uuid")); + webhookDeliveryDbTester.insert(newDto().setProjectUuid(project.getUuid()).setCeTaskUuid("t1").setWebhookUuid("wh-1-uuid")); } userSession.logIn().addProjectPermission(UserRole.ADMIN, project); @@ -231,7 +231,7 @@ public class WebhookDeliveriesActionIT { public void validate_pagination_last_page() { for (int i = 0; i < 12; i++) { - webhookDeliveryDbTester.insert(newDto().setComponentUuid(project.uuid()).setCeTaskUuid("t1").setWebhookUuid("wh-1-uuid")); + webhookDeliveryDbTester.insert(newDto().setProjectUuid(project.getUuid()).setCeTaskUuid("t1").setWebhookUuid("wh-1-uuid")); } userSession.logIn().addProjectPermission(UserRole.ADMIN, project); @@ -250,7 +250,7 @@ public class WebhookDeliveriesActionIT { @Test public void search_by_component_and_throw_ForbiddenException_if_not_admin_of_project() { WebhookDeliveryDto dto = newDto() - .setComponentUuid(project.uuid()); + .setProjectUuid(project.getUuid()); dbClient.webhookDeliveryDao().insert(db.getSession(), dto); db.commit(); userSession.logIn().addProjectPermission(UserRole.USER, project); @@ -265,7 +265,7 @@ public class WebhookDeliveriesActionIT { @Test public void search_by_task_and_throw_ForbiddenException_if_not_admin_of_project() { WebhookDeliveryDto dto = newDto() - .setComponentUuid(project.uuid()); + .setProjectUuid(project.getUuid()); dbClient.webhookDeliveryDao().insert(db.getSession(), dto); db.commit(); userSession.logIn().addProjectPermission(UserRole.USER, project); diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/webhook/ws/WebhookDeliveryActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/webhook/ws/WebhookDeliveryActionIT.java index 782afb9d43e..b12e175b40e 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/webhook/ws/WebhookDeliveryActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/webhook/ws/WebhookDeliveryActionIT.java @@ -23,11 +23,10 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.sonar.api.server.ws.WebService; -import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; import org.sonar.db.DbClient; import org.sonar.db.DbTester; -import org.sonar.db.component.ComponentDto; +import org.sonar.db.project.ProjectDto; import org.sonar.db.webhook.WebhookDeliveryDto; import org.sonar.server.component.ComponentFinder; import org.sonar.server.component.TestComponentFinder; @@ -50,18 +49,18 @@ public class WebhookDeliveryActionIT { public UserSessionRule userSession = UserSessionRule.standalone(); @Rule - public DbTester db = DbTester.create(System2.INSTANCE); + public DbTester db = DbTester.create(true); private DbClient dbClient = db.getDbClient(); private WsActionTester ws; - private ComponentDto project; + private ProjectDto project; @Before public void setUp() { ComponentFinder componentFinder = TestComponentFinder.from(db); WebhookDeliveryAction underTest = new WebhookDeliveryAction(dbClient, userSession, componentFinder); ws = new WsActionTester(underTest); - project = db.components().insertPrivateProject(c -> c.setKey("my-project")).getMainBranchComponent(); + project = db.components().insertPrivateProject(c -> c.setKey("my-project")).getProjectDto(); } @Test @@ -96,7 +95,7 @@ public class WebhookDeliveryActionIT { public void load_the_delivery_of_example() { WebhookDeliveryDto dto = newDto() .setUuid("d1") - .setComponentUuid(project.uuid()) + .setProjectUuid(project.getUuid()) .setCeTaskUuid("task-1") .setName("Jenkins") .setUrl("http://jenkins") @@ -120,7 +119,7 @@ public class WebhookDeliveryActionIT { @Test public void return_delivery_that_failed_to_be_sent() { WebhookDeliveryDto dto = newDto() - .setComponentUuid(project.uuid()) + .setProjectUuid(project.getUuid()) .setSuccess(false) .setHttpStatus(null) .setErrorStacktrace("IOException -> can not connect"); @@ -140,7 +139,7 @@ public class WebhookDeliveryActionIT { @Test public void return_delivery_with_none_of_optional_fields() { WebhookDeliveryDto dto = newDto() - .setComponentUuid(project.uuid()) + .setProjectUuid(project.getUuid()) .setCeTaskUuid(null) .setHttpStatus(null) .setErrorStacktrace(null) @@ -162,7 +161,7 @@ public class WebhookDeliveryActionIT { @Test public void throw_ForbiddenException_if_not_admin_of_project() { WebhookDeliveryDto dto = newDto() - .setComponentUuid(project.uuid()); + .setProjectUuid(project.getUuid()); dbClient.webhookDeliveryDao().insert(db.getSession(), dto); db.commit(); userSession.logIn().addProjectPermission(UserRole.USER, project); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/webhook/ws/WebhookDeliveriesAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/webhook/ws/WebhookDeliveriesAction.java index c871b5be029..07d8ea74669 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/webhook/ws/WebhookDeliveriesAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/webhook/ws/WebhookDeliveriesAction.java @@ -127,8 +127,8 @@ public class WebhookDeliveriesAction implements WebhooksWsAction { ProjectDto project = componentFinder.getProjectByKey(dbSession, projectKey); projectUuidMap = new HashMap<>(); projectUuidMap.put(project.getUuid(), project); - totalElements = dbClient.webhookDeliveryDao().countDeliveriesByComponentUuid(dbSession, project.getUuid()); - deliveries = dbClient.webhookDeliveryDao().selectOrderedByComponentUuid(dbSession, project.getUuid(), offset(page, pageSize), pageSize); + totalElements = dbClient.webhookDeliveryDao().countDeliveriesByProjectUuid(dbSession, project.getUuid()); + deliveries = dbClient.webhookDeliveryDao().selectOrderedByProjectUuid(dbSession, project.getUuid(), offset(page, pageSize), pageSize); } else { totalElements = dbClient.webhookDeliveryDao().countDeliveriesByCeTaskUuid(dbSession, ceTaskId); deliveries = dbClient.webhookDeliveryDao().selectOrderedByCeTaskUuid(dbSession, ceTaskId, offset(page, pageSize), pageSize); @@ -141,7 +141,7 @@ public class WebhookDeliveriesAction implements WebhooksWsAction { private Map getProjectsDto(DbSession dbSession, List deliveries) { Map deliveredComponentUuid = deliveries .stream() - .collect(Collectors.toMap(WebhookDeliveryLiteDto::getUuid, WebhookDeliveryLiteDto::getComponentUuid)); + .collect(Collectors.toMap(WebhookDeliveryLiteDto::getUuid, WebhookDeliveryLiteDto::getProjectUuid)); if (!deliveredComponentUuid.isEmpty()) { return dbClient.projectDao().selectByUuids(dbSession, new HashSet<>(deliveredComponentUuid.values())) @@ -183,7 +183,7 @@ public class WebhookDeliveriesAction implements WebhooksWsAction { Webhooks.DeliveriesWsResponse.Builder responseBuilder = Webhooks.DeliveriesWsResponse.newBuilder(); Webhooks.Delivery.Builder deliveryBuilder = Webhooks.Delivery.newBuilder(); for (WebhookDeliveryLiteDto dto : deliveryDtos) { - ProjectDto project = projectUuidMap.get(dto.getComponentUuid()); + ProjectDto project = projectUuidMap.get(dto.getProjectUuid()); copyDtoToProtobuf(project, dto, deliveryBuilder); responseBuilder.addDeliveries(deliveryBuilder); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/webhook/ws/WebhookDeliveryAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/webhook/ws/WebhookDeliveryAction.java index 9ba09c28c69..3e2631f3b90 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/webhook/ws/WebhookDeliveryAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/webhook/ws/WebhookDeliveryAction.java @@ -81,7 +81,7 @@ public class WebhookDeliveryAction implements WebhooksWsAction { try (DbSession dbSession = dbClient.openSession(false)) { WebhookDeliveryDto delivery = dbClient.webhookDeliveryDao().selectByUuid(dbSession, deliveryUuid) .orElseThrow(() -> new NotFoundException("Webhook delivery not found")); - ProjectDto project = componentFinder.getProjectByUuid(dbSession, delivery.getComponentUuid()); + ProjectDto project = componentFinder.getProjectByUuid(dbSession, delivery.getProjectUuid()); return new Data(project, delivery); } } -- 2.39.5