From eb1523cc8153b71f81dcd29577c2b704d81ed4bb Mon Sep 17 00:00:00 2001 From: Richard Ockerby Date: Mon, 24 Apr 2023 11:38:21 +0100 Subject: [PATCH] Alter the datatype of the UmbracoKeyValue value column (#14085) --- .../Migrations/Upgrade/UmbracoPlan.cs | 3 ++ .../Upgrade/V_11_4_0/AlterKeyValueDataType.cs | 37 +++++++++++++++++++ .../Persistence/Dtos/KeyValueDto.cs | 3 ++ 3 files changed, 43 insertions(+) create mode 100644 src/Umbraco.Infrastructure/Migrations/Upgrade/V_11_4_0/AlterKeyValueDataType.cs diff --git a/src/Umbraco.Infrastructure/Migrations/Upgrade/UmbracoPlan.cs b/src/Umbraco.Infrastructure/Migrations/Upgrade/UmbracoPlan.cs index 0206442897..c6fbe0557a 100644 --- a/src/Umbraco.Infrastructure/Migrations/Upgrade/UmbracoPlan.cs +++ b/src/Umbraco.Infrastructure/Migrations/Upgrade/UmbracoPlan.cs @@ -67,5 +67,8 @@ public class UmbracoPlan : MigrationPlan // To 11.3.0 To("{BB3889ED-E2DE-49F2-8F71-5FD8616A2661}"); + + // To 11.4.0 + To("{FFB6B9B0-F1A8-45E9-9CD7-25700577D1CA}"); } } diff --git a/src/Umbraco.Infrastructure/Migrations/Upgrade/V_11_4_0/AlterKeyValueDataType.cs b/src/Umbraco.Infrastructure/Migrations/Upgrade/V_11_4_0/AlterKeyValueDataType.cs new file mode 100644 index 0000000000..413711ad1d --- /dev/null +++ b/src/Umbraco.Infrastructure/Migrations/Upgrade/V_11_4_0/AlterKeyValueDataType.cs @@ -0,0 +1,37 @@ +using System.Reflection; +using NPoco; +using Umbraco.Cms.Infrastructure.Persistence; +using Umbraco.Cms.Infrastructure.Persistence.Dtos; + +namespace Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_11_4_0; + +public class AlterKeyValueDataType : MigrationBase +{ + private readonly IMigrationContext _context; + + public AlterKeyValueDataType(IMigrationContext context) + : base(context) => _context = context; + + protected override void Migrate() + { + // SQLite doesn't need this upgrade + if (_context.Database.DatabaseType.IsSqlite()) + { + return; + } + + string tableName = KeyValueDto.TableName; + string colName = "value"; + + // Determine the current datatype of the column within the database + string colDataType = Database.ExecuteScalar($"SELECT TOP(1) CHARACTER_MAXIMUM_LENGTH FROM INFORMATION_SCHEMA.COLUMNS" + + $" WHERE TABLE_NAME = '{tableName}' AND COLUMN_NAME = '{colName}'"); + + // 255 is the old length, -1 indicate MAX length + if (colDataType == "255") + { + // Upgrade to MAX length + Database.Execute($"ALTER TABLE {tableName} ALTER COLUMN {colName} nvarchar(MAX)"); + } + } +} diff --git a/src/Umbraco.Infrastructure/Persistence/Dtos/KeyValueDto.cs b/src/Umbraco.Infrastructure/Persistence/Dtos/KeyValueDto.cs index 5576b5ca43..5c985a0174 100644 --- a/src/Umbraco.Infrastructure/Persistence/Dtos/KeyValueDto.cs +++ b/src/Umbraco.Infrastructure/Persistence/Dtos/KeyValueDto.cs @@ -11,12 +11,15 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Dtos; [ExplicitColumns] internal class KeyValueDto { + public const string TableName = Constants.DatabaseSchema.Tables.KeyValue; + [Column("key")] [Length(256)] [PrimaryKeyColumn(AutoIncrement = false, Clustered = true)] public string Key { get; set; } = null!; [Column("value")] + [SpecialDbType(SpecialDbTypes.NVARCHARMAX)] [NullSetting(NullSetting = NullSettings.Null)] public string? Value { get; set; }