Fixes up some things from PR 610 and fixes up quoted values when using the Insert.IntoTable migration.

This commit is contained in:
Shannon
2015-01-06 13:44:42 +11:00
parent fa48af02a1
commit f6cd597841
7 changed files with 110 additions and 85 deletions

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Umbraco.Core.Persistence.DatabaseModelDefinitions;
using Umbraco.Core.Persistence.SqlSyntax;
@@ -43,7 +44,7 @@ namespace Umbraco.Core.Persistence.Migrations.Syntax.Insert.Expressions
foreach (var keyVal in item)
{
cols += keyVal.Key + ",";
vals += keyVal.Value + ",";
vals += GetQuotedValue(keyVal.Value) + ",";
}
cols = cols.TrimEnd(',');
vals = vals.TrimEnd(',');
@@ -58,5 +59,29 @@ namespace Umbraco.Core.Persistence.Migrations.Syntax.Insert.Expressions
return string.Join(",", insertItems);
}
private string GetQuotedValue(object val)
{
var type = val.GetType();
switch (Type.GetTypeCode(type))
{
case TypeCode.Boolean:
return ((bool) val) ? "1" : "0";
case TypeCode.Single:
case TypeCode.Double:
case TypeCode.Decimal:
case TypeCode.SByte:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Byte:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
return val.ToString();
default:
return SqlSyntaxContext.SqlSyntaxProvider.GetQuotedValue(val.ToString());
}
}
}
}

View File

@@ -9,14 +9,15 @@ namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSevenThreeZe
{
public override void Up()
{
Execute.Code(AddRelationType);
}
Insert.IntoTable("umbracoRelationType").Row(new
{
dual = false,
parentObjectType = Guid.Parse(Constants.ObjectTypes.Document),
childObjectType = Guid.Parse(Constants.ObjectTypes.Document),
name = Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteName,
alias = Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteAlias
});
public static string AddRelationType(Database database)
{
database.Insert("umbracoRelationType", "id", false, new RelationTypeDto { Alias = Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteAlias, ChildObjectType = new Guid(Constants.ObjectTypes.Document), ParentObjectType = new Guid(Constants.ObjectTypes.Document), Dual = false, Name = Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteName });
return string.Empty;
}
public override void Down()

View File

@@ -15,6 +15,13 @@ namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSevenThreeZe
public override void Up()
{
//Don't execute anything if there is no 'master' column - this might occur if the db is already upgraded
var cols = SqlSyntaxContext.SqlSyntaxProvider.GetColumnsInSchema(Context.Database);
if (cols.Any(x => x.ColumnName.InvariantEquals("master") && x.TableName.InvariantEquals("cmsTemplate")) == false)
{
return;
}
//update the parentId column for all templates to be correct so it matches the current 'master' template
//NOTE: we are using dynamic because we need to get the data in a column that no longer exists in the schema
var templates = Context.Database.Fetch<dynamic>(new Sql().Select("*").From<TemplateDto>());

View File

@@ -0,0 +1,64 @@
using System;
using System.Linq;
using Umbraco.Core.Auditing;
using Umbraco.Core.Events;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
namespace Umbraco.Core.Strategies
{
public sealed class RelateOnTrashHandler : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
ContentService.Moved += ContentService_Moved;
ContentService.Trashed += ContentService_Trashed;
}
private void ContentService_Moved(IContentService sender, MoveEventArgs<IContent> e)
{
foreach (var item in e.MoveInfoCollection.Where(x => x.OriginalPath.Contains(Constants.System.RecycleBinContent.ToInvariantString())))
{
var relationService = ApplicationContext.Current.Services.RelationService;
var relationTypeAlias = Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteAlias;
var relations = relationService.GetByChildId(item.Entity.Id);
foreach (var relation in relations.Where(x => x.RelationType.Alias.InvariantEquals(relationTypeAlias)))
{
relationService.Delete(relation);
}
}
}
private void ContentService_Trashed(IContentService sender, MoveEventArgs<IContent> e)
{
var relationService = ApplicationContext.Current.Services.RelationService;
var relationTypeAlias = Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteAlias;
var relationType = relationService.GetRelationTypeByAlias(relationTypeAlias);
// check that the relation-type exists, if not, then recreate it
if (relationType == null)
{
var documentObjectType = new Guid(Constants.ObjectTypes.Document);
var relationTypeName = Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteName;
relationType = new RelationType(documentObjectType, documentObjectType, relationTypeAlias, relationTypeName);
relationService.Save(relationType);
}
foreach (var item in e.MoveInfoCollection)
{
var originalPath = item.OriginalPath.ToDelimitedList();
var originalParentId = originalPath.Count > 2
? int.Parse(originalPath[originalPath.Count - 2])
: Constants.System.Root;
// Add a relation for the item being deleted, so that we can know the original parent for if we need to restore later
var relation = new Relation(originalParentId, item.Entity.Id, relationType);
relationService.Save(relation);
Audit.Add(AuditTypes.Delete, string.Format("Trashed content with Id: '{0}' related to original parent content with Id: '{1}'", item.Entity.Id, originalParentId), item.Entity.WriterId, item.Entity.Id);
}
}
}
}

View File

@@ -1097,6 +1097,8 @@
<Compile Include="Standalone\ServiceContextManager.cs" />
<Compile Include="Standalone\StandaloneCoreApplication.cs" />
<Compile Include="Standalone\StandaloneCoreBootManager.cs" />
<Compile Include="Strategies\RelateOnCopyHandler.cs" />
<Compile Include="Strategies\RelateOnTrashHandler.cs" />
<Compile Include="Strings\ContentBaseExtensions.cs" />
<Compile Include="Strings\Diff.cs" />
<Compile Include="Sync\CurrentServerEnvironmentStatus.cs" />