React to Migrations changes

This commit is contained in:
Brice Lambson 2015-02-09 20:15:43 -08:00 committed by Brice Lambson
parent 4139f3f18b
commit a09d8a4ead
7 changed files with 34 additions and 50 deletions

View File

@ -8,14 +8,9 @@ using Microsoft.AspNet.Diagnostics.Entity.Views;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.RequestContainer; using Microsoft.AspNet.RequestContainer;
using Microsoft.Data.Entity; using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Infrastructure;
using Microsoft.Data.Entity.Internal; using Microsoft.Data.Entity.Internal;
using Microsoft.Data.Entity.Relational; using Microsoft.Data.Entity.Relational;
using Microsoft.Data.Entity.Relational.Migrations; using Microsoft.Data.Entity.Relational.Migrations;
using Microsoft.Data.Entity.Relational.Migrations.Infrastructure;
using Microsoft.Data.Entity.Relational.Migrations.Utilities;
using Microsoft.Data.Entity.Utilities;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging; using Microsoft.Framework.Logging;
using System; using System;
using System.Linq; using System.Linq;
@ -54,7 +49,7 @@ namespace Microsoft.AspNet.Diagnostics.Entity
try try
{ {
#if !ASPNETCORE50 #if !ASPNETCORE50
// TODO This probably isn't the correct place for this workaround, it // TODO This probably isn't the correct place for this workaround, it
// needs to be called before anything is written to CallContext // needs to be called before anything is written to CallContext
// http://msdn.microsoft.com/en-us/library/dn458353(v=vs.110).aspx // http://msdn.microsoft.com/en-us/library/dn458353(v=vs.110).aspx
System.Configuration.ConfigurationManager.GetSection("system.xml/xmlReader"); System.Configuration.ConfigurationManager.GetSection("system.xml/xmlReader");
@ -89,14 +84,9 @@ namespace Microsoft.AspNet.Diagnostics.Entity
var migrator = ((IAccessor<Migrator>)dbContext.Database).Service; var migrator = ((IAccessor<Migrator>)dbContext.Database).Service;
var pendingMigrations = migrator.GetPendingMigrations().Select(m => m.GetMigrationId()); var pendingMigrations = migrator.GetUnappliedMigrations().Select(m => m.Id);
var pendingModelChanges = true; var pendingModelChanges = migrator.HasPendingModelChanges();
var snapshot = migrator.MigrationAssembly.ModelSnapshot;
if (snapshot != null)
{
pendingModelChanges = migrator.ModelDiffer.Diff(snapshot.Model, dbContext.Model).Any();
}
if ((!databaseExists && pendingMigrations.Any()) || pendingMigrations.Any() || pendingModelChanges) if ((!databaseExists && pendingMigrations.Any()) || pendingMigrations.Any() || pendingModelChanges)
{ {
@ -128,7 +118,7 @@ namespace Microsoft.AspNet.Diagnostics.Entity
logger.WriteVerbose(Strings.DatabaseErrorPage_NoRecordedException); logger.WriteVerbose(Strings.DatabaseErrorPage_NoRecordedException);
return false; return false;
} }
bool match = false; bool match = false;
for (var e = exception; e != null && !match; e = e.InnerException) for (var e = exception; e != null && !match; e = e.InnerException)
{ {

View File

@ -11,15 +11,12 @@ using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.TestHost; using Microsoft.AspNet.TestHost;
using Microsoft.Data.Entity; using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Infrastructure;
using Microsoft.Data.Entity.Internal; using Microsoft.Data.Entity.Internal;
using Microsoft.Data.Entity.Relational.Migrations; using Microsoft.Data.Entity.Relational.Migrations.History;
using Microsoft.Data.Entity.Relational.Migrations.Infrastructure;
using Microsoft.Data.Entity.Relational.Migrations.Utilities;
using Microsoft.Data.Entity.Utilities;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Xunit; using Xunit;
using Microsoft.AspNet.Diagnostics.Entity.Tests.Helpers; using Microsoft.AspNet.Diagnostics.Entity.Tests.Helpers;
using Microsoft.Data.Entity.Infrastructure;
namespace Microsoft.AspNet.Diagnostics.Entity.Tests namespace Microsoft.AspNet.Diagnostics.Entity.Tests
{ {
@ -105,11 +102,11 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
Assert.True(db.Database.AsRelational().Exists()); Assert.True(db.Database.AsRelational().Exists());
var migrator = ((IAccessor<Migrator>)db.Database).Service; var historyRepository = ((IAccessor<IServiceProvider>)db).Service.GetRequiredService<DbContextService<IHistoryRepository>>().Service;
var appliedMigrations = migrator.GetDatabaseMigrations(); var appliedMigrations = historyRepository.GetAppliedMigrations();
Assert.Equal(2, appliedMigrations.Count); Assert.Equal(2, appliedMigrations.Count);
Assert.Equal("111111111111111_MigrationOne", appliedMigrations.ElementAt(0).GetMigrationId()); Assert.Equal("111111111111111_MigrationOne", appliedMigrations.ElementAt(0).MigrationId);
Assert.Equal("222222222222222_MigrationTwo", appliedMigrations.ElementAt(1).GetMigrationId()); Assert.Equal("222222222222222_MigrationTwo", appliedMigrations.ElementAt(1).MigrationId);
} }
} }
} }
@ -208,7 +205,7 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
await server.CreateClient().PostAsync("http://localhost" + MigrationsEndPointOptions.DefaultPath, formData)); await server.CreateClient().PostAsync("http://localhost" + MigrationsEndPointOptions.DefaultPath, formData));
Assert.Equal(StringsHelpers.GetResourceString("FormatMigrationsEndPointMiddleware_Exception", typeof(BloggingContextWithSnapshotThatThrows)), ex.Message); Assert.Equal(StringsHelpers.GetResourceString("FormatMigrationsEndPointMiddleware_Exception", typeof(BloggingContextWithSnapshotThatThrows)), ex.Message);
Assert.Equal("Welcome to the invalid snapshot!", ex.InnerException.Message); Assert.Equal("Welcome to the invalid migration!", ex.InnerException.Message);
} }
} }
} }

