diff --git a/src/Orchard.Tests/DataMigration/SchemaBuilderTests.cs b/src/Orchard.Tests/DataMigration/SchemaBuilderTests.cs index bf93a8d80..59f493f52 100644 --- a/src/Orchard.Tests/DataMigration/SchemaBuilderTests.cs +++ b/src/Orchard.Tests/DataMigration/SchemaBuilderTests.cs @@ -1,4 +1,5 @@ -using System.Data; +using System; +using System.Data; using System.Linq; using Autofac; using NHibernate; @@ -37,8 +38,6 @@ namespace Orchard.Tests.DataMigration { var builder = new ContainerBuilder(); - builder.RegisterInstance(new ShellSettings { DataTablePrefix = "TEST_", DataProvider = "SqlCe" }); - var session = _sessionFactory.OpenSession(); builder.RegisterInstance(appDataFolder).As(); builder.RegisterType().As(); @@ -49,7 +48,7 @@ namespace Orchard.Tests.DataMigration { builder.RegisterType().As(); builder.RegisterInstance(new DefaultContentManagerTests.TestSessionLocator(session)).As(); builder.RegisterInstance(new ShellBlueprint { Records = Enumerable.Empty() }).As(); - builder.RegisterInstance(new ShellSettings { Name = "temp", DataProvider = "SqlCe", DataTablePrefix = "TEST_" }).As(); + builder.RegisterInstance(new ShellSettings { Name = "temp", DataProvider = "SqlCe", DataTablePrefix = "TEST" }).As(); builder.RegisterModule(new DataModule()); _container = builder.Build(); @@ -154,6 +153,61 @@ namespace Orchard.Tests.DataMigration { .Column("UserId", DbType.Int32, column => column.NotNull())) .CreateForeignKey("FK_User", "Address", new[] { "UserId" }, "User", new[] { "Id" }) .DropForeignKey("Address", "FK_User"); + } + + [Test, ExpectedException] + public void BiggerDataShouldNotFit() { + _schemaBuilder + .CreateTable("ContentItemRecord", table => table + .Column("Id", DbType.Int32, column => column.PrimaryKey().Identity()) + .Column("Data", DbType.String, column => column.WithLength(255))); + + // should write successfully less than 255 chars + _schemaBuilder + .ExecuteSql("insert into TEST_ContentItemRecord (Data) values('Hello World')"); + + // should throw an exception if trying to write more data + _schemaBuilder + .ExecuteSql(String.Format("insert into TEST_ContentItemRecord (Data) values('{0}')", new String('x', 256))); + + _schemaBuilder + .AlterTable("ContentItemRecord", table => table + .AlterColumn("Data", column => column.WithType(DbType.String).WithLength(257))); + + _schemaBuilder + .ExecuteSql(String.Format("insert into TEST_ContentItemRecord (Data) values('{0}')", new String('x', 256))); + } + + [Test] + public void ShouldAllowFieldSizeAlteration() { + _schemaBuilder + .CreateTable("ContentItemRecord", table => table + .Column("Id", DbType.Int32, column => column.PrimaryKey().Identity()) + .Column("Data", DbType.String, column => column.WithLength(255))); + + // should write successfully less than 255 chars + _schemaBuilder + .ExecuteSql("insert into TEST_ContentItemRecord (Data) values('Hello World')"); + + _schemaBuilder + .AlterTable("ContentItemRecord", table => table + .AlterColumn("Data", column => column.WithType(DbType.String).WithLength(2048))); + + // should write successfully a bigger value now + _schemaBuilder + .ExecuteSql(String.Format("insert into TEST_ContentItemRecord (Data) values('{0}')", new String('x', 2048))); + } + + [Test, ExpectedException(typeof(OrchardException))] + public void ChangingSizeWithoutTypeShouldNotBeAllowed() { + _schemaBuilder + .CreateTable("ContentItemRecord", table => table + .Column("Id", DbType.Int32, column => column.PrimaryKey().Identity()) + .Column("Data", DbType.String, column => column.WithLength(255))); + + _schemaBuilder + .AlterTable("ContentItemRecord", table => table + .AlterColumn("Data", column => column.WithLength(2048))); } } diff --git a/src/Orchard/ContentManagement/DataMigrations/FrameworkDataMigration.cs b/src/Orchard/ContentManagement/DataMigrations/FrameworkDataMigration.cs index b1dcca7ed..5cdaaf79f 100644 --- a/src/Orchard/ContentManagement/DataMigrations/FrameworkDataMigration.cs +++ b/src/Orchard/ContentManagement/DataMigrations/FrameworkDataMigration.cs @@ -1,4 +1,5 @@ -using Orchard.Data.Migration; +using System.Data; +using Orchard.Data.Migration; namespace Orchard.ContentManagement.DataMigrations { public class FrameworkDataMigration : DataMigrationImpl { @@ -35,5 +36,13 @@ namespace Orchard.ContentManagement.DataMigrations { return 1; } + + public int UpdateFrom1() { + SchemaBuilder + .AlterTable("ContentItemRecord", table => table + .AlterColumn("Data", column => column.WithType(DbType.String).Unlimited())); + + return 2; + } } } \ No newline at end of file diff --git a/src/Orchard/Data/Migration/Interpreters/DefaultDataMigrationInterpreter.cs b/src/Orchard/Data/Migration/Interpreters/DefaultDataMigrationInterpreter.cs index 86b7390a8..6377aa25f 100644 --- a/src/Orchard/Data/Migration/Interpreters/DefaultDataMigrationInterpreter.cs +++ b/src/Orchard/Data/Migration/Interpreters/DefaultDataMigrationInterpreter.cs @@ -39,7 +39,7 @@ namespace Orchard.Data.Migration.Interpreters { _reportsCoordinator = reportsCoordinator; Logger = NullLogger.Instance; - + T = NullLocalizer.Instance; var configuration = _sessionFactoryHolder.GetConfiguration(); _dialect = Dialect.GetDialect(configuration.Properties); } @@ -192,6 +192,11 @@ namespace Orchard.Data.Migration.Interpreters { if (command.DbType != DbType.Object) { builder.Append(GetTypeName(command.DbType, command.Length, command.Precision, command.Scale)); } + else { + 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 it's properies")); + } + } // [default value] if (!string.IsNullOrEmpty(command.Default)) {