Fix MySql support

Fixes #7565 
Fixes #7570 
Fixes #7570
Fixes #5366
Fixes #7571 
Fixes #4779
This commit is contained in:
siyamandayubi
2017-03-02 21:52:28 +01:00
committed by Sébastien Ros
parent 6f39308741
commit 0e3b8ad32d
3 changed files with 50 additions and 17 deletions

View File

@@ -7,7 +7,7 @@ namespace Orchard.Users {
public class UsersDataMigration : DataMigrationImpl { public class UsersDataMigration : DataMigrationImpl {
public int Create() { public int Create() {
SchemaBuilder.CreateTable("UserPartRecord", SchemaBuilder.CreateTable("UserPartRecord",
table => table table => table
.ContentPartRecord() .ContentPartRecord()
.Column<string>("UserName") .Column<string>("UserName")

View File

@@ -405,7 +405,7 @@ namespace Orchard.Data.Migration.Interpreters {
if ( value == null ) { if ( value == null ) {
return "null"; return "null";
} }
TypeCode typeCode = Type.GetTypeCode(value.GetType()); TypeCode typeCode = Type.GetTypeCode(value.GetType());
switch (typeCode) { switch (typeCode) {
case TypeCode.Empty: case TypeCode.Empty:
@@ -428,7 +428,7 @@ namespace Orchard.Data.Migration.Interpreters {
case TypeCode.Decimal: case TypeCode.Decimal:
return Convert.ToString(value, CultureInfo.InvariantCulture); return Convert.ToString(value, CultureInfo.InvariantCulture);
case TypeCode.DateTime: case TypeCode.DateTime:
return String.Concat("'", Convert.ToString(value, CultureInfo.InvariantCulture), "'"); return String.Concat("'", ((DateTime)value).ToString("yyyy-MM-dd hh:mm:ss", CultureInfo.InvariantCulture), "'");
} }
return "null"; return "null";

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.Linq;
using System.Text; using System.Text;
using NHibernate.Dialect; using NHibernate.Dialect;
using Orchard.Data.Migration.Schema; using Orchard.Data.Migration.Schema;
@@ -8,12 +9,14 @@ using Orchard.Environment.Configuration;
using Orchard.Localization; using Orchard.Localization;
namespace Orchard.Data.Migration.Interpreters { namespace Orchard.Data.Migration.Interpreters {
public class MySqlCommandInterpreter : ICommandInterpreter<AlterColumnCommand> { public class MySqlCommandInterpreter : ICommandInterpreter<AlterColumnCommand>, ICommandInterpreter<AddIndexCommand> {
private readonly Lazy<Dialect> _dialectLazy; private readonly Lazy<Dialect> _dialectLazy;
private readonly ShellSettings _shellSettings; private readonly ShellSettings _shellSettings;
private readonly ITransactionManager _transactionManager;
private readonly DefaultDataMigrationInterpreter _dataMigrationInterpreter; private readonly DefaultDataMigrationInterpreter _dataMigrationInterpreter;
public MySqlCommandInterpreter(DefaultDataMigrationInterpreter dataMigrationInterpreter) { public MySqlCommandInterpreter(DefaultDataMigrationInterpreter dataMigrationInterpreter, ITransactionManager transactionManager) {
_transactionManager = transactionManager;
_dataMigrationInterpreter = dataMigrationInterpreter; _dataMigrationInterpreter = dataMigrationInterpreter;
T = NullLocalizer.Instance; T = NullLocalizer.Instance;
} }
@@ -26,14 +29,16 @@ namespace Orchard.Data.Migration.Interpreters {
public MySqlCommandInterpreter( public MySqlCommandInterpreter(
ShellSettings shellSettings, ShellSettings shellSettings,
ISessionFactoryHolder sessionFactoryHolder) { ISessionFactoryHolder sessionFactoryHolder,
_shellSettings = shellSettings; ITransactionManager transactionManager) {
_dialectLazy = new Lazy<Dialect>(() => Dialect.GetDialect(sessionFactoryHolder.GetConfiguration().Properties)); _shellSettings = shellSettings;
_transactionManager = transactionManager;
_dialectLazy = new Lazy<Dialect>(() => Dialect.GetDialect(sessionFactoryHolder.GetConfiguration().Properties));
} }
public string[] CreateStatements(AlterColumnCommand command) { public string[] CreateStatements(AlterColumnCommand command) {
var builder = new StringBuilder(); var builder = new StringBuilder();
builder.AppendFormat("alter table {0} modify column {1} ", builder.AppendFormat("alter table {0} modify column {1} ",
_dialectLazy.Value.QuoteForTableName(PrefixTableName(command.TableName)), _dialectLazy.Value.QuoteForTableName(PrefixTableName(command.TableName)),
_dialectLazy.Value.QuoteForColumnName(command.ColumnName)); _dialectLazy.Value.QuoteForColumnName(command.ColumnName));
@@ -42,8 +47,7 @@ namespace Orchard.Data.Migration.Interpreters {
// type // type
if (command.DbType != DbType.Object) { if (command.DbType != DbType.Object) {
builder.Append(DefaultDataMigrationInterpreter.GetTypeName(_dialectLazy.Value, command.DbType, command.Length, command.Precision, command.Scale)); builder.Append(DefaultDataMigrationInterpreter.GetTypeName(_dialectLazy.Value, command.DbType, command.Length, command.Precision, command.Scale));
} } else {
else {
if (command.Length > 0 || command.Precision > 0 || command.Scale > 0) { if (command.Length > 0 || command.Precision > 0 || command.Scale > 0) {
throw new OrchardException(T("Error while executing data migration: you need to specify the field's type in order to change its properties")); throw new OrchardException(T("Error while executing data migration: you need to specify the field's type in order to change its properties"));
} }
@@ -56,7 +60,7 @@ namespace Orchard.Data.Migration.Interpreters {
_dialectLazy.Value.QuoteForTableName(PrefixTableName(command.TableName)), _dialectLazy.Value.QuoteForTableName(PrefixTableName(command.TableName)),
_dialectLazy.Value.QuoteForColumnName(command.ColumnName)); _dialectLazy.Value.QuoteForColumnName(command.ColumnName));
var initLength2 = builder2.Length; var initLength2 = builder2.Length;
if (command.Default != null) { if (command.Default != null) {
builder2.Append(" set default ").Append(_dataMigrationInterpreter.ConvertToSqlValue(command.Default)).Append(" "); builder2.Append(" set default ").Append(_dataMigrationInterpreter.ConvertToSqlValue(command.Default)).Append(" ");
} }
@@ -64,13 +68,11 @@ namespace Orchard.Data.Migration.Interpreters {
// result // result
var result = new List<string>(); var result = new List<string>();
if (builder.Length > initLength) if (builder.Length > initLength) {
{
result.Add(builder.ToString()); result.Add(builder.ToString());
} }
if (builder2.Length > initLength2) if (builder2.Length > initLength2) {
{
result.Add(builder2.ToString()); result.Add(builder2.ToString());
} }
@@ -82,5 +84,36 @@ namespace Orchard.Data.Migration.Interpreters {
return tableName; return tableName;
return _shellSettings.DataTablePrefix + "_" + tableName; return _shellSettings.DataTablePrefix + "_" + tableName;
} }
public string[] CreateStatements(AddIndexCommand command) {
var session = _transactionManager.GetSession();
using (var sqlCommand = session.Connection.CreateCommand()) {
var columnNames = String.Join(", ", command.ColumnNames.Select(c => string.Format("'{0}'", c)));
var tableName = PrefixTableName(command.TableName);
// check whether the index contains big nvarchar columns or text fields
string sql = @"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = '{1}' AND COLUMN_NAME in ({0}) AND TABLE_SCHEMA = '{2}' AND
((Data_type = 'varchar' and CHARACTER_MAXIMUM_LENGTH > 767) OR data_type= 'text');";
sql = string.Format(sql, columnNames, tableName, session.Connection.Database);
sqlCommand.CommandText = sql;
var columnList = command.ColumnNames.ToList();
using (var reader = sqlCommand.ExecuteReader()) {
// Provide prefix for string columns with length longer than 767
while (reader.Read()) {
var columnName = reader.GetString(0);
columnList[columnList.IndexOf(columnName)] = string.Format("{0}(767)", columnName);
}
}
return new[] {string.Format("create index {1} on {0} ({2}) ",
_dialectLazy.Value.QuoteForTableName(tableName),
_dialectLazy.Value.QuoteForTableName(PrefixTableName(command.IndexName)),
String.Join(", ", columnList))};
}
}
} }
} }