-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Safe mongodb index create #1732
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
Safe mongodb index create #1732
Conversation
Fix RabbitMQ consuming body reusing (dotnetcore#1727)
If the name differs, we keep the old index.
… existing key patterns
Please explain:
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements safe MongoDB index creation to ensure backward compatibility with previous versions that may have created indexes with different names but identical key patterns. The change prevents duplicate indexes with the same key pattern from being created.
- Replaces direct
CreateManyAsync
calls with a new helper method that checks for existing index key patterns - Removes duplicate
StatusName
index entries from both published and received message index arrays - Adds logic to compare index key patterns rather than index names when determining if an index already exists
Comments suppressed due to low confidence (2)
src/DotNetCore.CAP.MongoDB/IStorageInitializer.MongoDB.cs:1
- The standalone
StatusName
index entries are removed, but there's still a composite indexStatusName.Ascending(x => x.ExpiresAt)
on lines 105 and 123. This removal may break existing queries that rely on the standaloneStatusName
index for optimal performance.
// Copyright (c) .NET Core Community. All rights reserved.
src/DotNetCore.CAP.MongoDB/IStorageInitializer.MongoDB.cs:1
- The standalone
StatusName
index entries are removed, but there's still a composite indexStatusName.Ascending(x => x.ExpiresAt)
on lines 105 and 123. This removal may break existing queries that rely on the standaloneStatusName
index for optimal performance.
// Copyright (c) .NET Core Community. All rights reserved.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
var indexes = await indexList.ToListAsync(cancellationToken); | ||
|
||
var existingIndexes = new HashSet<string>( | ||
indexes.Select(i => GetIndexKeyPattern(i["key"].AsBsonDocument)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accessing i["key"]
without checking if the key exists could throw a KeyNotFoundException
if an index document doesn't have a 'key' field. Add a null check or use TryGetValue
to handle malformed index documents safely.
indexes.Select(i => GetIndexKeyPattern(i["key"].AsBsonDocument)) | |
indexes | |
.Select(i => i.TryGetValue("key", out var keyValue) && keyValue != null | |
? GetIndexKeyPattern(keyValue.AsBsonDocument) | |
: null) | |
.Where(pattern => pattern != null) |
Copilot uses AI. Check for mistakes.
@yang-xiaodong LGTM, this may ba a problem if you are not doing any index maintenance. FYI even in case there will be a runtime exception, it will not break initialization, will just log it. |
@demorgi yes, agree. lets make it simple |
Description:
The change creates indexes if an index with the same key pattern does not already exist.
This ensures backward compatibility with previous versions that created indexes with different names but the same key pattern.
Affected components: