From ef48baf7aee467772c4627c8011c19e1a8eac7cd Mon Sep 17 00:00:00 2001 From: Stephan Date: Fri, 23 Dec 2016 10:31:14 +0100 Subject: [PATCH] deploy-157 - simplify tables creation in migrations --- .../Migrations/Syntax/Create/CreateBuilder.cs | 19 +++++ .../Syntax/Create/ICreateBuilder.cs | 2 + .../Migrations/CreateTableMigrationTests.cs | 70 +++++++++++++++++++ src/Umbraco.Tests/Umbraco.Tests.csproj | 1 + 4 files changed, 92 insertions(+) create mode 100644 src/Umbraco.Tests/Migrations/CreateTableMigrationTests.cs diff --git a/src/Umbraco.Core/Persistence/Migrations/Syntax/Create/CreateBuilder.cs b/src/Umbraco.Core/Persistence/Migrations/Syntax/Create/CreateBuilder.cs index e3ad6f972d..6afb0a4ca7 100644 --- a/src/Umbraco.Core/Persistence/Migrations/Syntax/Create/CreateBuilder.cs +++ b/src/Umbraco.Core/Persistence/Migrations/Syntax/Create/CreateBuilder.cs @@ -6,6 +6,7 @@ using Umbraco.Core.Persistence.Migrations.Syntax.Create.Expressions; using Umbraco.Core.Persistence.Migrations.Syntax.Create.ForeignKey; using Umbraco.Core.Persistence.Migrations.Syntax.Create.Index; using Umbraco.Core.Persistence.Migrations.Syntax.Create.Table; +using Umbraco.Core.Persistence.Migrations.Syntax.Execute.Expressions; using Umbraco.Core.Persistence.Migrations.Syntax.Expressions; using Umbraco.Core.Persistence.SqlSyntax; @@ -27,6 +28,24 @@ namespace Umbraco.Core.Persistence.Migrations.Syntax.Create _databaseProviders = databaseProviders; } + public void Table() + { + var tableDefinition = DefinitionFactory.GetTableDefinition(_sqlSyntax, typeof(T)); + + AddSql(_sqlSyntax.Format(tableDefinition)); + AddSql(_sqlSyntax.FormatPrimaryKey(tableDefinition)); + foreach (var sql in _sqlSyntax.Format(tableDefinition.ForeignKeys)) + AddSql(sql); + foreach (var sql in _sqlSyntax.Format(tableDefinition.Indexes)) + AddSql(sql); + } + + private void AddSql(string sql) + { + var expression = new ExecuteSqlStatementExpression(_context.CurrentDatabaseProvider, _databaseProviders, _sqlSyntax) { SqlStatement = sql }; + _context.Expressions.Add(expression); + } + public ICreateTableWithColumnSyntax Table(string tableName) { var expression = new CreateTableExpression(_context.CurrentDatabaseProvider, _databaseProviders, _sqlSyntax) { TableName = tableName }; diff --git a/src/Umbraco.Core/Persistence/Migrations/Syntax/Create/ICreateBuilder.cs b/src/Umbraco.Core/Persistence/Migrations/Syntax/Create/ICreateBuilder.cs index e079127705..3cb2fe7e9f 100644 --- a/src/Umbraco.Core/Persistence/Migrations/Syntax/Create/ICreateBuilder.cs +++ b/src/Umbraco.Core/Persistence/Migrations/Syntax/Create/ICreateBuilder.cs @@ -8,6 +8,8 @@ namespace Umbraco.Core.Persistence.Migrations.Syntax.Create { public interface ICreateBuilder : IFluentSyntax { + void Table(); + ICreateTableWithColumnSyntax Table(string tableName); ICreateColumnOnTableSyntax Column(string columnName); diff --git a/src/Umbraco.Tests/Migrations/CreateTableMigrationTests.cs b/src/Umbraco.Tests/Migrations/CreateTableMigrationTests.cs new file mode 100644 index 0000000000..11055ffc51 --- /dev/null +++ b/src/Umbraco.Tests/Migrations/CreateTableMigrationTests.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Moq; +using NUnit.Framework; +using Semver; +using Umbraco.Core; +using Umbraco.Core.Logging; +using Umbraco.Core.Models.Rdbms; +using Umbraco.Core.Persistence; +using Umbraco.Core.Persistence.Migrations; +using Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSevenFiveZero; +using Umbraco.Core.Persistence.SqlSyntax; +using Umbraco.Core.Services; +using Umbraco.Tests.TestHelpers; +using GlobalSettings = Umbraco.Core.Configuration.GlobalSettings; + +namespace Umbraco.Tests.Migrations +{ + [TestFixture] + [DatabaseTestBehavior(DatabaseBehavior.EmptyDbFilePerTest)] + public class CreateTableMigrationTests : BaseDatabaseFactoryTest + { + [Test] + public void CreateTableOfTDto() + { + var logger = new DebugDiagnosticsLogger(); + + var migrationRunner = new MigrationRunner( + Mock.Of(), + logger, + new SemVersion(0, 0, 0), + new SemVersion(1, 0, 0), + "Test", + + // explicit migrations + new CreateTableOfTDtoMigration(SqlSyntax, logger) + ); + + var db = new UmbracoDatabase("Datasource=|DataDirectory|UmbracoPetaPocoTests.sdf;Flush Interval=1;", Constants.DatabaseProviders.SqlCe, Logger); + + var upgraded = migrationRunner.Execute(db, DatabaseProviders.SqlServerCE, true); + Assert.IsTrue(upgraded); + + var helper = new DatabaseSchemaHelper(db, logger, SqlSyntax); + var exists = helper.TableExist("umbracoNode"); + Assert.IsTrue(exists); + } + + [Migration("1.0.0", 0, "Test")] + public class CreateTableOfTDtoMigration : MigrationBase + { + public CreateTableOfTDtoMigration(ISqlSyntaxProvider sqlSyntax, ILogger logger) + : base(sqlSyntax, logger) + { } + + public override void Up() + { + Create.Table(); + } + + public override void Down() + { + throw new NotImplementedException(); + } + } + } +} diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 1f30ff95e5..f344cb4ae4 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -179,6 +179,7 @@ +