deploy-157 - simplify tables creation in migrations
This commit is contained in:
@@ -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<T>()
|
||||
{
|
||||
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 };
|
||||
|
||||
@@ -8,6 +8,8 @@ namespace Umbraco.Core.Persistence.Migrations.Syntax.Create
|
||||
{
|
||||
public interface ICreateBuilder : IFluentSyntax
|
||||
{
|
||||
void Table<T>();
|
||||
|
||||
ICreateTableWithColumnSyntax Table(string tableName);
|
||||
ICreateColumnOnTableSyntax Column(string columnName);
|
||||
|
||||
|
||||
70
src/Umbraco.Tests/Migrations/CreateTableMigrationTests.cs
Normal file
70
src/Umbraco.Tests/Migrations/CreateTableMigrationTests.cs
Normal file
@@ -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<IMigrationEntryService>(),
|
||||
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<NodeDto>();
|
||||
}
|
||||
|
||||
public override void Down()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -179,6 +179,7 @@
|
||||
<Compile Include="CallContextTests.cs" />
|
||||
<Compile Include="IO\ShadowFileSystemTests.cs" />
|
||||
<Compile Include="Logging\ConsoleLogger.cs" />
|
||||
<Compile Include="Migrations\CreateTableMigrationTests.cs" />
|
||||
<Compile Include="Migrations\MigrationIssuesTests.cs" />
|
||||
<Compile Include="Persistence\LocksTests.cs" />
|
||||
<Compile Include="Persistence\BulkDataReaderTests.cs" />
|
||||
|
||||
Reference in New Issue
Block a user