React to Migrations changes
This commit is contained in:
parent
4139f3f18b
commit
a09d8a4ead
|
|
@ -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;
|
||||||
|
|
@ -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)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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>()
|
||||||
|
|
|
||||||
|
|
@ -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; }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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; }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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; }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue