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
87 changes: 0 additions & 87 deletions src/DocumentDbTests/Bug_3778_schema_name_issue.cs

This file was deleted.

48 changes: 48 additions & 0 deletions src/DocumentDbTests/Bugs/Bug_3778_schema_name_issue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Threading.Tasks;
using JasperFx;
using Marten.Testing;
using Marten.Testing.Harness;
using Weasel.Postgresql.Tables;
using Xunit;

namespace DocumentDbTests.Bugs;

public class Bug_3778_schema_name__ending_with_d_issue: OneOffConfigurationsContext
{
[Theory]
[InlineData("pprd")]
[InlineData("d")]
public async Task TestSchemaNameEndingWith_d_In_Index(string schemaName)
{
StoreOptions(options =>
{
options.Connection(ConnectionSource.ConnectionString);
options.AutoCreateSchemaObjects = AutoCreate.All;
options.DatabaseSchemaName = schemaName;
options.Schema.For<User3778>()
.Index(d => d.D1)
.Index(d => d.D2)
.NgramIndex(d => d.Name)
.FullTextIndex(d => d.Name)
.Duplicate(d => d.Manager.Name, configure: idx =>
{
idx.Name = "idx_manager_name";
idx.Method = IndexMethod.hash;
})
.Metadata(m =>
{
m.LastModified.MapTo(f => f.LastModifiedOn);
m.CreatedAt.MapTo(f => f.CreatedOn);
m.Revision.MapTo(f => f.Version);
})
.Index(f => new { f.CreatedOn, f.IsArchived })
.UseNumericRevisions(true);
});

await theStore.EnsureStorageExistsAsync(typeof(User3778));
}
}

public record User3778(Guid Id, string Name, DateTimeOffset D1, DateOnly D2, User3778 Manager,
DateTimeOffset LastModifiedOn, DateTimeOffset CreatedOn, int Version, bool IsArchived);
21 changes: 17 additions & 4 deletions src/Marten/Util/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text.RegularExpressions;
using JasperFx.Core;
using JasperFx.Core.Reflection;
using Marten.Schema;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

Expand Down Expand Up @@ -45,7 +46,7 @@ public static string ToJsonKey(this MemberInfo member, Casing casing)
}

/// <summary>
/// Remove table alias from a SQL string. This is also a candidate to move to Weasel.
/// Remove table alias from a SQL string.
/// </summary>
/// <param name="sql"></param>
/// <param name="tableAlias"></param>
Expand All @@ -55,9 +56,21 @@ public static string RemoveTableAlias(this string sql, string tableAlias)
if (string.IsNullOrEmpty(sql) || string.IsNullOrEmpty(tableAlias))
return sql;

// Remove 'd.' only when it's NOT followed by 'mt_' (anything followed bt mt_ will be schema name)
var regex = _removeTableAliasRegexCache.GetOrAdd(tableAlias, alias =>
new Regex(@$"\b{Regex.Escape(alias)}\.(?!mt_)", RegexOptions.Compiled));
// First pass: remove '<table_alias>.' for any the Marten defined metadata columns
string[] metadataColumns = [SchemaConstants.DocumentTypeColumn, SchemaConstants.LastModifiedColumn,
SchemaConstants.DotNetTypeColumn, SchemaConstants.VersionColumn, SchemaConstants.CreatedAtColumn,
SchemaConstants.DeletedColumn, SchemaConstants.DeletedAtColumn];
var metadataColumnRegex = _removeTableAliasRegexCache.GetOrAdd(@$"\b{Regex.Escape(tableAlias)}\.({string.Join("|", metadataColumns)})\b", pattern =>
new Regex(pattern, RegexOptions.Compiled));
sql = metadataColumnRegex.Replace(sql, "$1");

if (!sql.Contains($"{tableAlias}."))
return sql;

// Second pass: remove '<table_alias>.' only when it's NOT followed by 'mt_' (anything followed by mt_ could possibly be schema name for a Marten function)
var regex = _removeTableAliasRegexCache.GetOrAdd(@$"\b{Regex.Escape(tableAlias)}\.(?!mt_)", pattern =>
new Regex(pattern, RegexOptions.Compiled));

return regex.Replace(sql, "");
}
}
Loading