Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport to branch(3.10) : Fix delete behavior in Cosmos adapter #2347

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.azure.cosmos.CosmosClient;
import com.azure.cosmos.CosmosException;
import com.azure.cosmos.implementation.NotFoundException;
import com.azure.cosmos.models.CosmosItemRequestOptions;
import com.azure.cosmos.models.PartitionKey;
import com.scalar.db.api.Delete;
Expand Down Expand Up @@ -42,7 +43,11 @@ private void execute(Mutation mutation, TableMetadata tableMetadata) throws Cosm
PartitionKey partitionKey = cosmosMutation.getCosmosPartitionKey();
CosmosItemRequestOptions options = new CosmosItemRequestOptions();

getContainer(mutation).deleteItem(id, partitionKey, options);
try {
getContainer(mutation).deleteItem(id, partitionKey, options);
} catch (NotFoundException ignored) {
// don't throw an exception if the item is not found
}
} else {
// clustering key is not fully specified
executeStoredProcedure(mutation, tableMetadata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.azure.cosmos.CosmosException;
import com.azure.cosmos.CosmosScripts;
import com.azure.cosmos.CosmosStoredProcedure;
import com.azure.cosmos.implementation.NotFoundException;
import com.azure.cosmos.models.CosmosItemRequestOptions;
import com.azure.cosmos.models.CosmosItemResponse;
import com.azure.cosmos.models.CosmosStoredProcedureRequestOptions;
Expand Down Expand Up @@ -121,6 +122,19 @@ public void handle_DeleteWithoutConditionsCosmosExceptionThrown_ShouldThrowExecu
.hasCause(toThrow);
}

@Test
public void handle_DeleteWithoutConditionsNotFoundExceptionThrown_ShouldNotThrowAnyException() {
// Arrange
doThrow(NotFoundException.class)
.when(container)
.deleteItem(anyString(), any(PartitionKey.class), any(CosmosItemRequestOptions.class));

Delete delete = prepareDelete();

// Act Assert
assertThatCode(() -> handler.handle(delete)).doesNotThrowAnyException();
}

@Test
public void handle_DeleteWithoutClusteringKeyGiven_ShouldCallStoredProcedure() {
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,17 @@ public void delete_MultipleDeleteWithDifferentConditionsGiven_ShouldDeleteProper
assertThat(results.size()).isEqualTo(0);
}

@Test
public void delete_ForNonExistingRecord_ShouldDoNothing() throws ExecutionException {
// Arrange

// Act Assert
assertThatCode(() -> storage.delete(prepareDeletes().get(0))).doesNotThrowAnyException();

Optional<Result> result = storage.get(prepareGet(0, 0));
assertThat(result).isNotPresent();
}

@Test
public void mutate_MultiplePutGiven_ShouldStoreProperly() throws ExecutionException, IOException {
// Arrange
Expand Down