View File

@ -26,7 +26,7 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
{ {
_connectionString = new SqlConnectionStringBuilder _connectionString = new SqlConnectionStringBuilder
{ {
DataSource = @"(localdb)\v11.0", DataSource = @"(localdb)\MSSQLLocalDB",
InitialCatalog = name, InitialCatalog = name,
IntegratedSecurity = true, IntegratedSecurity = true,
ConnectTimeout = 30 ConnectTimeout = 30

View File

@ -2,7 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.Data.Entity; using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Relational.Migrations.Infrastructure; using Microsoft.Data.Entity.Relational.Migrations;
using System; using System;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
@ -11,7 +11,7 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
{ {
public class BloggingContext : DbContext public class BloggingContext : DbContext
{ {
protected static readonly string CurrentProductVersion = typeof(HistoryRepository) protected static readonly string CurrentProductVersion = typeof(Migrator)
.GetTypeInfo() .GetTypeInfo()
.Assembly .Assembly
.GetCustomAttributes<AssemblyInformationalVersionAttribute>() .GetCustomAttributes<AssemblyInformationalVersionAttribute>()

View File

@ -7,8 +7,7 @@ using Microsoft.Data.Entity.Relational.Migrations;
using Microsoft.Data.Entity.Relational.Migrations.Builders; using Microsoft.Data.Entity.Relational.Migrations.Builders;
using Microsoft.Data.Entity.Relational.Migrations.Infrastructure; using Microsoft.Data.Entity.Relational.Migrations.Infrastructure;
using System; using System;
using System.Linq; using System.Collections.Generic;
using System.Reflection;
namespace Microsoft.AspNet.Diagnostics.Entity.Tests namespace Microsoft.AspNet.Diagnostics.Entity.Tests
{ {
@ -51,19 +50,19 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
} }
[ContextType(typeof(BloggingContextWithMigrations))] [ContextType(typeof(BloggingContextWithMigrations))]
public class MigrationOne : Migration, IMigrationMetadata public class MigrationOne : Migration
{ {
string IMigrationMetadata.MigrationId public override string Id
{ {
get { return "111111111111111_MigrationOne"; } get { return "111111111111111_MigrationOne"; }
} }
string IMigrationMetadata.ProductVersion public override string ProductVersion
{ {
get { return CurrentProductVersion; } get { return CurrentProductVersion; }
} }
IModel IMigrationMetadata.TargetModel public override IModel Target
{ {
get { return new BloggingContextWithMigrationsModelSnapshot().Model; } get { return new BloggingContextWithMigrationsModelSnapshot().Model; }
} }
@ -73,10 +72,10 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
migrationBuilder.CreateTable("Blog", migrationBuilder.CreateTable("Blog",
c => new c => new
{ {
BlogId = c.Int(nullable: false, identity: true), BlogId = c.Column("int", annotations: new Dictionary<string, string> { { "SqlServer:ValueGeneration", "Identity" } }),
Name = c.String(), Name = c.Column("nvarchar(max)", nullable: true),
}) })
.PrimaryKey("PK_Blog", t => t.BlogId); .PrimaryKey(t => t.BlogId, name: "PK_Blog");
} }
public override void Down(MigrationBuilder migrationBuilder) public override void Down(MigrationBuilder migrationBuilder)
@ -86,19 +85,19 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
} }
[ContextType(typeof(BloggingContextWithMigrations))] [ContextType(typeof(BloggingContextWithMigrations))]
public class MigrationTwo : Migration, IMigrationMetadata public class MigrationTwo : Migration
{ {
string IMigrationMetadata.MigrationId public override string Id
{ {
get { return "222222222222222_MigrationTwo"; } get { return "222222222222222_MigrationTwo"; }
} }
string IMigrationMetadata.ProductVersion public override string ProductVersion
{ {
get { return CurrentProductVersion; } get { return CurrentProductVersion; }
} }
IModel IMigrationMetadata.TargetModel public override IModel Target
{ {
get { return new BloggingContextWithMigrationsModelSnapshot().Model; } get { return new BloggingContextWithMigrationsModelSnapshot().Model; }
} }

View File

@ -26,19 +26,19 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
} }
[ContextType(typeof(BloggingContextWithPendingModelChanges))] [ContextType(typeof(BloggingContextWithPendingModelChanges))]
public partial class MigrationOne : Migration, IMigrationMetadata public partial class MigrationOne : Migration
{ {
string IMigrationMetadata.MigrationId public override string Id
{ {
get { return "111111111111111_MigrationOne"; } get { return "111111111111111_MigrationOne"; }
} }
string IMigrationMetadata.ProductVersion public override string ProductVersion
{ {
get { return CurrentProductVersion; } get { return CurrentProductVersion; }
} }
IModel IMigrationMetadata.TargetModel public override IModel Target
{ {
get { return new BasicModelBuilder().Model; } get { return new BasicModelBuilder().Model; }
} }

View File

@ -7,8 +7,6 @@ using Microsoft.Data.Entity.Relational.Migrations;
using Microsoft.Data.Entity.Relational.Migrations.Builders; using Microsoft.Data.Entity.Relational.Migrations.Builders;
using Microsoft.Data.Entity.Relational.Migrations.Infrastructure; using Microsoft.Data.Entity.Relational.Migrations.Infrastructure;
using System; using System;
using System.Linq;
using System.Reflection;
namespace Microsoft.AspNet.Diagnostics.Entity.Tests namespace Microsoft.AspNet.Diagnostics.Entity.Tests
{ {
@ -31,19 +29,19 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
} }
[ContextType(typeof(BloggingContextWithSnapshotThatThrows))] [ContextType(typeof(BloggingContextWithSnapshotThatThrows))]
public class MigrationOne : Migration, IMigrationMetadata public class MigrationOne : Migration
{ {
string IMigrationMetadata.MigrationId public override string Id
{ {
get { return "111111111111111_MigrationOne"; } get { return "111111111111111_MigrationOne"; }
} }
string IMigrationMetadata.ProductVersion public override string ProductVersion
{ {
get { return CurrentProductVersion; } get { return CurrentProductVersion; }
} }
IModel IMigrationMetadata.TargetModel public override IModel Target
{ {
get { return new BloggingContextWithSnapshotThatThrowsModelSnapshot().Model; } get { return new BloggingContextWithSnapshotThatThrowsModelSnapshot().Model; }
} }