Skip to content

Support multiple container unique-key constraints - #320

Open
Mike Krüger (mkrueger) wants to merge 1 commit into
mainfrom
dev/mkrueger/fix/312-unique-key-policy
Open

Mike Krüger (mkrueger) wants to merge 1 commit into
mainfrom
dev/mkrueger/fix/312-unique-key-policy

Conversation

@mkrueger

Copy link
Copy Markdown
Collaborator

Add --unique_key_policy to mkcon and create container so users can define multiple independent or composite unique-key constraints through the CLI or MCP, on both data-plane and ARM connections. Each uniqueKeys entry defines a separate constraint; multiple paths in one entry form a composite constraint.

  • Pass the policy as a JSON string. The shell validates its structure and limits before issuing a write. The existing comma-separated unique_key argument still defines one composite constraint and cannot be combined with the new option.
  • When either input supplies a policy, an existing container now produces an error. On the data plane, this replaces the previous existing result for legacy unique_key calls. For that idempotent result, omit the policy when re-running against an existing container.
  • ARM checks for an existing container before calling create-or-update. The check is not atomic; avoid concurrent creation attempts for the same name.

Fixes #312

Comment on lines +36 to +40
foreach (var key in keys.EnumerateArray())
{
var paths = ReadArrayProperty(key, "paths");
constraints.Add(paths.EnumerateArray().Select(ReadPath).ToArray());
}

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Regression coverage is missing for rejecting --unique_key_policy on item and database targets and their aliases, including empty values, before backend access.

1 open issue
What changed in this PR

Adds full unique-key policy support to container creation, allowing independent and composite constraints through CLI and MCP on data-plane and ARM connections.

Changes:

  • Adds --unique_key_policy with structure and limit validation, while retaining the legacy composite-key argument.
  • Passes multiple constraints to both backends and rejects policy-bearing requests for existing containers.
  • Updates help, documentation, and tests for binding, validation, and backend behavior.
File Description
l10n/​CosmosDBShell.json Updates generated help and error messages.
docs/​commands.md Documents policy syntax and creation-only behavior.
CosmosDBShell/​lang/​en.ftl Adds localized option descriptions and errors.
CosmosDBShell/​Azure.Data.Cosmos.Shell.Core/​ICosmosResourceOperations.cs Accepts multiple unique-key constraints.
CosmosDBShell/​Azure.Data.Cosmos.Shell.Core/​ExistingContainerUniqueKeyPolicyException.cs Adds a dedicated existing-container error.
CosmosDBShell/​Azure.Data.Cosmos.Shell.Core/​DataPlaneCosmosResourceOperations.cs Builds multiple constraints and rejects existing containers with policies.
CosmosDBShell/​Azure.Data.Cosmos.Shell.Core/​CosmosResourceFacade.cs Forwards structured constraints to resource operations.
CosmosDBShell/​Azure.Data.Cosmos.Shell.Core/​CosmosArmResourceProvider.cs Builds ARM policies and checks container existence before writing.
CosmosDBShell/​Azure.Data.Cosmos.Shell.Core/​ArmCosmosResourceOperations.cs Passes structured constraints to the ARM provider.
CosmosDBShell/​Azure.Data.Cosmos.Shell.Commands/​MakeContainerCommand.cs Binds and validates policies and translates backend errors.
CosmosDBShell/​Azure.Data.Cosmos.Shell.Commands/​CreateCommand.cs Forwards the option and restricts it to container targets.
CosmosDBShell/​Azure.Data.Cosmos.Shell.Commands/​ContainerUniqueKeyPolicy.cs Parses legacy and JSON inputs and validates constraints.
CosmosDBShell.Tests/​McpConfirmationTests.cs Tests MCP policy binding for both commands.
CosmosDBShell.Tests/​CommandTests/​ServerlessCreationThroughputTests.cs Tests CLI binding, validation, backend payloads, and existing-container rejection.

Review effort: Balanced

Comment on lines +58 to +60
if (this.UniqueKeyPolicy is not null && !IsContainer(item))
{
throw CommandException.Usage("create", MessageService.GetString("command-create-error_unique_key_policy_only_for_containers"));
@github-code-quality

Copy link
Copy Markdown

Code coverage overview

Languages: C#

C# / code-coverage/dotnet

main 3628267 dev/mkrueger/fix/312... 2fe0f4a +/-
76% 76% 0%
View coverage of the 5 most impacted files.
File main 3628267 dev/mkrueger/fix/312... 2fe0f4a +/-
D:\a\CosmosDBSh...ainerCommand.cs 89% 88% -1%
D:\a\CosmosDBSh...reateCommand.cs 79% 79% 0%
D:\a\CosmosDBSh...ceOperations.cs 51% 52% +1%
D:\a\CosmosDBSh...urceProvider.cs 65% 66% +1%
D:\a\CosmosDBSh...queKeyPolicy.cs 0% 100% +100%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants