mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Data migration schema API
--HG-- branch : dev
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
using System.Data;
|
||||
using NUnit.Framework;
|
||||
using Orchard.DataMigration.Schema;
|
||||
|
||||
namespace Orchard.Tests.DataMigration {
|
||||
[TestFixture]
|
||||
public class DataMigrationCommandsTests {
|
||||
|
||||
[Test]
|
||||
public void AllMethodsShouldBeCalledSuccessfully() {
|
||||
var schemaBuilder = new SchemaBuilder("TEST_");
|
||||
|
||||
schemaBuilder
|
||||
.CreateTable("User", table => table
|
||||
.ContentPartRecord()
|
||||
.Column("Id", DbType.Int32, column => column.PrimaryKey())
|
||||
.Column("Firstname", DbType.String, column => column.Length(255))
|
||||
.Column("Lastname", DbType.String, column => column.Precision(0).Scale(1))
|
||||
.ForeignKey("User_Address", fk => fk.On("Id", "Address", "UserId")))
|
||||
.CreateTable("Address", table => table
|
||||
.VersionedContentPartRecord()
|
||||
.Column("City", DbType.String)
|
||||
.Column("ZIP", DbType.Int32, column => column.Unique())
|
||||
.Column("UserId", DbType.Int32, column => column.NotNull()))
|
||||
.AlterTable("User", table => table
|
||||
.AddColumn("Age", DbType.Int32)
|
||||
.AlterColumn("Lastname", column => column.Default("John"))
|
||||
.AlterColumn("Lastname", column => column.Rename("John"))
|
||||
.DropColumn("Lastname")
|
||||
.CreateIndex("IDX_XYZ", "NickName")
|
||||
.DropIndex("IDX_XYZ")
|
||||
.AddForeignKey("FKL", fk => fk.On("Id", "A", "Id").On("Id", "B", "Id") )
|
||||
.DropForeignKey("FKL"))
|
||||
.DropTable("Address")
|
||||
.ExecuteSql("DROP DATABASE", statement => statement.ForDialect("SQLite").ForDialect("MsSqlServer2008"));
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,11 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using Autofac;
|
||||
using NHibernate;
|
||||
using NUnit.Framework;
|
||||
using Orchard.ContentManagement.Records;
|
||||
using Orchard.Data;
|
||||
using Orchard.DataMigration.Interpreters;
|
||||
using Orchard.Environment.Configuration;
|
||||
using Orchard.Environment.Extensions;
|
||||
using Orchard.Environment.Extensions.Folders;
|
||||
@@ -49,6 +51,7 @@ namespace Orchard.Tests.DataMigration {
|
||||
|
||||
builder.RegisterInstance(new ShellSettings { DataTablePrefix = "TEST_"});
|
||||
|
||||
builder.RegisterType<NullInterpreter>().As<IDataMigrationInterpreter>();
|
||||
builder.RegisterInstance(_folders).As<IExtensionFolders>();
|
||||
builder.RegisterType<ExtensionManager>().As<IExtensionManager>();
|
||||
builder.RegisterType<DataMigrationManager>().As<IDataMigrationManager>();
|
||||
@@ -135,7 +138,6 @@ namespace Orchard.Tests.DataMigration {
|
||||
public int UpdateFrom666() {
|
||||
return 999;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class DataMigrationDependenciesModule1 : IDataMigration {
|
||||
@@ -165,7 +167,6 @@ namespace Orchard.Tests.DataMigration {
|
||||
|
||||
public int Create() {
|
||||
Assert.That(SchemaBuilder, Is.Not.Null);
|
||||
Assert.That(SchemaBuilder.TablePrefix, Is.EqualTo("TEST_"));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -176,7 +177,6 @@ namespace Orchard.Tests.DataMigration {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class DataMigrationFeatureNeedUpdate2 : IDataMigration {
|
||||
public string Feature {
|
||||
get { return "Feature2"; }
|
||||
@@ -200,6 +200,20 @@ namespace Orchard.Tests.DataMigration {
|
||||
return 999;
|
||||
}
|
||||
}
|
||||
|
||||
public class DataMigrationSimpleBuilder : DataMigrationImpl {
|
||||
public override string Feature {
|
||||
get { return "Feature1"; }
|
||||
}
|
||||
|
||||
public int Create() {
|
||||
SchemaBuilder.CreateTable("UserRecord", table =>
|
||||
table.Column("Id", DbType.Int32, column =>
|
||||
column.PrimaryKey()));
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DataMigrationShouldDoNothingIfNoDataMigrationIsProvidedForFeature() {
|
||||
@@ -389,5 +403,23 @@ features:
|
||||
|
||||
Assert.That(_dataMigrationManager.GetFeaturesThatNeedUpdate().Contains("Feature3"), Is.False);
|
||||
}
|
||||
|
||||
|
||||
[Test] public void SchemaBuilderShouldCreateSql() {
|
||||
|
||||
Init(new[] { typeof(DataMigrationSimpleBuilder) });
|
||||
|
||||
_folders.Manifests.Add("Module1", @"
|
||||
name: Module1
|
||||
version: 0.1
|
||||
orchardversion: 1
|
||||
features:
|
||||
Feature1:
|
||||
Description: Feature
|
||||
");
|
||||
|
||||
_dataMigrationManager.Update("Feature1");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
135
src/Orchard.Tests/DataMigration/SchemaBuilderTests.cs
Normal file
135
src/Orchard.Tests/DataMigration/SchemaBuilderTests.cs
Normal file
@@ -0,0 +1,135 @@
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using Autofac;
|
||||
using NHibernate;
|
||||
using NUnit.Framework;
|
||||
using Orchard.Data;
|
||||
using Orchard.DataMigration.Interpreters;
|
||||
using Orchard.DataMigration.Schema;
|
||||
using Orchard.Environment.Configuration;
|
||||
using Orchard.Tests.ContentManagement;
|
||||
using System.IO;
|
||||
|
||||
namespace Orchard.Tests.DataMigration {
|
||||
[TestFixture]
|
||||
public class SchemaBuilderTests {
|
||||
private IContainer _container;
|
||||
private ISessionFactory _sessionFactory;
|
||||
private string _databaseFileName;
|
||||
private SchemaBuilder _schemaBuilder;
|
||||
private DefaultDataMigrationInterpreter _interpreter;
|
||||
|
||||
[SetUp]
|
||||
public void Setup() {
|
||||
_databaseFileName = Path.GetTempFileName();
|
||||
_sessionFactory = DataUtility.CreateSessionFactory(
|
||||
_databaseFileName);
|
||||
|
||||
var builder = new ContainerBuilder();
|
||||
|
||||
builder.RegisterInstance(new ShellSettings { DataTablePrefix = "TEST_", DataProvider = "SQLite" });
|
||||
|
||||
var session = _sessionFactory.OpenSession();
|
||||
builder.RegisterType<DefaultDataMigrationInterpreter>().As<IDataMigrationInterpreter>();
|
||||
builder.RegisterInstance(new DefaultContentManagerTests.TestSessionLocator(session)).As<ISessionLocator>();
|
||||
builder.RegisterInstance(new ShellSettings { DataProvider = "SQLite", DataTablePrefix = "TEST_" }).As<ShellSettings>();
|
||||
builder.RegisterType<SqLiteCommandInterpreter>().As<ICommandInterpreter>();
|
||||
_container = builder.Build();
|
||||
|
||||
_interpreter = _container.Resolve<IDataMigrationInterpreter>() as DefaultDataMigrationInterpreter;
|
||||
_schemaBuilder = new SchemaBuilder(_interpreter);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AllMethodsShouldBeCalledSuccessfully() {
|
||||
|
||||
_schemaBuilder = new SchemaBuilder(new NullInterpreter());
|
||||
|
||||
_schemaBuilder
|
||||
.CreateTable("User", table => table
|
||||
.ContentPartRecord()
|
||||
.Column("Id", DbType.Int32, column => column.PrimaryKey())
|
||||
.Column("Firstname", DbType.String, column => column.WithLength(255))
|
||||
.Column("Lastname", DbType.String, column => column.WithPrecision(0).WithScale(1)))
|
||||
.CreateTable("Address", table => table
|
||||
.VersionedContentPartRecord()
|
||||
.Column("City", DbType.String)
|
||||
.Column("ZIP", DbType.Int32, column => column.Unique())
|
||||
.Column("UserId", DbType.Int32, column => column.NotNull()))
|
||||
.CreateForeignKey("User_Address", "User", new[] { "UserId" }, "User", new[] { "Id" })
|
||||
.AlterTable("User", table => table
|
||||
.AddColumn("Age", DbType.Int32))
|
||||
.AlterTable("User", table => table
|
||||
.DropColumn("Lastname"))
|
||||
.AlterTable("User", table => table
|
||||
.CreateIndex("IDX_XYZ", "NickName"))
|
||||
.AlterTable("User", table => table
|
||||
.DropIndex("IDX_XYZ"))
|
||||
.DropForeignKey("Addresse", "User_Address")
|
||||
.DropTable("Address")
|
||||
.ExecuteSql("drop database", statement => statement.ForProvider("SQLite"))
|
||||
.ExecuteSql("DROP DATABASE", statement => statement.ForProvider("SQLServer"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CreateCommandShouldBeHandled() {
|
||||
|
||||
_schemaBuilder
|
||||
.CreateTable("User", table => table
|
||||
.Column("Id", DbType.Int32, column => column.PrimaryKey())
|
||||
.Column("Firstname", DbType.String, column => column.WithLength(255))
|
||||
.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("''"))
|
||||
);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DropTableCommandShouldBeHandled() {
|
||||
|
||||
_schemaBuilder
|
||||
.DropTable("User");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CustomSqlStatementsShouldBeHandled() {
|
||||
|
||||
_schemaBuilder
|
||||
.ExecuteSql("select 1");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AlterTableCommandShouldBeHandled() {
|
||||
|
||||
_schemaBuilder
|
||||
.CreateTable("User", table => table
|
||||
.Column("Firstname", DbType.String, column => column.WithLength(255))
|
||||
.Column("Lastname", DbType.String, column => column.WithLength(100).NotNull()))
|
||||
.AlterTable("User", table => table
|
||||
.AddColumn("Age", DbType.Int32))
|
||||
.AlterTable("User", table => table
|
||||
.AlterColumn("Lastname", column => column.WithDefault("'John'")))
|
||||
.AlterTable("User", table => table
|
||||
.DropColumn("Firstname")
|
||||
);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ForeignKeyShouldBeCreatedAndRemoved() {
|
||||
|
||||
_schemaBuilder
|
||||
.CreateTable("User", table => table
|
||||
.Column("Id", DbType.Int32, column => column.PrimaryKey())
|
||||
.Column("Firstname", DbType.String, column => column.WithLength(255))
|
||||
.Column("Lastname", DbType.String, column => column.WithPrecision(0).WithScale(1)))
|
||||
.CreateTable("Address", table => table
|
||||
.Column("City", DbType.String)
|
||||
.Column("ZIP", DbType.Int32, column => column.Unique())
|
||||
.Column("UserId", DbType.Int32, column => column.NotNull()))
|
||||
.CreateForeignKey("User_Address", "User", new[] { "UserId" }, "User", new[] { "Id" })
|
||||
.DropForeignKey("User", "User_Address");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
27
src/Orchard.Tests/DataMigration/Utilities/NullInterpreter.cs
Normal file
27
src/Orchard.Tests/DataMigration/Utilities/NullInterpreter.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
using System;
|
||||
using Orchard.DataMigration.Interpreters;
|
||||
using Orchard.DataMigration.Schema;
|
||||
|
||||
public class NullInterpreter : IDataMigrationInterpreter {
|
||||
public void Visit(SchemaCommand command) {
|
||||
}
|
||||
|
||||
public void Visit(CreateTableCommand command) {
|
||||
}
|
||||
|
||||
public void Visit(DropTableCommand command) {
|
||||
}
|
||||
|
||||
public void Visit(AlterTableCommand command) {
|
||||
}
|
||||
|
||||
public void Visit(SqlStatementCommand command) {
|
||||
}
|
||||
|
||||
public void Visit(CreateForeignKeyCommand command) {
|
||||
}
|
||||
|
||||
public void Visit(DropForeignKeyCommand command) {
|
||||
}
|
||||
}
|
@@ -180,8 +180,9 @@
|
||||
<Compile Include="ContentManagement\Records\GammaRecord.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="DataMigration\DataMigrationCommandsTests.cs" />
|
||||
<Compile Include="DataMigration\SchemaBuilderTests.cs" />
|
||||
<Compile Include="DataMigration\DataMigrationTests.cs" />
|
||||
<Compile Include="DataMigration\Utilities\NullInterpreter.cs" />
|
||||
<Compile Include="DataUtility.cs" />
|
||||
<Compile Include="Data\Builders\SessionFactoryBuilderTests.cs" />
|
||||
<Compile Include="Data\RepositoryTests.cs" />
|
||||
|
Reference in New Issue
Block a user