Skip to content

Commit 785168c

Browse files
Persistence Models: DTO attributes fixes (#21670)
* quote table, column and alias names with SqlSyntaxProvider methods in raw sql * refactoring private methods into new file as internal methods, refactor new extensions into another file * refactor GetAlias method * Double check the change * improve code health * change new static classes into public static partial class NPocoSqlExtensions * resolve some Copilot review suggestions * revert Copilot suggestion because it decreases code health * revert test * compare in with LOWER, change two methods from private to protected in UmbracoDatabaseFactory * revert Query.cs in this PR * Refactor for code health and fixing raw sql * divers small issues fixed * refactor two methods to respect the DRY pricipal * update IQuery interface * clean up * revert refactoring for CodeScene * delete obsolete Test * rename method * remove new methods and updates, which are not relevat for this PR * prepare for additional states in the future * don't mix string building methods * fix SQL injection danger * fix test for reverted methods * another SqlSyntax issue * fix update * fix reverted changes * restore change for this PR * restore change for this PR * fix merge bug * update formating * extend ISqlSytax for database independent autoIkrement feature * fix DTOs, extend ISqlSyntax * fix tests * revert * updates * diverse SqlSyntax and NPoco related updates for custom databse providers * fix names * fix PrimaryKey for multi columns * Resolve the issues with SqlSyntaxProvider for SQLite. If executed correctly, a single test would reveal the problem. * add another test * revert changes which causes even more issues * fix column const naming * ensure column const names from v17.2 * add comment for change * Update src/Umbraco.Infrastructure/Persistence/Dtos/ContentTypeAllowedContentTypeDto.cs Co-authored-by: Andy Butland <abutland73@gmail.com> * Update src/Umbraco.Infrastructure/Persistence/Dtos/ContentTypeTemplateDto.cs Co-authored-by: Andy Butland <abutland73@gmail.com> * resolve review comments * Make ReferenceMemberName consistent across all DTOs (use constants defined on the referenced DTO). * Ensure [ExplicitColumns] attribute exists on all DTOs. * Ensure we consistently use PrimaryKeyColumnName over PrimaryKeyName. * Fix further inconsistency to use only TemplateNodeIdColumnName. * Fixed trailing whitespace. * Restored primary key constraint name on ContentVersionCleanupPolicyDto (it doesn't seem in scope of PR to remove this). * Removed the confusing PrimaryKeyColumnName constants for multi-column primary key DTOs where the constant refers to only one of the key columns. * ReferenceMemberName needs to be a C# property name, so it's safer to use nameof. * Comment fix. * Amended accessibility modifiers. * Fixed/tidied comments. * Fixed references from UserGroupDto. --------- Co-authored-by: Andy Butland <abutland73@gmail.com>
1 parent 8ace1e0 commit 785168c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+113
-118
lines changed

src/Umbraco.Infrastructure/Persistence/Dtos/AccessDto.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ internal sealed class AccessDto
3939
public DateTime UpdateDate { get; set; }
4040

4141
[ResultColumn]
42-
[Reference(ReferenceType.Many, ReferenceMemberName = AccessRuleDto.AccessIdColumnName)]
42+
[Reference(ReferenceType.Many, ReferenceMemberName = nameof(AccessRuleDto.AccessId))]
4343
public List<AccessRuleDto> Rules { get; set; } = new();
4444
}

src/Umbraco.Infrastructure/Persistence/Dtos/AccessRuleDto.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal sealed class AccessRuleDto
1313
public const string TableName = Constants.DatabaseSchema.Tables.AccessRule;
1414
public const string PrimaryKeyColumnName = Constants.DatabaseSchema.Columns.PrimaryKeyNameId;
1515

16-
internal const string AccessIdColumnName = "accessId";
16+
private const string AccessIdColumnName = "accessId";
1717

1818
private const string RuleValueColumnName = "ruleValue";
1919
private const string RuleTypeColumnName = "ruleType";

src/Umbraco.Infrastructure/Persistence/Dtos/ContentDto.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ public class ContentDto
1313
public const string PrimaryKeyColumnName = Constants.DatabaseSchema.Columns.NodeIdName;
1414
public const string ContentTypeIdColumnName = "contentTypeId";
1515

