-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Migrations: Create missing tabs on content types when referenced by both composition and content type groups (closes #20058) #20303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lauraneto
merged 7 commits into
main
from
v16/bugfix/migration-from-v13-moving-groups-to-the-generic-tab
Oct 1, 2025
+401
−2
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
849fdf0
Add migration to create missing tabs
lauraneto 57b475c
Small fixes
lauraneto 439bd3a
Merge branch 'main' into v16/bugfix/migration-from-v13-moving-groups-…
lauraneto cac4481
WIP: Integration test.
AndyButland 912f973
Added asserts to show the current issue with the integration test.
AndyButland 1cb7506
Adjusted the integration test
lauraneto 9eab650
Added logging of result. Minor re-order and extraction refactoring in…
AndyButland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/Umbraco.Infrastructure/Migrations/Upgrade/V_16_4_0/CreateMissingTabs.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
using NPoco; | ||
using Umbraco.Cms.Core.Scoping; | ||
using Umbraco.Cms.Infrastructure.Persistence; | ||
using Umbraco.Cms.Infrastructure.Persistence.Dtos; | ||
using Umbraco.Extensions; | ||
|
||
namespace Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_16_4_0; | ||
|
||
/// <summary> | ||
/// Creates missing tabs on content types when a tab is referenced by both a composition and the content type's own groups. | ||
/// </summary> | ||
/// <remarks> | ||
/// In v13, if a tab had groups in both a composition and the content type, the tab might not exist on the content type itself. | ||
/// Newer versions require such tabs to also exist directly on the content type. This migration ensures those tabs are created. | ||
/// </remarks> | ||
[Obsolete("Remove in Umbraco 18.")] | ||
public class CreateMissingTabs : UnscopedAsyncMigrationBase | ||
{ | ||
private readonly IScopeProvider _scopeProvider; | ||
|
||
public CreateMissingTabs(IMigrationContext context, IScopeProvider scopeProvider) | ||
: base(context) => _scopeProvider = scopeProvider; | ||
|
||
protected override async Task MigrateAsync() | ||
{ | ||
using IScope scope = _scopeProvider.CreateScope(); | ||
|
||
// 1. Find all property groups (type 0) and extract their tab alias (the part before the first '/'). | ||
// This helps identify which groups are referencing tabs. | ||
Sql<ISqlContext> groupsSql = Database.SqlContext.Sql() | ||
.SelectDistinct<PropertyTypeGroupDto>("g", pt => pt.ContentTypeNodeId) | ||
.AndSelect(GetTabAliasQuery("g.alias") + " AS tabAlias") | ||
.From<PropertyTypeDto>(alias: "p") | ||
.InnerJoin<PropertyTypeGroupDto>(alias: "g").On<PropertyTypeDto, PropertyTypeGroupDto>( | ||
(pt, ptg) => pt.PropertyTypeGroupId == ptg.Id && pt.ContentTypeId == ptg.ContentTypeNodeId, | ||
aliasLeft: "p", | ||
"g") | ||
.Where<PropertyTypeGroupDto>(x => x.Type == 0, alias: "g") | ||
.Where(CheckContainsTabAliasQuery("g.alias")); | ||
|
||
// 2. Get all existing tabs (type 1) for all content types. | ||
Sql<ISqlContext> tabsSql = Database.SqlContext.Sql() | ||
.Select<PropertyTypeGroupDto>("g2", g => g.UniqueId, g => g.ContentTypeNodeId, g => g.Alias) | ||
.From<PropertyTypeGroupDto>(alias: "g2") | ||
.Where<PropertyTypeGroupDto>(x => x.Type == 1, alias: "g2"); | ||
|
||
// 3. Identify groups that reference a tab alias which does not exist as a tab for their content type. | ||
// These are the "missing tabs" that need to be created. | ||
Sql<ISqlContext> missingTabsSql = Database.SqlContext.Sql() | ||
.Select<PropertyTypeGroupDto>("groups", g => g.ContentTypeNodeId) | ||
.AndSelect("groups.tabAlias") | ||
.From() | ||
.AppendSubQuery(groupsSql, "groups") | ||
.LeftJoin(tabsSql, "tabs") | ||
.On("groups.ContentTypeNodeId = tabs.ContentTypeNodeId AND tabs.alias = groups.tabAlias") | ||
.WhereNull<PropertyTypeGroupDto>(ptg => ptg.UniqueId, "tabs"); | ||
|
||
// 4. For each missing tab, find the corresponding tab details (text, alias, sort order) | ||
// from the parent content type (composition) that already has this tab. | ||
Sql<ISqlContext> missingTabsWithDetailsSql = Database.SqlContext.Sql() | ||
.SelectDistinct<PropertyTypeGroupDto>("missingTabs", ptg => ptg.ContentTypeNodeId) | ||
.AndSelect<PropertyTypeGroupDto>("tg", ptg => ptg.Text, ptg => ptg.Alias, ptg => ptg.SortOrder) | ||
.From() | ||
.AppendSubQuery(missingTabsSql, "missingTabs") | ||
.InnerJoin<ContentType2ContentTypeDto>(alias: "ct2ct") | ||
.On<PropertyTypeGroupDto, ContentType2ContentTypeDto>( | ||
(ptg, ct2Ct) => ptg.ContentTypeNodeId == ct2Ct.ChildId, | ||
"missingTabs", | ||
"ct2ct") | ||
.InnerJoin<PropertyTypeGroupDto>(alias: "tg") | ||
.On<ContentType2ContentTypeDto, PropertyTypeGroupDto>( | ||
(ct2Ct, ptg) => ct2Ct.ParentId == ptg.ContentTypeNodeId, | ||
"ct2ct", | ||
"tg") | ||
.Append("AND tg.alias = missingTabs.tabAlias"); | ||
|
||
List<MissingTabWithDetails> missingTabsWithDetails = | ||
await Database.FetchAsync<MissingTabWithDetails>(missingTabsWithDetailsSql); | ||
|
||
// 5. Create and insert new tab records for each missing tab, using the details from the parent/composition. | ||
IEnumerable<PropertyTypeGroupDto> newTabs = missingTabsWithDetails | ||
.Select(missingTabWithDetails => new PropertyTypeGroupDto | ||
{ | ||
UniqueId = Guid.CreateVersion7(), | ||
ContentTypeNodeId = missingTabWithDetails.ContentTypeNodeId, | ||
Type = 1, | ||
Text = missingTabWithDetails.Text, | ||
Alias = missingTabWithDetails.Alias, | ||
SortOrder = missingTabWithDetails.SortOrder, | ||
}); | ||
await Database.InsertBatchAsync(newTabs); | ||
|
||
// 6. Complete the migration and commit the transaction. | ||
Context.Complete(); | ||
scope.Complete(); | ||
} | ||
|
||
private string GetTabAliasQuery(string columnName) => | ||
DatabaseType == DatabaseType.SQLite | ||
? $"substr({columnName}, 0, INSTR({columnName},'/'))" | ||
: $"SUBSTRING({columnName}, 1, CHARINDEX('/', {columnName}) - 1)"; | ||
|
||
private string CheckContainsTabAliasQuery(string columnName) => | ||
DatabaseType == DatabaseType.SQLite | ||
? $"INSTR({columnName}, '/') > 0" | ||
: $"CHARINDEX('/', {columnName}) > 0"; | ||
|
||
private class MissingTabWithDetails | ||
{ | ||
public required int ContentTypeNodeId { get; set; } | ||
|
||
public required string Alias { get; set; } | ||
|
||
public required string Text { get; set; } | ||
|
||
public required int SortOrder { get; set; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.