From 4bb89c1827d15621918e95d360df4e9afc282fa7 Mon Sep 17 00:00:00 2001 From: Shannon Date: Fri, 11 Sep 2015 11:49:37 +0200 Subject: [PATCH] Fixes: U4-7090 Upgrade error with dates --- .../Migrations/MigrationExpressionBase.cs | 2 ++ .../Persistence/SqlSyntax/ISqlSyntaxProvider.cs | 1 + .../Persistence/SqlSyntax/MySqlSyntaxProvider.cs | 15 +++++++++++++++ .../SqlSyntax/SqlSyntaxProviderBase.cs | 15 +++++++++++++++ 4 files changed, 33 insertions(+) diff --git a/src/Umbraco.Core/Persistence/Migrations/MigrationExpressionBase.cs b/src/Umbraco.Core/Persistence/Migrations/MigrationExpressionBase.cs index 7dde31d3e3..7dff959ade 100644 --- a/src/Umbraco.Core/Persistence/Migrations/MigrationExpressionBase.cs +++ b/src/Umbraco.Core/Persistence/Migrations/MigrationExpressionBase.cs @@ -76,6 +76,8 @@ namespace Umbraco.Core.Persistence.Migrations case TypeCode.UInt32: case TypeCode.UInt64: return val.ToString(); + case TypeCode.DateTime: + return SqlSyntax.FormatDateTime((DateTime) val); default: return SqlSyntaxContext.SqlSyntaxProvider.GetQuotedValue(val.ToString()); } diff --git a/src/Umbraco.Core/Persistence/SqlSyntax/ISqlSyntaxProvider.cs b/src/Umbraco.Core/Persistence/SqlSyntax/ISqlSyntaxProvider.cs index 899a462172..056d9c1d9a 100644 --- a/src/Umbraco.Core/Persistence/SqlSyntax/ISqlSyntaxProvider.cs +++ b/src/Umbraco.Core/Persistence/SqlSyntax/ISqlSyntaxProvider.cs @@ -54,6 +54,7 @@ namespace Umbraco.Core.Persistence.SqlSyntax string DeleteConstraint { get; } string CreateForeignKeyConstraint { get; } string DeleteDefaultConstraint { get; } + string FormatDateTime(DateTime date, bool includeTime = true); string Format(TableDefinition table); string Format(IEnumerable columns); List Format(IEnumerable indexes); diff --git a/src/Umbraco.Core/Persistence/SqlSyntax/MySqlSyntaxProvider.cs b/src/Umbraco.Core/Persistence/SqlSyntax/MySqlSyntaxProvider.cs index dcb7dc029c..6832be7d65 100644 --- a/src/Umbraco.Core/Persistence/SqlSyntax/MySqlSyntaxProvider.cs +++ b/src/Umbraco.Core/Persistence/SqlSyntax/MySqlSyntaxProvider.cs @@ -191,6 +191,21 @@ ORDER BY TABLE_NAME, INDEX_NAME", return false; } + /// + /// This is used ONLY if we need to format datetime without using SQL parameters (i.e. during migrations) + /// + /// + /// + /// + /// + /// MySQL has a DateTime standard that is unambiguous and works on all servers: + /// YYYYMMDDHHMMSS + /// + public override string FormatDateTime(DateTime date, bool includeTime = true) + { + return includeTime ? date.ToString("YYYYMMDDHHmmss") : date.ToString("YYYYMMDD"); + } + public override string GetQuotedTableName(string tableName) { return string.Format("`{0}`", tableName); diff --git a/src/Umbraco.Core/Persistence/SqlSyntax/SqlSyntaxProviderBase.cs b/src/Umbraco.Core/Persistence/SqlSyntax/SqlSyntaxProviderBase.cs index 08d85e0573..7a56a461bc 100644 --- a/src/Umbraco.Core/Persistence/SqlSyntax/SqlSyntaxProviderBase.cs +++ b/src/Umbraco.Core/Persistence/SqlSyntax/SqlSyntaxProviderBase.cs @@ -251,6 +251,21 @@ namespace Umbraco.Core.Persistence.SqlSyntax return true; } + /// + /// This is used ONLY if we need to format datetime without using SQL parameters (i.e. during migrations) + /// + /// + /// + /// + /// + /// MSSQL has a DateTime standard that is unambiguous and works on all servers: + /// YYYYMMDD HH:mm:ss + /// + public virtual string FormatDateTime(DateTime date, bool includeTime = true) + { + return includeTime ? date.ToString("YYYYMMDD HH:mm:ss") : date.ToString("YYYYMMDD"); + } + public virtual string Format(TableDefinition table) { var statement = string.Format(CreateTable, GetQuotedTableName(table.Name), Format(table.Columns));