16-
internal const string ReferenceMemberName = "NodeId"; // should be ContentVersionDto.NodeIdColumnName, but for database compatibility we keep it like this
17-
1816
[Column(PrimaryKeyColumnName)]
1917
[PrimaryKeyColumn(AutoIncrement = false)]
2018
[ForeignKey(typeof(NodeDto))]
@@ -32,6 +30,6 @@ public class ContentDto
3230
// they can only be loaded one by one (as several content),
3331
// so this here is a OneToOne reference
3432
[ResultColumn]
35-
[Reference(ReferenceType.OneToOne, ReferenceMemberName = ReferenceMemberName)]
33+
[Reference(ReferenceType.OneToOne, ReferenceMemberName = nameof(ContentVersionDto.NodeId))]
3634
public ContentVersionDto ContentVersionDto { get; set; } = null!;
3735
}

src/Umbraco.Infrastructure/Persistence/Dtos/ContentNuDto.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,27 @@
66
namespace Umbraco.Cms.Infrastructure.Persistence.Dtos;
77

88
[TableName(TableName)]
9-
[PrimaryKey(PrimaryKeyColumnName, AutoIncrement = false)]
9+
[PrimaryKey([NodeIdColumnName, PublishedColumnName], AutoIncrement = false)]
1010
[ExplicitColumns]
1111
public class ContentNuDto
1212
{
1313
public const string TableName = Constants.DatabaseSchema.Tables.NodeData;
14-
public const string PrimaryKeyColumnName = Constants.DatabaseSchema.Columns.NodeIdName;
14+
public const string NodeIdColumnName = Constants.DatabaseSchema.Columns.NodeIdName;
15+
16+
[Obsolete("Use NodeIdColumnName instead. Scheduled for removal in Umbraco 18.")]
17+
public const string PrimaryKeyColumnName = NodeIdColumnName;
1518

1619
private const string PublishedColumnName = "published";
1720
private const string RvColumnName = "rv";
1821
private const string DataRawColumnName = "dataRaw";
1922

20-
[Column(PrimaryKeyColumnName)]
21-
[PrimaryKeyColumn(AutoIncrement = false, Name = "PK_cmsContentNu", OnColumns = $"{PrimaryKeyColumnName}, {PublishedColumnName}")]
22-
[ForeignKey(typeof(ContentDto), Column = PrimaryKeyColumnName, OnDelete = Rule.Cascade)]
23+
[Column(NodeIdColumnName)]
24+
[PrimaryKeyColumn(AutoIncrement = false, Name = "PK_cmsContentNu", OnColumns = $"{NodeIdColumnName}, {PublishedColumnName}")]
25+
[ForeignKey(typeof(ContentDto), Column = ContentDto.PrimaryKeyColumnName, OnDelete = Rule.Cascade)]
2326
public int NodeId { get; set; }
2427

2528
[Column(PublishedColumnName)]
26-
[Index(IndexTypes.NonClustered, Name = "IX_" + TableName + "_" + PublishedColumnName, ForColumns = $"{PublishedColumnName},{PrimaryKeyColumnName},{RvColumnName}", IncludeColumns = DataRawColumnName)]
29+
[Index(IndexTypes.NonClustered, Name = "IX_" + TableName + "_" + PublishedColumnName, ForColumns = $"{PublishedColumnName},{NodeIdColumnName},{RvColumnName}", IncludeColumns = DataRawColumnName)]
2730
public bool Published { get; set; }
2831

2932
/// <summary>

src/Umbraco.Infrastructure/Persistence/Dtos/ContentType2ContentTypeDto.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
namespace Umbraco.Cms.Infrastructure.Persistence.Dtos;
66

77
[TableName(TableName)]
8+
[PrimaryKey([ParentIdColumnName, ChildIdColumnName], AutoIncrement = false)]
89
[ExplicitColumns]
910
internal sealed class ContentType2ContentTypeDto
1011
{
1112
public const string TableName = Constants.DatabaseSchema.Tables.ContentTypeTree;
12-
public const string PrimaryKeyColumnName = "parentContentTypeId";
13+
public const string ParentIdColumnName = "parentContentTypeId";
1314
public const string ChildIdColumnName = "childContentTypeId";
1415

15-
[Column(PrimaryKeyColumnName)]
16-
[PrimaryKeyColumn(AutoIncrement = false, Clustered = true, Name = "PK_cmsContentType2ContentType", OnColumns = $"{PrimaryKeyColumnName}, {ChildIdColumnName}")]
16+
[Column(ParentIdColumnName)]
17+
[PrimaryKeyColumn(AutoIncrement = false, Clustered = true, Name = "PK_cmsContentType2ContentType", OnColumns = $"{ParentIdColumnName}, {ChildIdColumnName}")]
1718
[ForeignKey(typeof(NodeDto), Name = "FK_cmsContentType2ContentType_umbracoNode_parent")]
1819
public int ParentId { get; set; }
1920

src/Umbraco.Infrastructure/Persistence/Dtos/ContentTypeAllowedContentTypeDto.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,27 @@
55
namespace Umbraco.Cms.Infrastructure.Persistence.Dtos;
66

77
[TableName(TableName)]
8-
[PrimaryKey(PrimaryKeyColumnName, AutoIncrement = false)]
8+
[PrimaryKey([IdKeyColumnName, AllowedIdColumnName], AutoIncrement = false)]
99
[ExplicitColumns]
1010
internal sealed class ContentTypeAllowedContentTypeDto
1111
{
1212
public const string TableName = Constants.DatabaseSchema.Tables.ContentChildType;
1313

14-
// To avoid any risk of casing bugs caused by inconsistencies between upgraded and new installs, we keep the casing "Id" here even though in other tables the usual casing is lower-case ("id").
15-
public const string PrimaryKeyColumnName = "Id";
14+
// To avoid any risk of casing bugs caused by inconsistencies between upgraded and new installs, we keep the casing "Id" here
15+
// even though in other tables the usual casing is lower-case ("id").
16+
public const string IdKeyColumnName = "Id";
17+
1618
public const string NodeIdColumnName = Constants.DatabaseSchema.Columns.NodeIdName;
1719
public const string SortOrderColumnName = "SortOrder";
1820
public const string AllowedIdColumnName = "AllowedId";
1921

20-
[Column(PrimaryKeyColumnName)]
21-
[ForeignKey(typeof(ContentTypeDto), Name = "FK_cmsContentTypeAllowedContentType_cmsContentType", Column = NodeIdColumnName)]
22-
[PrimaryKeyColumn(AutoIncrement = false, Clustered = true, Name = "PK_cmsContentTypeAllowedContentType", OnColumns = $"{PrimaryKeyColumnName}, {AllowedIdColumnName}")]
22+
[Column(IdKeyColumnName)]
23+
[ForeignKey(typeof(ContentTypeDto), Name = "FK_cmsContentTypeAllowedContentType_cmsContentType", Column = ContentTypeDto.NodeIdColumnName)]
24+
[PrimaryKeyColumn(AutoIncrement = false, Clustered = true, Name = "PK_cmsContentTypeAllowedContentType", OnColumns = $"{IdKeyColumnName}, {AllowedIdColumnName}")]
2325
public int Id { get; set; }
2426

2527
[Column(AllowedIdColumnName)]
26-
[ForeignKey(typeof(ContentTypeDto), Name = "FK_cmsContentTypeAllowedContentType_cmsContentType1", Column = NodeIdColumnName)]
28+
[ForeignKey(typeof(ContentTypeDto), Name = "FK_cmsContentTypeAllowedContentType_cmsContentType1", Column = ContentTypeDto.NodeIdColumnName)]
2729
public int AllowedId { get; set; }
2830

2931
[Column(SortOrderColumnName)]

src/Umbraco.Infrastructure/Persistence/Dtos/ContentTypeDto.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ internal class ContentTypeDto
1313
public const string PrimaryKeyColumnName = Constants.DatabaseSchema.Columns.PrimaryKeyNamePk;
1414
public const string NodeIdColumnName = Constants.DatabaseSchema.Columns.NodeIdName;
1515

16-
internal const string ReferenceColumnName = "NodeId"; // should be ContentTypeDto.NodeIdColumnName, but for database compatibility we keep it like this
17-
1816
private string? _alias;
1917

2018
// Public constants to bind properties between DTOs
@@ -64,6 +62,6 @@ internal class ContentTypeDto
6462
public byte Variations { get; set; }
6563

6664
[ResultColumn]
67-
[Reference(ReferenceType.OneToOne, ColumnName = ReferenceColumnName)]
65+
[Reference(ReferenceType.OneToOne, ColumnName = nameof(NodeId))]
6866
public NodeDto NodeDto { get; set; } = null!;
6967
}

src/Umbraco.Infrastructure/Persistence/Dtos/ContentTypeTemplateDto.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55
namespace Umbraco.Cms.Infrastructure.Persistence.Dtos;
66

77
[TableName(TableName)]
8-
[PrimaryKey(PrimaryKeyName, AutoIncrement = false)]
8+
[PrimaryKey([ContentTypeNodeIdColumnName, TemplateNodeIdColumnName], AutoIncrement = false)]
99
[ExplicitColumns]
1010
internal sealed class ContentTypeTemplateDto
1111
{
1212
public const string TableName = Constants.DatabaseSchema.Tables.DocumentType;
13-
public const string PrimaryKeyName = "contentTypeNodeId";
14-
public const string TemplateNodeIdName = "templateNodeId";
13+
public const string TemplateNodeIdColumnName = "templateNodeId";
14+
public const string ContentTypeNodeIdColumnName = "contentTypeNodeId";
1515

16-
[Column(PrimaryKeyName)]
17-
[PrimaryKeyColumn(AutoIncrement = false, Name = "PK_cmsDocumentType", OnColumns = $"{PrimaryKeyName}, {TemplateNodeIdName}")]
16+
[Column(ContentTypeNodeIdColumnName)]
17+
[PrimaryKeyColumn(AutoIncrement = false, Name = "PK_cmsDocumentType", OnColumns = $"{ContentTypeNodeIdColumnName}, {TemplateNodeIdColumnName}")]
1818
[ForeignKey(typeof(ContentTypeDto), Column = ContentTypeDto.NodeIdColumnName)]
1919
[ForeignKey(typeof(NodeDto))]
2020
public int ContentTypeNodeId { get; set; }
2121

22-
[Column(TemplateNodeIdName)]
22+
[Column(TemplateNodeIdColumnName)]
2323
[ForeignKey(typeof(TemplateDto), Column = TemplateDto.NodeIdColumnName)]
2424
public int TemplateNodeId { get; set; }
2525

src/Umbraco.Infrastructure/Persistence/Dtos/ContentVersionCleanupPolicyDto.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
namespace Umbraco.Cms.Infrastructure.Persistence.Dtos;
66

77
[TableName(TableName)]
8-
[PrimaryKey(PrimaryKeyName, AutoIncrement = false)]
8+
[PrimaryKey(PrimaryKeyColumnName, AutoIncrement = false)]
99
[ExplicitColumns]
1010
internal sealed class ContentVersionCleanupPolicyDto
1111
{
1212
public const string TableName = Constants.DatabaseSchema.Tables.ContentVersionCleanupPolicy;
13-
public const string PrimaryKeyName = "contentTypeId";
13+
public const string PrimaryKeyColumnName = "contentTypeId";
1414

15-
[Column(PrimaryKeyName)]
15+
[Column(PrimaryKeyColumnName)]
1616
[PrimaryKeyColumn(AutoIncrement = false, Name = "PK_umbracoContentVersionCleanupPolicy")]
1717
[ForeignKey(typeof(ContentTypeDto), Column = ContentTypeDto.NodeIdColumnName)]
1818
public int ContentTypeId { get; set; }

src/Umbraco.Infrastructure/Persistence/Dtos/ContentVersionCultureVariationDto.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ internal sealed class ContentVersionCultureVariationDto
4444
[Column(UpdateDateColumnName)] // TODO: db rename to 'updateDate'
4545
public DateTime UpdateDate { get; set; }
4646

47-
[Column(UpdateUserIdColumnName)] // TODO: db rename to 'updateDate'
47+
[Column(UpdateUserIdColumnName)] // TODO: db rename to 'updateUserId'
4848
[ForeignKey(typeof(UserDto))]
4949
[NullSetting(NullSetting = NullSettings.Null)]
5050
public int? UpdateUserId

0 commit comments

Comments
 (0)