This commit is contained in:
Brice Lambson 2015-08-17 10:24:21 -07:00
parent 97e66878a7
commit e8fc5a970a
7 changed files with 41 additions and 92 deletions

View File

@ -80,11 +80,19 @@ namespace Microsoft.AspNet.Diagnostics.Entity
{
var databaseExists = creator.Exists();
var migrator = dbContext.GetService<IMigrator>();
var historyRepository = dbContext.GetService<IHistoryRepository>();
var migrationsAssembly = dbContext.GetService<IMigrationsAssembly>();
var modelDiffer = dbContext.GetService<IMigrationsModelDiffer>();
var pendingMigrations = migrator.GetUnappliedMigrations().Select(m => m.Id);
var appliedMigrations = historyRepository.GetAppliedMigrations();
var pendingMigrations = (
from m in migrationsAssembly.Migrations
where !appliedMigrations.Any(
r => string.Equals(r.MigrationId, m.Id, StringComparison.OrdinalIgnoreCase))
select m.Id)
.ToList();
var pendingModelChanges = migrator.HasPendingModelChanges();
var pendingModelChanges = modelDiffer.HasDifferences(migrationsAssembly.ModelSnapshot?.Model, dbContext.Model);
if ((!databaseExists && pendingMigrations.Any()) || pendingMigrations.Any() || pendingModelChanges)
{

View File

@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
@ -10,19 +9,14 @@ using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Diagnostics.Entity.FunctionalTests.Helpers;
using Microsoft.AspNet.Diagnostics.Entity.Tests.Helpers;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.TestHost;
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Infrastructure;
using Microsoft.Data.Entity.Internal;
using Microsoft.Data.Entity.Migrations;
using Microsoft.Data.Entity.Migrations.Infrastructure;
using Microsoft.Data.Entity.Storage;
using Microsoft.Data.Entity.Utilities;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
using Xunit;
using Microsoft.AspNet.Diagnostics.Entity.Tests.Helpers;
namespace Microsoft.AspNet.Diagnostics.Entity.Tests
{
@ -162,7 +156,7 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
{
using (var db = context.ApplicationServices.GetService<BloggingContextWithPendingModelChanges>())
{
db.Database.ApplyMigrations();
db.Database.Migrate();
db.Blogs.Add(new Blog());
db.SaveChanges();

View File

@ -13,5 +13,8 @@
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View File

@ -1,24 +1,14 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Infrastructure;
using Microsoft.Data.Entity.Migrations;
using System;
using System.Linq;
using System.Reflection;
namespace Microsoft.AspNet.Diagnostics.Entity.Tests
{
public class BloggingContext : DbContext
{
protected static readonly string CurrentProductVersion = typeof(Migrator)
.GetTypeInfo()
.Assembly
.GetCustomAttributes<AssemblyInformationalVersionAttribute>()
.Single()
.InformationalVersion;
protected BloggingContext(DbContextOptions options)
: base(options)
{ }

View File

@ -4,9 +4,9 @@
using System;
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Infrastructure;
using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.Migrations;
using Microsoft.Data.Entity.Migrations.Builders;
using Microsoft.Data.Entity.Migrations.Infrastructure;
using Microsoft.Data.Entity.SqlServer.Metadata;
namespace Microsoft.AspNet.Diagnostics.Entity.Tests
{
@ -26,10 +26,10 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
return new BloggingContextWithMigrations(options);
}
[ContextType(typeof(BloggingContextWithMigrations))]
[DbContext(typeof(BloggingContextWithMigrations))]
public class BloggingContextWithMigrationsModelSnapshot : ModelSnapshot
{
public override void BuildModel(ModelBuilder builder)
protected override void BuildModel(ModelBuilder builder)
{
builder.Entity("Blogging.Models.Blog", b =>
{
@ -40,7 +40,7 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
}
}
[ContextType(typeof(BloggingContextWithMigrations))]
[DbContext(typeof(BloggingContextWithMigrations))]
public class MigrationOne : Migration
{
public override string Id
@ -48,34 +48,26 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
get { return "111111111111111_MigrationOne"; }
}
public override string ProductVersion
{
get { return CurrentProductVersion; }
}
public override IModel TargetModel => new BloggingContextWithMigrationsModelSnapshot().Model;
public override void BuildTargetModel(ModelBuilder modelBuilder)
{
new BloggingContextWithMigrationsModelSnapshot().BuildModel(modelBuilder);
}
public override void Up(MigrationBuilder migrationBuilder)
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable("Blog",
c => new
{
BlogId = c.Column<string>("int").Annotation("SqlServer:ValueGeneration", "Identity"),
Name = c.Column<string>("nvarchar(max)", nullable: true),
BlogId = c.Column<int>().Annotation("SqlServer:ValueGenerationStrategy", SqlServerIdentityStrategy.IdentityColumn),
Name = c.Column<string>(isNullable: true),
})
.PrimaryKey("PK_Blog", t => t.BlogId);
}
public override void Down(MigrationBuilder migrationBuilder)
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable("Blog");
}
}
[ContextType(typeof(BloggingContextWithMigrations))]
[DbContext(typeof(BloggingContextWithMigrations))]
public class MigrationTwo : Migration
{
public override string Id
@ -83,20 +75,9 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
get { return "222222222222222_MigrationTwo"; }
}
public override string ProductVersion
{
get { return CurrentProductVersion; }
}
public override IModel TargetModel => new BloggingContextWithMigrationsModelSnapshot().Model;
public override void BuildTargetModel(ModelBuilder modelBuilder)
{
new BloggingContextWithMigrationsModelSnapshot().BuildModel(modelBuilder);
}
public override void Up(MigrationBuilder migrationBuilder)
{ }
public override void Down(MigrationBuilder migrationBuilder)
protected override void Up(MigrationBuilder migrationBuilder)
{ }
}
}

View File

@ -5,8 +5,6 @@ using System;
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Infrastructure;
using Microsoft.Data.Entity.Migrations;
using Microsoft.Data.Entity.Migrations.Builders;
using Microsoft.Data.Entity.Migrations.Infrastructure;
namespace Microsoft.AspNet.Diagnostics.Entity.Tests
{
@ -16,15 +14,15 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
: base(provider, options)
{ }
[ContextType(typeof(BloggingContextWithPendingModelChanges))]
[DbContext(typeof(BloggingContextWithPendingModelChanges))]
public class BloggingModelSnapshot : ModelSnapshot
{
public override void BuildModel(ModelBuilder modelBuilder)
protected override void BuildModel(ModelBuilder modelBuilder)
{
}
}
[ContextType(typeof(BloggingContextWithPendingModelChanges))]
[DbContext(typeof(BloggingContextWithPendingModelChanges))]
public partial class MigrationOne : Migration
{
public override string Id
@ -32,19 +30,7 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
get { return "111111111111111_MigrationOne"; }
}
public override string ProductVersion
{
get { return CurrentProductVersion; }
}
public override void BuildTargetModel(ModelBuilder modelBuilder)
{
}
public override void Up(MigrationBuilder migrationBuilder)
{ }
public override void Down(MigrationBuilder migrationBuilder)
protected override void Up(MigrationBuilder migrationBuilder)
{ }
}
}

View File

@ -1,13 +1,11 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Infrastructure;
using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.Migrations;
using Microsoft.Data.Entity.Migrations.Builders;
using Microsoft.Data.Entity.Migrations.Infrastructure;
using System;
namespace Microsoft.AspNet.Diagnostics.Entity.Tests
{
@ -17,16 +15,16 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
: base(provider, options)
{ }
[ContextType(typeof(BloggingContextWithSnapshotThatThrows))]
[DbContext(typeof(BloggingContextWithSnapshotThatThrows))]
public class BloggingContextWithSnapshotThatThrowsModelSnapshot : ModelSnapshot
{
public override void BuildModel(ModelBuilder modelBuilder)
protected override void BuildModel(ModelBuilder modelBuilder)
{
throw new Exception("Welcome to the invalid snapshot!");
}
}
[ContextType(typeof(BloggingContextWithSnapshotThatThrows))]
[DbContext(typeof(BloggingContextWithSnapshotThatThrows))]
public class MigrationOne : Migration
{
public override string Id
@ -34,23 +32,12 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
get { return "111111111111111_MigrationOne"; }
}
public override string ProductVersion
{
get { return CurrentProductVersion; }
}
public override IModel TargetModel => new BloggingContextWithSnapshotThatThrowsModelSnapshot().Model;
public override void BuildTargetModel(ModelBuilder modelBuilder)
{
new BloggingContextWithSnapshotThatThrowsModelSnapshot().BuildModel(modelBuilder);
}
public override void Up(MigrationBuilder migrationBuilder)
protected override void Up(MigrationBuilder migrationBuilder)
{
throw new Exception("Welcome to the invalid migration!");
}
public override void Down(MigrationBuilder migrationBuilder)
{ }
}
}
}