From 418e238a2e30189d1a87885214f188e1e99cb2c3 Mon Sep 17 00:00:00 2001 From: Malik Diarra Date: Wed, 26 Oct 2022 08:00:14 -0700 Subject: [PATCH] Optimize listDestinationsForWorkspace route (#18456) Add a function to fetch all destinations in a workspace --- .../config/persistence/ConfigRepository.java | 16 ++++++++++++++++ .../ConfigRepositoryE2EReadWriteTest.java | 9 +++++++++ .../server/handlers/DestinationHandler.java | 10 +--------- .../server/handlers/DestinationHandlerTest.java | 3 ++- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/airbyte-config/config-persistence/src/main/java/io/airbyte/config/persistence/ConfigRepository.java b/airbyte-config/config-persistence/src/main/java/io/airbyte/config/persistence/ConfigRepository.java index 38acad7f9fe4..7f9a7d011981 100644 --- a/airbyte-config/config-persistence/src/main/java/io/airbyte/config/persistence/ConfigRepository.java +++ b/airbyte-config/config-persistence/src/main/java/io/airbyte/config/persistence/ConfigRepository.java @@ -650,6 +650,22 @@ public List listDestinationConnection() throws JsonValida return persistence.listConfigs(ConfigSchema.DESTINATION_CONNECTION, DestinationConnection.class); } + /** + * Returns all destinations for a workspace. Does not contain secrets. + * + * @param workspaceId - id of the workspace + * @return destinations + * @throws IOException - you never know when you IO + */ + public List listWorkspaceDestinationConnection(UUID workspaceId) throws IOException { + final Result result = database.query(ctx -> ctx.select(asterisk()) + .from(ACTOR) + .where(ACTOR.ACTOR_TYPE.eq(ActorType.destination)) + .and(ACTOR.WORKSPACE_ID.eq(workspaceId)) + .andNot(ACTOR.TOMBSTONE).fetch()); + return result.stream().map(DbConverter::buildDestinationConnection).collect(Collectors.toList()); + } + public StandardSync getStandardSync(final UUID connectionId) throws JsonValidationException, IOException, ConfigNotFoundException { return persistence.getConfig(ConfigSchema.STANDARD_SYNC, connectionId.toString(), StandardSync.class); } diff --git a/airbyte-config/config-persistence/src/test/java/io/airbyte/config/persistence/ConfigRepositoryE2EReadWriteTest.java b/airbyte-config/config-persistence/src/test/java/io/airbyte/config/persistence/ConfigRepositoryE2EReadWriteTest.java index 552355066b5e..e3f3cee96398 100644 --- a/airbyte-config/config-persistence/src/test/java/io/airbyte/config/persistence/ConfigRepositoryE2EReadWriteTest.java +++ b/airbyte-config/config-persistence/src/test/java/io/airbyte/config/persistence/ConfigRepositoryE2EReadWriteTest.java @@ -306,6 +306,15 @@ void testListWorkspaceSources() throws IOException { assertThat(sources).hasSameElementsAs(expectedSources); } + @Test + void testListWorkspaceDestinations() throws IOException { + UUID workspaceId = MockData.standardWorkspaces().get(0).getWorkspaceId(); + final List expectedDestinations = MockData.destinationConnections().stream() + .filter(destination -> destination.getWorkspaceId().equals(workspaceId)).collect(Collectors.toList()); + final List destinations = configRepository.listWorkspaceDestinationConnection(workspaceId); + assertThat(destinations).hasSameElementsAs(expectedDestinations); + } + @Test void testSourceDefinitionGrants() throws IOException { final UUID workspaceId = MockData.standardWorkspaces().get(0).getWorkspaceId(); diff --git a/airbyte-server/src/main/java/io/airbyte/server/handlers/DestinationHandler.java b/airbyte-server/src/main/java/io/airbyte/server/handlers/DestinationHandler.java index 73515d520eb5..4263e25e5df4 100644 --- a/airbyte-server/src/main/java/io/airbyte/server/handlers/DestinationHandler.java +++ b/airbyte-server/src/main/java/io/airbyte/server/handlers/DestinationHandler.java @@ -198,15 +198,7 @@ public DestinationReadList listDestinationsForWorkspace(final WorkspaceIdRequest throws ConfigNotFoundException, IOException, JsonValidationException { final List reads = Lists.newArrayList(); - for (final DestinationConnection dci : configRepository.listDestinationConnection()) { - if (!dci.getWorkspaceId().equals(workspaceIdRequestBody.getWorkspaceId())) { - continue; - } - - if (dci.getTombstone()) { - continue; - } - + for (final DestinationConnection dci : configRepository.listWorkspaceDestinationConnection(workspaceIdRequestBody.getWorkspaceId())) { reads.add(buildDestinationRead(dci)); } diff --git a/airbyte-server/src/test/java/io/airbyte/server/handlers/DestinationHandlerTest.java b/airbyte-server/src/test/java/io/airbyte/server/handlers/DestinationHandlerTest.java index 6dc096c862e1..28fb3868bb2b 100644 --- a/airbyte-server/src/test/java/io/airbyte/server/handlers/DestinationHandlerTest.java +++ b/airbyte-server/src/test/java/io/airbyte/server/handlers/DestinationHandlerTest.java @@ -230,7 +230,8 @@ void testListDestinationForWorkspace() throws JsonValidationException, ConfigNot final WorkspaceIdRequestBody workspaceIdRequestBody = new WorkspaceIdRequestBody().workspaceId(destinationConnection.getWorkspaceId()); when(configRepository.getDestinationConnection(destinationConnection.getDestinationId())).thenReturn(destinationConnection); - when(configRepository.listDestinationConnection()).thenReturn(Lists.newArrayList(destinationConnection)); + when(configRepository.listWorkspaceDestinationConnection(destinationConnection.getWorkspaceId())) + .thenReturn(Lists.newArrayList(destinationConnection)); when(configRepository.getStandardDestinationDefinition(standardDestinationDefinition.getDestinationDefinitionId())) .thenReturn(standardDestinationDefinition); when(secretsProcessor.prepareSecretsForOutput(destinationConnection.getConfiguration(),