Skip to content

Commit cb9cfe1

Browse files
committed
Allow skipping version check on versioned docs
1 parent bcd2f8b commit cb9cfe1

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

src/Foundatio.Repositories.Elasticsearch/Repositories/ElasticRepositoryBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ await Run.WithRetriesAsync(async () => {
204204
if (id.Routing != null)
205205
indexParameters.Routing = id.Routing;
206206

207-
if (HasVersion) {
207+
if (HasVersion && !options.ShouldSkipVersionCheck()) {
208208
indexParameters.IfSequenceNumber = response.SequenceNumber;
209209
indexParameters.IfPrimaryTerm = response.PrimaryTerm;
210210
}
@@ -1097,7 +1097,7 @@ private async Task IndexDocumentsAsync(IReadOnlyCollection<T> documents, bool is
10971097

10981098
i.Index(ElasticIndex.GetIndex(document));
10991099

1100-
if (HasVersion && !isCreateOperation) {
1100+
if (HasVersion && !isCreateOperation && !options.ShouldSkipVersionCheck()) {
11011101
var elasticVersion = ((IVersioned)document).GetElasticVersion();
11021102
i.IfPrimaryTerm(elasticVersion.PrimaryTerm);
11031103
i.IfSequenceNumber(elasticVersion.SequenceNumber);
@@ -1134,7 +1134,7 @@ private async Task IndexDocumentsAsync(IReadOnlyCollection<T> documents, bool is
11341134
//baseOperation.Routing = GetParentIdFunc != null ? GetParentIdFunc(d) : d.Id;
11351135
baseOperation.Index = ElasticIndex.GetIndex(d);
11361136

1137-
if (HasVersion && !isCreateOperation) {
1137+
if (HasVersion && !isCreateOperation && !options.ShouldSkipVersionCheck()) {
11381138
var elasticVersion = ((IVersioned)d).GetElasticVersion();
11391139
indexOperation.IfSequenceNumber = elasticVersion.SequenceNumber;
11401140
indexOperation.IfPrimaryTerm = elasticVersion.PrimaryTerm;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Foundatio.Repositories.Options;
2+
3+
namespace Foundatio.Repositories {
4+
public static class SetVersionsOptionsExtensions {
5+
internal const string SkipVersionCheckKey = "@SkipVersionCheck";
6+
7+
public static T VersionCheck<T>(this T options, bool shouldCheckVersion) where T : ICommandOptions {
8+
return options.BuildOption(SkipVersionCheckKey, shouldCheckVersion);
9+
}
10+
11+
public static T SkipVersionCheck<T>(this T options) where T : ICommandOptions {
12+
return options.VersionCheck(true);
13+
}
14+
}
15+
}
16+
17+
namespace Foundatio.Repositories.Options {
18+
public static class ReadVersionOptionsExtensions {
19+
public static bool ShouldSkipVersionCheck(this ICommandOptions options) {
20+
return options.SafeGetOption(SetVersionsOptionsExtensions.SkipVersionCheckKey, false);
21+
}
22+
}
23+
}

tests/Foundatio.Repositories.Elasticsearch.Tests/VersionedTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ public async Task AddAsync() {
4141
Assert.Equal(employee, employee2);
4242
}
4343

44+
[Fact]
45+
public async Task CanSaveNonExistingAsync() {
46+
var employee = EmployeeGenerator.Default;
47+
Assert.Null(employee.Version);
48+
employee.Id = ObjectId.GenerateNewId().ToString();
49+
50+
employee = await _employeeRepository.SaveAsync(employee, o => o.SkipVersionCheck());
51+
Assert.NotNull(employee?.Id);
52+
Assert.Equal("1:0", employee.Version);
53+
54+
var employee2 = await _employeeRepository.GetByIdAsync(employee.Id);
55+
Assert.Equal(employee, employee2);
56+
}
57+
4458
[Fact]
4559
public async Task AddAndIgnoreHighVersionAsync() {
4660
var employee = EmployeeGenerator.Generate();

0 commit comments

Comments
 (0)