Merge pull request #1411 from umbraco/temp-U4-8808
U4-8808 Fixing up 301 redirects
This commit is contained in:
@@ -5,7 +5,7 @@ using Umbraco.Core.Persistence.DatabaseAnnotations;
|
||||
namespace Umbraco.Core.Models.Rdbms
|
||||
{
|
||||
[TableName("umbracoRedirectUrl")]
|
||||
[PrimaryKey("id")]
|
||||
[PrimaryKey("id", autoIncrement = false)]
|
||||
[ExplicitColumns]
|
||||
class RedirectUrlDto
|
||||
{
|
||||
@@ -22,8 +22,8 @@ namespace Umbraco.Core.Models.Rdbms
|
||||
// inserts, and much faster on reads, so... we have an index on a hash.
|
||||
|
||||
[Column("id")]
|
||||
[PrimaryKeyColumn(IdentitySeed = 1, Name = "PK_umbracoRedirectUrl")]
|
||||
public int Id { get; set; }
|
||||
[PrimaryKeyColumn(Name = "PK_umbracoRedirectUrl", AutoIncrement = false)]
|
||||
public Guid Id { get; set; }
|
||||
|
||||
[ResultColumn]
|
||||
public int ContentId { get; set; }
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using System.Linq;
|
||||
using Umbraco.Core.Configuration;
|
||||
using Umbraco.Core.Logging;
|
||||
using Umbraco.Core.Persistence.Migrations.Syntax.Create;
|
||||
using Umbraco.Core.Persistence.SqlSyntax;
|
||||
|
||||
namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSevenFiveZero
|
||||
@@ -21,24 +20,40 @@ namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSevenFiveZer
|
||||
|
||||
private string MigrationCode(Database database)
|
||||
{
|
||||
// don't execute if the table is already there
|
||||
var tables = SqlSyntax.GetTablesInSchema(database).ToArray();
|
||||
if (tables.InvariantContains("umbracoRedirectUrl")) return null;
|
||||
var umbracoRedirectUrlTableName = "umbracoRedirectUrl";
|
||||
|
||||
var localContext = new LocalMigrationContext(Context.CurrentDatabaseProvider, database, SqlSyntax, Logger);
|
||||
|
||||
localContext.Create.Table("umbracoRedirectUrl")
|
||||
.WithColumn("id").AsInt32().Identity().PrimaryKey("PK_umbracoRedirectUrl")
|
||||
.WithColumn("contentId").AsInt32().NotNullable()
|
||||
.WithColumn("createDateUtc").AsDateTime().NotNullable()
|
||||
.WithColumn("url").AsString(2048).NotNullable();
|
||||
var tables = SqlSyntax.GetTablesInSchema(database).ToArray();
|
||||
|
||||
if (tables.InvariantContains(umbracoRedirectUrlTableName))
|
||||
{
|
||||
var columns = SqlSyntax.GetColumnsInSchema(database).ToArray();
|
||||
if (columns.Any(x => x.TableName.InvariantEquals(umbracoRedirectUrlTableName) && x.ColumnName.InvariantEquals("id") && x.DataType == "uniqueidentifier"))
|
||||
return null;
|
||||
localContext.Delete.Table(umbracoRedirectUrlTableName);
|
||||
}
|
||||
|
||||
localContext.Create.Index("IX_umbracoRedirectUrl")
|
||||
.OnTable("umbracoRedirectUrl")
|
||||
.OnColumn("url").Ascending()
|
||||
.OnColumn("createDateUtc").Ascending()
|
||||
localContext.Create.Table(umbracoRedirectUrlTableName)
|
||||
.WithColumn("id").AsGuid().NotNullable().PrimaryKey("PK_" + umbracoRedirectUrlTableName)
|
||||
.WithColumn("createDateUtc").AsDateTime().NotNullable()
|
||||
.WithColumn("url").AsString(2048).NotNullable()
|
||||
.WithColumn("contentKey").AsGuid().NotNullable()
|
||||
.WithColumn("urlHash").AsString(20).NotNullable();
|
||||
|
||||
localContext.Create.Index("IX_" + umbracoRedirectUrlTableName).OnTable(umbracoRedirectUrlTableName)
|
||||
.OnColumn("urlHash")
|
||||
.Ascending()
|
||||
.OnColumn("contentKey")
|
||||
.Ascending()
|
||||
.OnColumn("createDateUtc")
|
||||
.Descending()
|
||||
.WithOptions().NonClustered();
|
||||
|
||||
localContext.Create.ForeignKey("FK_" + umbracoRedirectUrlTableName)
|
||||
.FromTable(umbracoRedirectUrlTableName).ForeignColumn("contentKey")
|
||||
.ToTable("umbracoNode").PrimaryColumn("uniqueID");
|
||||
|
||||
return localContext.GetSql();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
using System.Linq;
|
||||
using Umbraco.Core.Configuration;
|
||||
using Umbraco.Core.Logging;
|
||||
using Umbraco.Core.Persistence.Migrations.Syntax.Alter;
|
||||
using Umbraco.Core.Persistence.Migrations.Syntax.Create;
|
||||
using Umbraco.Core.Persistence.Migrations.Syntax.Delete;
|
||||
using Umbraco.Core.Persistence.Migrations.Syntax.Execute;
|
||||
using Umbraco.Core.Persistence.SqlSyntax;
|
||||
|
||||
namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSevenFiveZero
|
||||
{
|
||||
[Migration("7.5.0", 101, GlobalSettings.UmbracoMigrationName)]
|
||||
public class AddRedirectUrlTable2 : MigrationBase
|
||||
{
|
||||
public AddRedirectUrlTable2(ISqlSyntaxProvider sqlSyntax, ILogger logger)
|
||||
: base(sqlSyntax, logger)
|
||||
{ }
|
||||
|
||||
public override void Up()
|
||||
{
|
||||
// defer, because we are making decisions based upon what's in the database
|
||||
Execute.Code(MigrationCode);
|
||||
}
|
||||
|
||||
private string MigrationCode(Database database)
|
||||
{
|
||||
var columns = SqlSyntax.GetColumnsInSchema(database).ToArray();
|
||||
|
||||
if (columns.Any(x => x.TableName.InvariantEquals("umbracoRedirectUrl") && x.ColumnName.InvariantEquals("contentKey")))
|
||||
return null;
|
||||
|
||||
var localContext = new LocalMigrationContext(Context.CurrentDatabaseProvider, database, SqlSyntax, Logger);
|
||||
|
||||
localContext.Execute.Sql("DELETE FROM umbracoRedirectUrl"); // else cannot add non-nullable field
|
||||
|
||||
var keyConstraints = SqlSyntax.GetConstraintsPerColumn(database).Distinct();
|
||||
var fk= keyConstraints
|
||||
.SingleOrDefault(x => x.Item1 == "umbracoRedirectUrl" && x.Item2 == "contentId" && x.Item3.InvariantStartsWith("PK_") == false);
|
||||
if (fk != null)
|
||||
localContext.Delete.ForeignKey(fk.Item3).OnTable("umbracoRedirectUrl");
|
||||
localContext.Delete.Column("contentId").FromTable("umbracoRedirectUrl");
|
||||
|
||||
// SQL CE does not want to alter-add non-nullable columns ;-(
|
||||
// but it's OK to create as nullable then alter, go figure
|
||||
//localContext.Alter.Table("umbracoRedirectUrl")
|
||||
// .AddColumn("contentKey").AsGuid().NotNullable();
|
||||
localContext.Alter.Table("umbracoRedirectUrl")
|
||||
.AddColumn("contentKey").AsGuid().Nullable();
|
||||
localContext.Alter.Table("umbracoRedirectUrl")
|
||||
.AlterColumn("contentKey").AsGuid().NotNullable();
|
||||
|
||||
localContext.Create.ForeignKey("FK_umbracoRedirectUrl")
|
||||
.FromTable("umbracoRedirectUrl").ForeignColumn("contentKey")
|
||||
.ToTable("umbracoNode").PrimaryColumn("uniqueID");
|
||||
|
||||
return localContext.GetSql();
|
||||
}
|
||||
|
||||
public override void Down()
|
||||
{ }
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
using System.Linq;
|
||||
using Umbraco.Core.Configuration;
|
||||
using Umbraco.Core.Logging;
|
||||
using Umbraco.Core.Persistence.SqlSyntax;
|
||||
|
||||
namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSevenFiveZero
|
||||
{
|
||||
[Migration("7.5.0", 102, GlobalSettings.UmbracoMigrationName)]
|
||||
public class AddRedirectUrlTable3 : MigrationBase
|
||||
{
|
||||
public AddRedirectUrlTable3(ISqlSyntaxProvider sqlSyntax, ILogger logger)
|
||||
: base(sqlSyntax, logger)
|
||||
{ }
|
||||
|
||||
public override void Up()
|
||||
{
|
||||
// defer, because we are making decisions based upon what's in the database
|
||||
Execute.Code(MigrationCode);
|
||||
}
|
||||
private string MigrationCode(Database database)
|
||||
{
|
||||
var columns = SqlSyntax.GetColumnsInSchema(database).ToArray();
|
||||
|
||||
if (columns.Any(x => x.TableName.InvariantEquals("umbracoRedirectUrl") && x.ColumnName.InvariantEquals("hurl")))
|
||||
return null;
|
||||
|
||||
var localContext = new LocalMigrationContext(Context.CurrentDatabaseProvider, database, SqlSyntax, Logger);
|
||||
|
||||
localContext.Execute.Sql("DELETE FROM umbracoRedirectUrl"); // else cannot add non-nullable field
|
||||
|
||||
localContext.Delete.Index("IX_umbracoRedirectUrl").OnTable("umbracoRedirectUrl");
|
||||
|
||||
// SQL CE does not want to alter-add non-nullable columns ;-(
|
||||
// but it's OK to create as nullable then alter, go figure
|
||||
//localContext.Alter.Table("umbracoRedirectUrl")
|
||||
// .AddColumn("urlHash").AsString(16).NotNullable();
|
||||
localContext.Alter.Table("umbracoRedirectUrl")
|
||||
.AddColumn("hurl").AsString(16).Nullable();
|
||||
localContext.Alter.Table("umbracoRedirectUrl")
|
||||
.AlterColumn("hurl").AsString(16).NotNullable();
|
||||
|
||||
localContext.Create.Index("IX_umbracoRedirectUrl").OnTable("umbracoRedirectUrl")
|
||||
.OnColumn("hurl")
|
||||
.Ascending()
|
||||
.OnColumn("contentKey")
|
||||
.Ascending()
|
||||
.OnColumn("createDateUtc")
|
||||
.Descending()
|
||||
.WithOptions().NonClustered();
|
||||
|
||||
return localContext.GetSql();
|
||||
}
|
||||
|
||||
public override void Down()
|
||||
{ }
|
||||
}
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
using System.Linq;
|
||||
using Umbraco.Core.Configuration;
|
||||
using Umbraco.Core.Logging;
|
||||
using Umbraco.Core.Persistence.SqlSyntax;
|
||||
|
||||
namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSevenFiveZero
|
||||
{
|
||||
[Migration("7.5.0", 103, GlobalSettings.UmbracoMigrationName)]
|
||||
public class AddRedirectUrlTable4 : MigrationBase
|
||||
{
|
||||
public AddRedirectUrlTable4(ISqlSyntaxProvider sqlSyntax, ILogger logger)
|
||||
: base(sqlSyntax, logger)
|
||||
{ }
|
||||
|
||||
public override void Up()
|
||||
{
|
||||
// defer, because we are making decisions based upon what's in the database
|
||||
Execute.Code(MigrationCode);
|
||||
}
|
||||
private string MigrationCode(Database database)
|
||||
{
|
||||
var columns = SqlSyntax.GetColumnsInSchema(database).ToArray();
|
||||
|
||||
if (columns.Any(x => x.TableName.InvariantEquals("umbracoRedirectUrl") && x.ColumnName.InvariantEquals("urlHash")))
|
||||
return null;
|
||||
|
||||
var localContext = new LocalMigrationContext(Context.CurrentDatabaseProvider, database, SqlSyntax, Logger);
|
||||
|
||||
localContext.Execute.Sql("DELETE FROM umbracoRedirectUrl"); // else cannot add non-nullable field
|
||||
|
||||
localContext.Delete.Index("IX_umbracoRedirectUrl").OnTable("umbracoRedirectUrl");
|
||||
|
||||
localContext.Delete.Column("hurl").FromTable("umbracoRedirectUrl");
|
||||
|
||||
// SQL CE does not want to alter-add non-nullable columns ;-(
|
||||
// but it's OK to create as nullable then alter, go figure
|
||||
//localContext.Alter.Table("umbracoRedirectUrl")
|
||||
// .AddColumn("urlHash").AsString(16).NotNullable();
|
||||
localContext.Alter.Table("umbracoRedirectUrl")
|
||||
.AddColumn("urlHash").AsString(16).Nullable();
|
||||
localContext.Alter.Table("umbracoRedirectUrl")
|
||||
.AlterColumn("urlHash").AsString(16).NotNullable();
|
||||
|
||||
localContext.Create.Index("IX_umbracoRedirectUrl").OnTable("umbracoRedirectUrl")
|
||||
.OnColumn("urlHash")
|
||||
.Ascending()
|
||||
.OnColumn("contentKey")
|
||||
.Ascending()
|
||||
.OnColumn("createDateUtc")
|
||||
.Descending()
|
||||
.WithOptions().NonClustered();
|
||||
|
||||
return localContext.GetSql();
|
||||
}
|
||||
|
||||
public override void Down()
|
||||
{ }
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
/// <summary>
|
||||
/// Defines the <see cref="IRedirectUrl"/> repository.
|
||||
/// </summary>
|
||||
public interface IRedirectUrlRepository : IRepositoryQueryable<int, IRedirectUrl>
|
||||
public interface IRedirectUrlRepository : IRepositoryQueryable<Guid, IRedirectUrl>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a redirect url.
|
||||
@@ -21,7 +21,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
/// Deletes a redirect url.
|
||||
/// </summary>
|
||||
/// <param name="id">The redirect url identifier.</param>
|
||||
void Delete(int id);
|
||||
void Delete(Guid id);
|
||||
|
||||
/// <summary>
|
||||
/// Deletes all redirect urls.
|
||||
|
||||
@@ -49,6 +49,10 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
INNER JOIN umbracoNode as TB2 ON TB1.nodeId = TB2.id
|
||||
WHERE TB2.trashed = '1' AND TB2.nodeObjectType = @NodeObjectType)",
|
||||
FormatDeleteStatement("umbracoAccess", "nodeId"),
|
||||
@"DELETE FROM umbracoRedirectUrl WHERE umbracoRedirectUrl.id IN(
|
||||
SELECT TB1.id FROM umbracoRedirectUrl as TB1
|
||||
INNER JOIN umbracoNode as TB2 ON TB1.contentKey = TB2.uniqueId
|
||||
WHERE TB2.trashed = '1' AND TB2.nodeObjectType = @NodeObjectType)",
|
||||
FormatDeleteStatement("umbracoRelation", "parentId"),
|
||||
FormatDeleteStatement("umbracoRelation", "childId"),
|
||||
FormatDeleteStatement("cmsTagRelationship", "nodeId"),
|
||||
|
||||
@@ -12,7 +12,7 @@ using Umbraco.Core.Persistence.UnitOfWork;
|
||||
|
||||
namespace Umbraco.Core.Persistence.Repositories
|
||||
{
|
||||
internal class RedirectUrlRepository : PetaPocoRepositoryBase<int, IRedirectUrl>, IRedirectUrlRepository
|
||||
internal class RedirectUrlRepository : PetaPocoRepositoryBase<Guid, IRedirectUrl>, IRedirectUrlRepository
|
||||
{
|
||||
public RedirectUrlRepository(IDatabaseUnitOfWork work, CacheHelper cache, ILogger logger, ISqlSyntaxProvider sqlSyntax)
|
||||
: base(work, cache, logger, sqlSyntax)
|
||||
@@ -23,19 +23,19 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
throw new NotSupportedException("This repository does not support this method.");
|
||||
}
|
||||
|
||||
protected override bool PerformExists(int id)
|
||||
protected override bool PerformExists(Guid id)
|
||||
{
|
||||
return PerformGet(id) != null;
|
||||
}
|
||||
|
||||
protected override IRedirectUrl PerformGet(int id)
|
||||
protected override IRedirectUrl PerformGet(Guid id)
|
||||
{
|
||||
var sql = GetBaseQuery(false).Where<RedirectUrlDto>(x => x.Id == id);
|
||||
var dto = Database.Fetch<RedirectUrlDto>(SqlSyntax.SelectTop(sql, 1)).FirstOrDefault();
|
||||
return dto == null ? null : Map(dto);
|
||||
}
|
||||
|
||||
protected override IEnumerable<IRedirectUrl> PerformGetAll(params int[] ids)
|
||||
protected override IEnumerable<IRedirectUrl> PerformGetAll(params Guid[] ids)
|
||||
{
|
||||
if (ids.Length > 2000)
|
||||
throw new NotSupportedException("This repository does not support more than 2000 ids.");
|
||||
@@ -86,7 +86,7 @@ JOIN umbracoNode ON umbracoRedirectUrl.contentKey=umbracoNode.uniqueID");
|
||||
{
|
||||
var dto = Map(entity);
|
||||
Database.Insert(dto);
|
||||
entity.Id = dto.Id;
|
||||
entity.Id = entity.Key.GetHashCode();
|
||||
}
|
||||
|
||||
protected override void PersistUpdatedItem(IRedirectUrl entity)
|
||||
@@ -101,7 +101,7 @@ JOIN umbracoNode ON umbracoRedirectUrl.contentKey=umbracoNode.uniqueID");
|
||||
|
||||
return new RedirectUrlDto
|
||||
{
|
||||
Id = redirectUrl.Id,
|
||||
Id = redirectUrl.Key,
|
||||
ContentKey = redirectUrl.ContentKey,
|
||||
CreateDateUtc = redirectUrl.CreateDateUtc,
|
||||
Url = redirectUrl.Url,
|
||||
@@ -117,7 +117,7 @@ JOIN umbracoNode ON umbracoRedirectUrl.contentKey=umbracoNode.uniqueID");
|
||||
try
|
||||
{
|
||||
url.DisableChangeTracking();
|
||||
url.Id = dto.Id;
|
||||
url.Id = dto.Id.GetHashCode();
|
||||
url.ContentId = dto.ContentId;
|
||||
url.ContentKey = dto.ContentKey;
|
||||
url.CreateDateUtc = dto.CreateDateUtc;
|
||||
@@ -148,7 +148,7 @@ JOIN umbracoNode ON umbracoRedirectUrl.contentKey=umbracoNode.uniqueID");
|
||||
Database.Execute("DELETE FROM umbracoRedirectUrl WHERE contentKey=@contentKey", new { contentKey });
|
||||
}
|
||||
|
||||
public void Delete(int id)
|
||||
public void Delete(Guid id)
|
||||
{
|
||||
Database.Delete<RedirectUrlDto>(id);
|
||||
}
|
||||
@@ -196,7 +196,7 @@ JOIN umbracoNode ON umbracoRedirectUrl.contentKey=umbracoNode.uniqueID");
|
||||
|
||||
private static string HashUrl(string url)
|
||||
{
|
||||
using (var crypto = new MD5CryptoServiceProvider())
|
||||
using (var crypto = new SHA1CryptoServiceProvider())
|
||||
{
|
||||
var inputBytes = Encoding.UTF8.GetBytes(url);
|
||||
var hashedBytes = crypto.ComputeHash(inputBytes);
|
||||
|
||||
@@ -33,7 +33,7 @@ namespace Umbraco.Core.Services
|
||||
/// Deletes a redirect url.
|
||||
/// </summary>
|
||||
/// <param name="id">The redirect url identifier.</param>
|
||||
void Delete(int id);
|
||||
void Delete(Guid id);
|
||||
|
||||
/// <summary>
|
||||
/// Deletes all redirect urls.
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace Umbraco.Core.Services
|
||||
if (redir != null)
|
||||
redir.CreateDateUtc = DateTime.UtcNow;
|
||||
else
|
||||
redir = new RedirectUrl { Url = url, ContentKey = contentKey };
|
||||
redir = new RedirectUrl { Key = Guid.NewGuid(), Url = url, ContentKey = contentKey };
|
||||
repo.AddOrUpdate(redir);
|
||||
uow.Commit();
|
||||
}
|
||||
@@ -39,7 +39,7 @@ namespace Umbraco.Core.Services
|
||||
}
|
||||
}
|
||||
|
||||
public void Delete(int id)
|
||||
public void Delete(Guid id)
|
||||
{
|
||||
using (var uow = UowProvider.GetUnitOfWork())
|
||||
using (var repo = RepositoryFactory.CreateRedirectUrlRepository(uow))
|
||||
|
||||
@@ -428,9 +428,6 @@
|
||||
<Compile Include="Persistence\Mappers\MigrationEntryMapper.cs" />
|
||||
<Compile Include="Persistence\Migrations\LocalMigrationContext.cs" />
|
||||
<Compile Include="Persistence\Migrations\Upgrades\TargetVersionFourOneZero\AddPreviewXmlTable.cs" />
|
||||
<Compile Include="Persistence\Migrations\Upgrades\TargetVersionSevenFiveZero\AddRedirectUrlTable4.cs" />
|
||||
<Compile Include="Persistence\Migrations\Upgrades\TargetVersionSevenFiveZero\AddRedirectUrlTable3.cs" />
|
||||
<Compile Include="Persistence\Migrations\Upgrades\TargetVersionSevenFiveZero\AddRedirectUrlTable2.cs" />
|
||||
<Compile Include="Persistence\Migrations\Upgrades\TargetVersionSevenFiveZero\EnsureServersLockObject.cs" />
|
||||
<Compile Include="Persistence\Migrations\Upgrades\TargetVersionSevenFiveZero\AddRedirectUrlTable.cs" />
|
||||
<Compile Include="Persistence\Migrations\Upgrades\TargetVersionSevenFiveZero\RemoveStylesheetDataAndTablesAgain.cs" />
|
||||
|
||||
@@ -119,10 +119,7 @@ namespace Umbraco.Tests.Migrations
|
||||
|
||||
//pass in explicit migrations
|
||||
new DeleteRedirectUrlTable(SqlSyntax, logger),
|
||||
new AddRedirectUrlTable(SqlSyntax, logger),
|
||||
new AddRedirectUrlTable2(SqlSyntax, logger),
|
||||
new AddRedirectUrlTable3(SqlSyntax, logger),
|
||||
new AddRedirectUrlTable4(SqlSyntax, logger)
|
||||
new AddRedirectUrlTable(SqlSyntax, logger)
|
||||
);
|
||||
|
||||
var db = new UmbracoDatabase("Datasource=|DataDirectory|UmbracoPetaPocoTests.sdf;Flush Interval=1;", "System.Data.SqlServerCe.4.0", Logger);
|
||||
|
||||
Reference in New Issue
Block a user