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

[IDP-788] Use default index name when the name is not provided #46

Merged
merged 1 commit into from
Jan 29, 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
15 changes: 14 additions & 1 deletion src/Workleap.Extensions.Mongo.Tests/UniqueIndexNameTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Workleap.Extensions.Xunit;
using Workleap.Extensions.Xunit;
using MongoDB.Driver;
using Workleap.Extensions.Mongo.Indexing;

Expand Down Expand Up @@ -39,6 +39,19 @@ public void UniqueIndex_NotCreated_When_Invalid_IndexName_Specified(string index
Assert.Null(uxIndexName);
}

[Fact]
public void UniqueIndex_ComputeDefaultName_When_IndexName_NotSpecified()
{
var index = new CreateIndexModel<SampleDocument>(
Builders<SampleDocument>.IndexKeys.Ascending(x => x.SampleField),
options: null);

var result = UniqueIndexName.TryCreate(index, out var uxIndexName);

Assert.True(result);
Assert.Equal("SampleField_1", uxIndexName!.Prefix);
}

private static CreateIndexModel<SampleDocument> CreateIndex(string indexName)
{
return new CreateIndexModel<SampleDocument>(
Expand Down
14 changes: 7 additions & 7 deletions src/Workleap.Extensions.Mongo/Indexing/UniqueIndexName.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.CodeAnalysis;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using MongoDB.Driver.Core.Operations;

namespace Workleap.Extensions.Mongo.Indexing;

Expand Down Expand Up @@ -37,8 +38,7 @@ private UniqueIndexName()
public static bool TryCreate<TDocument>(CreateIndexModel<TDocument> indexModel, [MaybeNullWhen(false)] out UniqueIndexName indexName)
{
var options = indexModel.Options;

var prefix = options.Name?.Trim() ?? string.Empty;
var prefix = options?.Name?.Trim() ?? IndexNameHelper.GetIndexName(indexModel.Keys.Render(BsonSerializer.LookupSerializer<TDocument>(), BsonSerializer.SerializerRegistry));
if (prefix.Length == 0)
{
indexName = null;
Expand All @@ -50,10 +50,10 @@ public static bool TryCreate<TDocument>(CreateIndexModel<TDocument> indexModel,

var bsonIndexFields = indexModel.Keys.Render(bsonSerializer, serializerRegistry).ToString();
var indexDescription = new StringBuilder(bsonIndexFields)
.Append(options.Unique.HasValue && options.Unique.Value ? "unique" : string.Empty)
.Append(options.Sparse.HasValue && options.Sparse.Value ? "sparse" : string.Empty)
.Append(options.WildcardProjection is { } projection ? projection.Render(bsonSerializer, serializerRegistry) : string.Empty)
.Append(options.PartialFilterExpression is { } filter ? filter.Render(bsonSerializer, serializerRegistry) : string.Empty)
.Append(options?.Unique is true ? "unique" : string.Empty)
.Append(options?.Sparse is true ? "sparse" : string.Empty)
.Append(options?.WildcardProjection is { } projection ? projection.Render(bsonSerializer, serializerRegistry) : string.Empty)
.Append(options?.PartialFilterExpression is { } filter ? filter.Render(bsonSerializer, serializerRegistry) : string.Empty)
.ToString();

string hashHex;
Expand Down