diff --git a/src/Umbraco.Core/Models/Rdbms/DictionaryDto.cs b/src/Umbraco.Core/Models/Rdbms/DictionaryDto.cs index 712d1937a9..1e1940ad8d 100644 --- a/src/Umbraco.Core/Models/Rdbms/DictionaryDto.cs +++ b/src/Umbraco.Core/Models/Rdbms/DictionaryDto.cs @@ -24,7 +24,8 @@ namespace Umbraco.Core.Models.Rdbms public Guid? Parent { get; set; } [Column("key")] - [Length(1000)] + [Length(450)] + [Index(IndexTypes.NonClustered, Name = "IX_cmsDictionary_key")] public string Key { get; set; } [ResultColumn] diff --git a/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenSevenZero/AddIndexToDictionaryKeyColumn.cs b/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenSevenZero/AddIndexToDictionaryKeyColumn.cs new file mode 100644 index 0000000000..8d708a18ea --- /dev/null +++ b/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenSevenZero/AddIndexToDictionaryKeyColumn.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Remoting.Contexts; +using System.Text; +using System.Threading.Tasks; +using Umbraco.Core.Logging; +using Umbraco.Core.Persistence.SqlSyntax; + +namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSevenSevenZero +{ + [Migration("7.7.0", 5, Constants.System.UmbracoMigrationName)] + public class AddIndexToDictionaryKeyColumn : MigrationBase + { + public AddIndexToDictionaryKeyColumn(ISqlSyntaxProvider sqlSyntax, ILogger logger) + : base(sqlSyntax, logger) + { } + + public override void Up() + { + Execute.Code(database => + { + //Now we need to check if we can actually do this because we won't be able to if there's data in there that is too long + var colLen = (SqlSyntax is MySqlSyntaxProvider) + ? database.ExecuteScalar(string.Format("select max(LENGTH({0})) from cmsDictionary", SqlSyntax.GetQuotedColumnName("key"))) + : database.ExecuteScalar(string.Format("select max(datalength({0})) from cmsDictionary", SqlSyntax.GetQuotedColumnName("key"))); + + if (colLen < 900 == false && colLen != null) + { + return null; + } + + var dbIndexes = SqlSyntax.GetDefinedIndexesDefinitions(Context.Database); + + //make sure it doesn't already exist + if (dbIndexes.Any(x => x.IndexName.InvariantEquals("IX_cmsDictionary_key")) == false) + { + var localContext = new LocalMigrationContext(Context.CurrentDatabaseProvider, database, SqlSyntax, Logger); + + //we can apply the index + localContext.Create.Index("IX_cmsDictionary_key").OnTable("cmsDictionary") + .OnColumn("key") + .Ascending() + .WithOptions() + .NonClustered(); + + return localContext.GetSql(); + } + + return null; + + }); + + + } + + public override void Down() + { + Delete.Index("IX_cmsDictionary_key").OnTable("cmsDictionary"); + } + + } +} diff --git a/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenSevenZero/ReduceDictionaryKeyColumnsSize.cs b/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenSevenZero/ReduceDictionaryKeyColumnsSize.cs new file mode 100644 index 0000000000..a707e4687a --- /dev/null +++ b/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenSevenZero/ReduceDictionaryKeyColumnsSize.cs @@ -0,0 +1,51 @@ +using System.Linq; +using Umbraco.Core.Logging; +using Umbraco.Core.Persistence.SqlSyntax; + +namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSevenSevenZero +{ + [Migration("7.7.0", 4, Constants.System.UmbracoMigrationName)] + public class ReduceDictionaryKeyColumnsSize : MigrationBase + { + public ReduceDictionaryKeyColumnsSize(ISqlSyntaxProvider sqlSyntax, ILogger logger) + : base(sqlSyntax, logger) + { } + + public override void Up() + { + //Now we need to check if we can actually do this because we won't be able to if there's data in there that is too long + + Execute.Code(database => + { + var dbIndexes = SqlSyntax.GetDefinedIndexesDefinitions(database); + + var colLen = (SqlSyntax is MySqlSyntaxProvider) + ? database.ExecuteScalar(string.Format("select max(LENGTH({0})) from cmsDictionary", SqlSyntax.GetQuotedColumnName("key"))) + : database.ExecuteScalar(string.Format("select max(datalength({0})) from cmsDictionary", SqlSyntax.GetQuotedColumnName("key"))); + + if (colLen < 900 == false) return null; + + var localContext = new LocalMigrationContext(Context.CurrentDatabaseProvider, database, SqlSyntax, Logger); + + //if it exists we need to drop it first + if (dbIndexes.Any(x => x.IndexName.InvariantEquals("IX_cmsDictionary_key"))) + { + localContext.Delete.Index("IX_cmsDictionary_key").OnTable("cmsDictionary"); + } + + //we can apply the col length change + localContext.Alter.Table("cmsDictionary") + .AlterColumn("key") + .AsString(450) + .NotNullable(); + + return localContext.GetSql(); + }); + } + + public override void Down() + { + + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index d5a9ea33cc..5aa2383237 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -500,6 +500,8 @@ + +