Skip to content
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
34 changes: 0 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,40 +249,6 @@ az cosmosdb update --resource-group $ResourceGroup --name $AccountName --enable

See [MS Learn](https://learn.microsoft.com/en-us/azure/cosmos-db/priority-based-execution) for more details.

### Delete resources by partition key

The preview version of the library extends the `ICosmosWriter` and `ILowPriorityCosmosWriter` with and additional method `DeletePartitionAsync` to delete all resources in a container based on a partition key. The deletion will be executed in a CosmosDB background service using a percentage of the RU's available. The effect are available immediatly as all resources in the partition will not be available through reads or queries.

In order to use this new method the "Delete All Items By Partition Key" feature needs to be enabled on the CosmosDB account.

This can be done through Azure CLI:

```bash
# Delete All Items By Partition Key
az cosmosdb update --resource-group $ResourceGroup --name $AccountName --capabilities DeleteAllItemsByPartitionKey
```

or wih bicep:

```bicep
resource cosmosAccount 'Microsoft.DocumentDB/databaseAccounts@2024-05-15' = {
name: cosmosName
properties: {
databaseAccountOfferType: 'Standard'
locations: location
capabilities: [
{
name: 'DeleteAllItemsByPartitionKey'
}
]
}
}
```

If the feature is not enabled when calling this method then a `CosmosException` will be thrown.

See [MS Learn](https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/how-to-delete-by-partition-key) for more details.

## Unit Testing
The reader and writer interfaces can easily be mocked, but in some cases it is nice to have a fake version of a reader or writer to mimic the behavior of the read and write operations. For this purpose the `Atc.Cosmos.Testing` namespace contains the following fakes:

Expand Down
5 changes: 3 additions & 2 deletions global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"sdk": {
"rollForward": "latestMajor",
"allowPrerelease": false
"version": "8.0.0",
"rollForward": "latestMinor",
"allowPrerelease": false
}
}
5 changes: 3 additions & 2 deletions src/Atc.Cosmos/Atc.Cosmos.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.45.0-preview.0" Condition="$(IsPreview)" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.44.1" Condition="!$(IsPreview)" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.47.0-preview.0" Condition="$(IsPreview)" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.46.0" Condition="!$(IsPreview)" />
</ItemGroup>

<ItemGroup Condition="$(TargetFramework) == 'netstandard2.0'">
Expand Down
2 changes: 0 additions & 2 deletions src/Atc.Cosmos/ICosmosWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ public Task<bool> TryDeleteAsync(
string documentId,
string partitionKey,
CancellationToken cancellationToken = default);
#if PREVIEW

/// <summary>
/// Preview Feature DeleteAllItemsByPartitionKey.<br/>
Expand All @@ -168,7 +167,6 @@ public Task<bool> TryDeleteAsync(
public Task DeletePartitionAsync(
string partitionKey,
CancellationToken cancellationToken = default);
#endif

/// <summary>
/// Updates a <typeparamref name="T"/> resource that is read from the configured
Expand Down
4 changes: 2 additions & 2 deletions src/Atc.Cosmos/Internal/CosmosWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ await container

return true;
}
#if PREVIEW

public Task DeletePartitionAsync(
string partitionKey,
Expand All @@ -185,11 +184,12 @@ public Task DeletePartitionAsync(
new PartitionKey(partitionKey),
new ItemRequestOptions
{
#if PREVIEW
PriorityLevel = PriorityLevel,
#endif
},
cancellationToken: cancellationToken)
.ProcessResponseMessage();
#endif

public Task<T> UpdateAsync(
string documentId,
Expand Down
2 changes: 0 additions & 2 deletions src/Atc.Cosmos/Testing/FakeCosmos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,6 @@ Task<bool> ICosmosWriter<T>.TryDeleteAsync(
documentId,
partitionKey,
cancellationToken);
#if PREVIEW

Task ICosmosWriter<T>.DeletePartitionAsync(
string partitionKey,
Expand All @@ -490,7 +489,6 @@ Task ICosmosWriter<T>.DeletePartitionAsync(
.DeletePartitionAsync(
partitionKey,
cancellationToken);
#endif

Task<T> ICosmosWriter<T>.UpdateAsync(
string documentId,
Expand Down
2 changes: 0 additions & 2 deletions src/Atc.Cosmos/Testing/FakeCosmosWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ await DeleteAsync(

return true;
}
#if PREVIEW

public virtual Task DeletePartitionAsync(
string partitionKey,
Expand All @@ -164,7 +163,6 @@ public virtual Task DeletePartitionAsync(

return Task.CompletedTask;
}
#endif

public virtual Task<T> UpdateAsync(
string documentId,
Expand Down
6 changes: 4 additions & 2 deletions test/Atc.Cosmos.Tests/CosmosWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ public async Task Should_Return_False_When_Trying_To_Delete_NonExisting_Resource
#endif
cancellationToken: cancellationToken);
}
#if PREVIEW

[Theory, AutoNSubstituteData]
public async Task DeletePartitionAsync_Calls_DeleteAllItemsByPartitionKeyStreamAsync_On_Container(
Expand All @@ -307,7 +306,11 @@ public async Task DeletePartitionAsync_Calls_DeleteAllItemsByPartitionKeyStreamA
.Received(1)
.DeleteAllItemsByPartitionKeyStreamAsync(
new PartitionKey(record.Pk),
#if PREVIEW
Arg.Is<ItemRequestOptions>(o => o.PriorityLevel == PriorityLevel.High),
#else
Arg.Any<ItemRequestOptions>(),
#endif
cancellationToken: cancellationToken);
}

Expand All @@ -323,7 +326,6 @@ public Task DeletePartitionAsync_Throws_CosmosException_If_ResponseMessage_Is_No
Func<Task> act = () => sut.DeletePartitionAsync(record.Pk, cancellationToken);
return act.Should().ThrowAsync<CosmosException>();
}
#endif

[Theory, AutoNSubstituteData]
public async Task UpdateAsync_Reads_The_Resource(
Expand Down
2 changes: 0 additions & 2 deletions test/Atc.Cosmos.Tests/Testing/FakeCosmosWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ public async Task DeleteAsync_Should_Replace_Existing_Document(
.Should()
.NotContain(existingDocument);
}
#if PREVIEW

[Theory, AutoNSubstituteData]
public async Task DeletePartitionAsyncAsync_Should_Delete_Existing_Documents(
Expand Down Expand Up @@ -249,7 +248,6 @@ public async Task DeletePartitionAsyncAsync_Should_Delete_Existing_Documents(
.And
.Contain(existingDocument3);
}
#endif

[Theory, AutoNSubstituteData]
public void UpdateAsync_Should_Throw_If_Document_Does_Not_Exists(
Expand Down
Loading