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
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ public void apply_the_version_suffix_to_table_alias_when_version_attribute_is_on
var mapping = store.Options.Storage.MappingFor(typeof(OtherAggregate));
mapping.Alias.ShouldBe("otheraggregate_3");
}

[Fact]
public void apply_the_version_suffix_when_document_alias_is_set_via_fluent_api()
{
using var store = DocumentStore.For(opts =>
{
opts.Connection(ConnectionSource.ConnectionString);
opts.Projections.Add<Version2>(ProjectionLifecycle.Async);
opts.Schema.For<MyAggregate>().DocumentAlias("custom_alias");
});

var mapping = store.Options.Storage.MappingFor(typeof(MyAggregate));
mapping.Alias.ShouldBe("custom_alias_2");
}
}

public class Version2: SingleStreamProjection<MyAggregate, Guid>
Expand Down
24 changes: 19 additions & 5 deletions src/Marten/Events/Projections/ProjectionDocumentPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ public void Apply(DocumentMapping mapping)

if (mapping.StoreOptions.Projections.TryFindAggregate(mapping.DocumentType, out var projection))
{
if (projection.Version > 1)
{
mapping.Alias += "_" + projection.Version;
}

mapping.UseOptimisticConcurrency = false;
mapping.Metadata.Version.Enabled = false;
mapping.UseNumericRevisions = true;
Expand All @@ -28,7 +23,26 @@ public void Apply(DocumentMapping mapping)
{
m.ConfigureAggregateMapping(mapping, mapping.StoreOptions);
}
}
}
}

internal class ProjectionVersionAliasPolicy : IDocumentPolicy
{
public void Apply(DocumentMapping mapping)
{
if (mapping.StoreOptions.Projections == null) return;

if (mapping.StoreOptions.Projections.TryFindAggregate(mapping.DocumentType, out var projection))
{
if (projection.Version > 1)
{
var suffix = "_" + projection.Version;
if (!mapping.Alias.EndsWith(suffix))
{
mapping.Alias += suffix;
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/Marten/StoreOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ internal INpgsqlDataSourceFactory NpgsqlDataSourceFactory
new ProjectionDocumentPolicy()
];

private readonly List<IDocumentPolicy> _postPolicies = new();
private readonly List<IDocumentPolicy> _postPolicies = new() { new ProjectionVersionAliasPolicy() };

/// <summary>
/// Register "initial data loads" that will be applied to the DocumentStore when it is
Expand Down
Loading