mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 03:25:23 +08:00
Manage default SQL values conversion in Datq Migrations
--HG-- branch : dev
This commit is contained in:
@@ -96,7 +96,7 @@ namespace Orchard.Tests.DataMigration {
|
||||
.Column("Lastname", DbType.String, column => column.WithLength(100).NotNull())
|
||||
.Column("SN", DbType.AnsiString, column => column.WithLength(40).Unique())
|
||||
.Column("Salary", DbType.Decimal, column => column.WithPrecision(9).WithScale(2))
|
||||
.Column("Gender", DbType.Decimal, column => column.WithDefault("''"))
|
||||
.Column("Gender", DbType.Decimal, column => column.WithDefault(""))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ namespace Orchard.Tests.DataMigration {
|
||||
.Column("Lastname", DbType.String, column => column.WithLength(100).NotNull())
|
||||
.Column("SN", DbType.AnsiString, column => column.WithLength(40).Unique())
|
||||
.Column("Salary", DbType.Decimal, column => column.WithPrecision(9).WithScale(2))
|
||||
.Column("Gender", DbType.Decimal, column => column.WithDefault("''"))
|
||||
.Column("Gender", DbType.Decimal, column => column.WithDefault(""))
|
||||
);
|
||||
|
||||
_schemaBuilder
|
||||
@@ -133,7 +133,7 @@ namespace Orchard.Tests.DataMigration {
|
||||
.AlterTable("User", table => table
|
||||
.AddColumn("Age", DbType.Int32))
|
||||
.AlterTable("User", table => table
|
||||
.AlterColumn("Lastname", column => column.WithDefault("'John'")))
|
||||
.AlterColumn("Lastname", column => column.WithDefault("John")))
|
||||
.AlterTable("User", table => table
|
||||
.DropColumn("Firstname")
|
||||
);
|
||||
|
@@ -23,17 +23,17 @@ namespace Orchard.Users.DataMigrations {
|
||||
|
||||
// Adds registration fields to previous versions
|
||||
SchemaBuilder
|
||||
.AlterTable("UserPartRecord", table => table.AddColumn<string>("RegistrationStatus", c => c.WithDefault("'Approved'")))
|
||||
.AlterTable("UserPartRecord", table => table.AddColumn<string>("EmailStatus", c => c.WithDefault("'Approved'")))
|
||||
.AlterTable("UserPartRecord", table => table.AddColumn<string>("RegistrationStatus", c => c.WithDefault("Approved")))
|
||||
.AlterTable("UserPartRecord", table => table.AddColumn<string>("EmailStatus", c => c.WithDefault("Approved")))
|
||||
.AlterTable("UserPartRecord", table => table.AddColumn<string>("EmailChallengeToken"));
|
||||
|
||||
// Site Settings record
|
||||
SchemaBuilder.CreateTable("RegistrationSettingsPartRecord", table => table
|
||||
.ContentPartRecord()
|
||||
.Column<bool>("UsersCanRegister", c => c.WithDefault("'0'"))
|
||||
.Column<bool>("UsersMustValidateEmail", c => c.WithDefault("'0'"))
|
||||
.Column<bool>("UsersAreModerated", c => c.WithDefault("'0'"))
|
||||
.Column<bool>("NotifyModeration", c => c.WithDefault("'0'"))
|
||||
.Column<bool>("UsersCanRegister", c => c.WithDefault(false))
|
||||
.Column<bool>("UsersMustValidateEmail", c => c.WithDefault(false))
|
||||
.Column<bool>("UsersAreModerated", c => c.WithDefault(false))
|
||||
.Column<bool>("NotifyModeration", c => c.WithDefault(false))
|
||||
);
|
||||
|
||||
return 2;
|
||||
|
@@ -94,12 +94,14 @@ namespace Orchard.Data.Migration.Generator {
|
||||
var command = new CreateTableCommand(tableName);
|
||||
|
||||
foreach (var column in table.ColumnIterator) {
|
||||
var table1 = table;
|
||||
var column1 = column;
|
||||
var sqlType = column1.GetSqlTypeCode(mapping);
|
||||
// create copies for local variables to be evaluated at the time the loop is called, and not lately when the la;bda is executed
|
||||
var tableCopy = table;
|
||||
var columnCopy = column;
|
||||
|
||||
var sqlType = columnCopy.GetSqlTypeCode(mapping);
|
||||
command.Column(column.Name, sqlType.DbType,
|
||||
action => {
|
||||
if (table1.PrimaryKey.Columns.Any(c => c.Name == column1.Name)) {
|
||||
if (tableCopy.PrimaryKey.Columns.Any(c => c.Name == columnCopy.Name)) {
|
||||
action.PrimaryKey();
|
||||
|
||||
if ( !isContentPart ) {
|
||||
@@ -108,26 +110,26 @@ namespace Orchard.Data.Migration.Generator {
|
||||
}
|
||||
|
||||
|
||||
if ( column1.IsLengthDefined()
|
||||
if ( columnCopy.IsLengthDefined()
|
||||
&& new DbType[] { DbType.StringFixedLength, DbType.String, DbType.AnsiString, DbType.AnsiStringFixedLength }.Contains(sqlType.DbType)
|
||||
&& column1.Length != Column.DefaultLength) {
|
||||
action.WithLength(column1.Length);
|
||||
&& columnCopy.Length != Column.DefaultLength) {
|
||||
action.WithLength(columnCopy.Length);
|
||||
}
|
||||
|
||||
if (column1.IsPrecisionDefined()) {
|
||||
action.WithPrecision((byte) column1.Precision);
|
||||
action.WithScale((byte) column1.Scale);
|
||||
if (columnCopy.IsPrecisionDefined()) {
|
||||
action.WithPrecision((byte) columnCopy.Precision);
|
||||
action.WithScale((byte) columnCopy.Scale);
|
||||
}
|
||||
if (column1.IsNullable) {
|
||||
if (columnCopy.IsNullable) {
|
||||
action.Nullable();
|
||||
}
|
||||
|
||||
if ( column1.IsUnique ) {
|
||||
if ( columnCopy.IsUnique ) {
|
||||
action.Unique();
|
||||
}
|
||||
|
||||
if(column1.DefaultValue != null) {
|
||||
action.WithDefault(column1.DefaultValue);
|
||||
if(columnCopy.DefaultValue != null) {
|
||||
action.WithDefault(columnCopy.DefaultValue);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NHibernate;
|
||||
@@ -200,7 +201,7 @@ namespace Orchard.Data.Migration.Interpreters {
|
||||
|
||||
// [default value]
|
||||
if (!string.IsNullOrEmpty(command.Default)) {
|
||||
builder.Append(" set default ").Append(command.Default).Append(Space);
|
||||
builder.Append(" set default ").Append(ConvertToSqlValue(command.Default)).Append(Space);
|
||||
}
|
||||
_sqlStatements.Add(builder.ToString());
|
||||
}
|
||||
@@ -304,7 +305,7 @@ namespace Orchard.Data.Migration.Interpreters {
|
||||
|
||||
// [default value]
|
||||
if (!string.IsNullOrEmpty(command.Default)) {
|
||||
builder.Append(" default ").Append(command.Default).Append(Space);
|
||||
builder.Append(" default ").Append(ConvertToSqlValue(command.Default)).Append(Space);
|
||||
}
|
||||
|
||||
// nullable
|
||||
@@ -352,5 +353,38 @@ namespace Orchard.Data.Migration.Interpreters {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static string ConvertToSqlValue(object value) {
|
||||
if ( value == null ) {
|
||||
return "null";
|
||||
}
|
||||
|
||||
TypeCode typeCode = Type.GetTypeCode(value.GetType());
|
||||
switch (typeCode) {
|
||||
case TypeCode.Empty:
|
||||
case TypeCode.Object:
|
||||
case TypeCode.DBNull:
|
||||
case TypeCode.String:
|
||||
case TypeCode.Char:
|
||||
return String.Concat("'", Convert.ToString(value).Replace("'", "''"), "'");
|
||||
case TypeCode.Boolean:
|
||||
return (bool) value ? "1" : "0";
|
||||
case TypeCode.SByte:
|
||||
case TypeCode.Int16:
|
||||
case TypeCode.UInt16:
|
||||
case TypeCode.Int32:
|
||||
case TypeCode.UInt32:
|
||||
case TypeCode.Int64:
|
||||
case TypeCode.UInt64:
|
||||
case TypeCode.Single:
|
||||
case TypeCode.Double:
|
||||
case TypeCode.Decimal:
|
||||
return Convert.ToString(value, CultureInfo.InvariantCulture);
|
||||
case TypeCode.DateTime:
|
||||
return String.Concat("'", Convert.ToString(value, CultureInfo.InvariantCulture), "'");
|
||||
}
|
||||
|
||||
return "null";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -17,7 +17,7 @@ namespace Orchard.Data.Migration.Schema {
|
||||
|
||||
public DbType DbType { get; private set; }
|
||||
|
||||
public string Default { get; private set; }
|
||||
public object Default { get; private set; }
|
||||
|
||||
public int? Length { get; private set; }
|
||||
|
||||
@@ -26,7 +26,7 @@ namespace Orchard.Data.Migration.Schema {
|
||||
return this;
|
||||
}
|
||||
|
||||
public ColumnCommand WithDefault(string @default) {
|
||||
public ColumnCommand WithDefault(object @default) {
|
||||
Default = @default;
|
||||
return this;
|
||||
}
|
||||
|
@@ -73,7 +73,7 @@ namespace Orchard.Data.Migration.Schema {
|
||||
return this;
|
||||
}
|
||||
|
||||
public new CreateColumnCommand WithDefault(string @default) {
|
||||
public new CreateColumnCommand WithDefault(object @default) {
|
||||
base.WithDefault(@default);
|
||||
return this;
|
||||
}
|
||||
|
Reference in New Issue
Block a user