Skip to content

Commit a4c0da3

Browse files
committed
Migrations: Use reliable GUID to check for existence of data type when creating (#20604)
* Use reliable GUID to check for existence of data type in migration. * Retrieve just a single field in existence check.
1 parent 28c01d2 commit a4c0da3

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/Umbraco.Infrastructure/Migrations/Upgrade/V_16_3_0/MigrateMediaTypeLabelProperties.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
using Umbraco.Cms.Core.Models;
77
using Umbraco.Cms.Core.Services;
88
using Umbraco.Cms.Core.Services.OperationStatus;
9+
using Umbraco.Cms.Infrastructure.Persistence;
910
using Umbraco.Cms.Infrastructure.Persistence.Dtos;
11+
using Umbraco.Extensions;
1012

1113
namespace Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_16_3_0;
1214

@@ -69,7 +71,7 @@ private void ToggleIndentityInsertForNodes(bool toggleOn)
6971

7072
private void IfNotExistsCreateBytesLabel()
7173
{
72-
if (Database.Exists<NodeDto>(Constants.DataTypes.LabelBytes))
74+
if (NodeExists(_labelBytesDataTypeKey))
7375
{
7476
return;
7577
}
@@ -89,7 +91,7 @@ private void IfNotExistsCreateBytesLabel()
8991
CreateDate = DateTime.Now,
9092
};
9193

92-
_ = Database.Insert(Constants.DatabaseSchema.Tables.Node, "id", false, nodeDto);
94+
Database.Insert(Constants.DatabaseSchema.Tables.Node, "id", false, nodeDto);
9395

9496
var dataTypeDto = new DataTypeDto
9597
{
@@ -100,12 +102,12 @@ private void IfNotExistsCreateBytesLabel()
100102
Configuration = "{\"umbracoDataValueType\":\"BIGINT\", \"labelTemplate\":\"{=value | bytes}\"}",
101103
};
102104

103-
_ = Database.Insert(Constants.DatabaseSchema.Tables.DataType, "pk", false, dataTypeDto);
105+
Database.Insert(Constants.DatabaseSchema.Tables.DataType, "pk", false, dataTypeDto);
104106
}
105107

106108
private void IfNotExistsCreatePixelsLabel()
107109
{
108-
if (Database.Exists<NodeDto>(Constants.DataTypes.LabelPixels))
110+
if (NodeExists(_labelPixelsDataTypeKey))
109111
{
110112
return;
111113
}
@@ -125,7 +127,7 @@ private void IfNotExistsCreatePixelsLabel()
125127
CreateDate = DateTime.Now,
126128
};
127129

128-
_ = Database.Insert(Constants.DatabaseSchema.Tables.Node, "id", false, nodeDto);
130+
Database.Insert(Constants.DatabaseSchema.Tables.Node, "id", false, nodeDto);
129131

130132
var dataTypeDto = new DataTypeDto
131133
{
@@ -136,7 +138,16 @@ private void IfNotExistsCreatePixelsLabel()
136138
Configuration = "{\"umbracoDataValueType\":\"INT\", \"labelTemplate\":\"{=value}px\"}",
137139
};
138140

139-
_ = Database.Insert(Constants.DatabaseSchema.Tables.DataType, "pk", false, dataTypeDto);
141+
Database.Insert(Constants.DatabaseSchema.Tables.DataType, "pk", false, dataTypeDto);
142+
}
143+
144+
private bool NodeExists(Guid uniqueId)
145+
{
146+
Sql<ISqlContext> sql = Database.SqlContext.Sql()
147+
.Select<NodeDto>(x => x.NodeId)
148+
.From<NodeDto>()
149+
.Where<NodeDto>(x => x.UniqueId == uniqueId);
150+
return Database.FirstOrDefault<NodeDto>(sql) is not null;
140151
}
141152

142153
private async Task MigrateMediaTypeLabels()

0 commit comments

Comments
 (0)