Database SnakeCaseNaming fixes
Fixes formatting of owned entity type property names. These are normally named "FooBar_Baz" by EF Core, but the snake case thing was turning them into "foo_bar__baz". The double underscore is now fixed. We don't *yet* have any EF Core owned entity in use, but I am planning to add one. I don't know if downstreams are using any so this should still be marked as a breaking change. Also fixed it creating and dropping a Compiled Regex instance for every name, the regex is now cached (and pregenerated).
This commit is contained in:
@@ -82,7 +82,7 @@ namespace Content.Server.Database
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class SnakeCaseConvention :
|
public partial class SnakeCaseConvention :
|
||||||
IEntityTypeAddedConvention,
|
IEntityTypeAddedConvention,
|
||||||
IEntityTypeAnnotationChangedConvention,
|
IEntityTypeAnnotationChangedConvention,
|
||||||
IPropertyAddedConvention,
|
IPropertyAddedConvention,
|
||||||
@@ -99,8 +99,8 @@ namespace Content.Server.Database
|
|||||||
|
|
||||||
public static string RewriteName(string name)
|
public static string RewriteName(string name)
|
||||||
{
|
{
|
||||||
var regex = new Regex("[A-Z]+", RegexOptions.Compiled);
|
return UpperCaseLocator()
|
||||||
return regex.Replace(
|
.Replace(
|
||||||
name,
|
name,
|
||||||
(Match match) => {
|
(Match match) => {
|
||||||
if (match.Index == 0 && (match.Value == "FK" || match.Value == "PK" || match.Value == "IX")) {
|
if (match.Index == 0 && (match.Value == "FK" || match.Value == "PK" || match.Value == "IX")) {
|
||||||
@@ -112,6 +112,11 @@ namespace Content.Server.Database
|
|||||||
return match.Value.ToLower();
|
return match.Value.ToLower();
|
||||||
if (match.Length > 1)
|
if (match.Length > 1)
|
||||||
return $"_{match.Value[..^1].ToLower()}_{match.Value[^1..^0].ToLower()}";
|
return $"_{match.Value[..^1].ToLower()}_{match.Value[^1..^0].ToLower()}";
|
||||||
|
|
||||||
|
// Do not add a _ if there is already one before this. This happens with owned entities.
|
||||||
|
if (name[match.Index - 1] == '_')
|
||||||
|
return match.Value.ToLower();
|
||||||
|
|
||||||
return "_" + match.Value.ToLower();
|
return "_" + match.Value.ToLower();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@@ -332,5 +337,8 @@ namespace Content.Server.Database
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[GeneratedRegex("[A-Z]+", RegexOptions.Compiled)]
|
||||||
|
private static partial Regex UpperCaseLocator();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user