From f99f821a6dd9a4e7c3dcefd55bfffb1e9a2f86ae Mon Sep 17 00:00:00 2001 From: Sven Geusens Date: Wed, 21 Aug 2024 09:07:09 +0200 Subject: [PATCH] Fix Mismatching constraint names in old migration (#16891) * Find the constraint name based on table,column,type name instead of hardcoding it * removed unnecesary using * Check constraint rename seperatly from column rename --- .../Upgrade/V_13_3_0/AlignUpgradedDatabase.cs | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/Umbraco.Infrastructure/Migrations/Upgrade/V_13_3_0/AlignUpgradedDatabase.cs b/src/Umbraco.Infrastructure/Migrations/Upgrade/V_13_3_0/AlignUpgradedDatabase.cs index 84171e8717..6ee48ce0e7 100644 --- a/src/Umbraco.Infrastructure/Migrations/Upgrade/V_13_3_0/AlignUpgradedDatabase.cs +++ b/src/Umbraco.Infrastructure/Migrations/Upgrade/V_13_3_0/AlignUpgradedDatabase.cs @@ -120,23 +120,48 @@ public class AlignUpgradedDatabase : MigrationBase // We need to do this to ensure we don't try to rename the constraint if it doesn't exist. const string tableName = "umbracoContentVersion"; const string columnName = "VersionDate"; + const string newColumnName = "versionDate"; + const string expectedConstraintName = "DF_umbracoContentVersion_versionDate"; + ColumnInfo? versionDateColumn = columns .FirstOrDefault(x => x is { TableName: tableName, ColumnName: columnName }); - if (versionDateColumn is null) + // we only want to rename the column if necessary + if (versionDateColumn is not null) { // The column was not found I.E. the column is correctly named - return; + RenameColumn(tableName, columnName, newColumnName, columns); } - RenameColumn(tableName, columnName, "versionDate", columns); - // Renames the default constraint for the column, // apparently the content version table used to be prefixed with cms and not umbraco // We don't have a fluid way to rename the default constraint so we have to use raw SQL // This should be okay though since we are only running this migration on SQL Server + Sql constraintNameQuery = Database.SqlContext.Sql(@$" +SELECT obj_Constraint.NAME AS 'constraintName' + FROM sys.objects obj_table + JOIN sys.objects obj_Constraint + ON obj_table.object_id = obj_Constraint.parent_object_id + JOIN sys.sysconstraints constraints + ON constraints.constid = obj_Constraint.object_id + JOIN sys.columns columns + ON columns.object_id = obj_table.object_id + AND columns.column_id = constraints.colid + WHERE obj_table.NAME = '{tableName}' + AND columns.NAME = '{newColumnName}' + AND obj_Constraint.type = 'D' +"); + var currentConstraintName = Database.ExecuteScalar(constraintNameQuery); + + + // only rename the constraint if necessary + if (currentConstraintName == expectedConstraintName) + { + return; + } + Sql renameConstraintQuery = Database.SqlContext.Sql( - "EXEC sp_rename N'DF_cmsContentVersion_VersionDate', N'DF_umbracoContentVersion_versionDate', N'OBJECT'"); + $"EXEC sp_rename N'{currentConstraintName}', N'{expectedConstraintName}', N'OBJECT'"); Database.Execute(renameConstraintQuery); }