From 105d3353cf32448f7f62e1528778a48accfba0b4 Mon Sep 17 00:00:00 2001 From: Rowan Miller Date: Mon, 23 Mar 2015 15:54:35 -0700 Subject: [PATCH] :fire: Remove migrations end point --- .../DatabaseErrorPageExtensions.cs | 9 +- .../DatabaseErrorPageOptions.cs | 19 -- .../MigrationsEndPointExtensions.cs | 27 --- .../MigrationsEndPointMiddleware.cs | 117 ---------- .../MigrationsEndPointOptions.cs | 24 -- .../Properties/Strings.Designer.cs | 208 ------------------ .../Strings.resx | 39 ---- .../Views/DatabaseErrorPage.cs | 162 ++------------ .../Views/DatabaseErrorPage.cshtml | 47 ---- src/PageGenerator/project.json | 1 + .../DatabaseErrorPageMiddlewareTest.cs | 85 ------- .../MigrationsEndPointMiddlewareTest.cs | 205 ----------------- .../DatabaseErrorPageOptionsTest.cs | 15 -- .../DatabaseErrorPageTest.cs | 39 ---- 14 files changed, 23 insertions(+), 974 deletions(-) delete mode 100644 src/Microsoft.AspNet.Diagnostics.Entity/MigrationsEndPointExtensions.cs delete mode 100644 src/Microsoft.AspNet.Diagnostics.Entity/MigrationsEndPointMiddleware.cs delete mode 100644 src/Microsoft.AspNet.Diagnostics.Entity/MigrationsEndPointOptions.cs delete mode 100644 test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/MigrationsEndPointMiddlewareTest.cs diff --git a/src/Microsoft.AspNet.Diagnostics.Entity/DatabaseErrorPageExtensions.cs b/src/Microsoft.AspNet.Diagnostics.Entity/DatabaseErrorPageExtensions.cs index 3eac097cc8..75604af1bc 100644 --- a/src/Microsoft.AspNet.Diagnostics.Entity/DatabaseErrorPageExtensions.cs +++ b/src/Microsoft.AspNet.Diagnostics.Entity/DatabaseErrorPageExtensions.cs @@ -21,14 +21,7 @@ namespace Microsoft.AspNet.Builder Check.NotNull(builder, "builder"); Check.NotNull(options, "options"); - builder = builder.UseMiddleware(options); - - if(options.EnableMigrationCommands) - { - builder.UseMigrationsEndPoint(new MigrationsEndPointOptions { Path = options.MigrationsEndPointPath }); - } - - return builder; + return builder.UseMiddleware(options); } } } diff --git a/src/Microsoft.AspNet.Diagnostics.Entity/DatabaseErrorPageOptions.cs b/src/Microsoft.AspNet.Diagnostics.Entity/DatabaseErrorPageOptions.cs index 72b785e297..0bd2bafd80 100644 --- a/src/Microsoft.AspNet.Diagnostics.Entity/DatabaseErrorPageOptions.cs +++ b/src/Microsoft.AspNet.Diagnostics.Entity/DatabaseErrorPageOptions.cs @@ -8,11 +8,9 @@ namespace Microsoft.AspNet.Diagnostics.Entity { public class DatabaseErrorPageOptions { - private PathString _migrationsEndPointPath = MigrationsEndPointOptions.DefaultPath; private bool _defaultVisibility; private bool? _showExceptionDetails; private bool? _listMigrations; - private bool? _enableMigrationCommands; public static DatabaseErrorPageOptions ShowAll { @@ -23,21 +21,10 @@ namespace Microsoft.AspNet.Diagnostics.Entity { ShowExceptionDetails = true, ListMigrations = true, - EnableMigrationCommands = true }; } } - public virtual PathString MigrationsEndPointPath - { - get { return _migrationsEndPointPath; } - set - { - Check.NotNull(value, "value"); - _migrationsEndPointPath = value; - } - } - public virtual bool ShowExceptionDetails { get { return _showExceptionDetails ?? _defaultVisibility; } @@ -50,12 +37,6 @@ namespace Microsoft.AspNet.Diagnostics.Entity set { _listMigrations = value; } } - public virtual bool EnableMigrationCommands - { - get { return _enableMigrationCommands ?? _defaultVisibility; } - set { _enableMigrationCommands = value; } - } - public virtual void SetDefaultVisibility(bool isVisible) { _defaultVisibility = isVisible; diff --git a/src/Microsoft.AspNet.Diagnostics.Entity/MigrationsEndPointExtensions.cs b/src/Microsoft.AspNet.Diagnostics.Entity/MigrationsEndPointExtensions.cs deleted file mode 100644 index 27629808fc..0000000000 --- a/src/Microsoft.AspNet.Diagnostics.Entity/MigrationsEndPointExtensions.cs +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using JetBrains.Annotations; -using Microsoft.AspNet.Diagnostics.Entity; -using Microsoft.AspNet.Diagnostics.Entity.Utilities; - -namespace Microsoft.AspNet.Builder -{ - public static class MigrationsEndPointExtensions - { - public static IApplicationBuilder UseMigrationsEndPoint([NotNull] this IApplicationBuilder builder) - { - Check.NotNull(builder, "builder"); - - return builder.UseMigrationsEndPoint(new MigrationsEndPointOptions()); - } - - public static IApplicationBuilder UseMigrationsEndPoint([NotNull] this IApplicationBuilder builder, [NotNull] MigrationsEndPointOptions options) - { - Check.NotNull(builder, "builder"); - Check.NotNull(options, "options"); - - return builder.UseMiddleware(options); - } - } -} diff --git a/src/Microsoft.AspNet.Diagnostics.Entity/MigrationsEndPointMiddleware.cs b/src/Microsoft.AspNet.Diagnostics.Entity/MigrationsEndPointMiddleware.cs deleted file mode 100644 index 13d65e1a30..0000000000 --- a/src/Microsoft.AspNet.Diagnostics.Entity/MigrationsEndPointMiddleware.cs +++ /dev/null @@ -1,117 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Net; -using System.Threading.Tasks; -using JetBrains.Annotations; -using Microsoft.AspNet.Builder; -using Microsoft.AspNet.Diagnostics.Entity.Utilities; -using Microsoft.AspNet.Http; -using Microsoft.Data.Entity; -using Microsoft.Framework.Logging; - -namespace Microsoft.AspNet.Diagnostics.Entity -{ - public class MigrationsEndPointMiddleware - { - private readonly RequestDelegate _next; - private readonly IServiceProvider _serviceProvider; - private readonly ILogger _logger; - private readonly MigrationsEndPointOptions _options; - - public MigrationsEndPointMiddleware([NotNull] RequestDelegate next, [NotNull] IServiceProvider serviceProvider, [NotNull] ILoggerFactory loggerFactory, [NotNull] MigrationsEndPointOptions options) - { - Check.NotNull(next, "next"); - Check.NotNull(serviceProvider, "serviceProvider"); - Check.NotNull(loggerFactory, "loggerFactory"); - Check.NotNull(options, "options"); - - _next = next; - _serviceProvider = serviceProvider; - _logger = loggerFactory.CreateLogger(); - _options = options; - } - - public virtual async Task Invoke([NotNull] HttpContext context) - { - Check.NotNull(context, "context"); - - if (context.Request.Path.Equals(_options.Path)) - { - _logger.LogVerbose(Strings.FormatMigrationsEndPointMiddleware_RequestPathMatched(context.Request.Path)); - - var db = await GetDbContext(context, _logger).WithCurrentCulture(); - if (db != null) - { - try - { - _logger.LogVerbose(Strings.FormatMigrationsEndPointMiddleware_ApplyingMigrations(db.GetType().FullName)); - - db.Database.AsRelational().ApplyMigrations(); - - context.Response.StatusCode = (int)HttpStatusCode.NoContent; - context.Response.Headers.Add("Pragma", new[] { "no-cache" }); - context.Response.Headers.Add("Cache-Control", new[] { "no-cache" }); - - _logger.LogVerbose(Strings.FormatMigrationsEndPointMiddleware_Applied(db.GetType().FullName)); - } - catch (Exception ex) - { - var message = Strings.FormatMigrationsEndPointMiddleware_Exception(db.GetType().FullName); - _logger.LogError(message); - throw new InvalidOperationException(message, ex); - } - } - } - else - { - await _next(context).WithCurrentCulture(); - } - } - - private static async Task GetDbContext(HttpContext context, ILogger logger) - { - var form = await context.Request.ReadFormAsync().WithCurrentCulture(); - var contextTypeName = form["context"]; - if (string.IsNullOrWhiteSpace(contextTypeName)) - { - logger.LogError(Strings.MigrationsEndPointMiddleware_NoContextType); - await WriteErrorToResponse(context.Response, Strings.MigrationsEndPointMiddleware_NoContextType).WithCurrentCulture(); - return null; - } - - var contextType = Type.GetType(contextTypeName); - if (contextType == null) - { - var message = Strings.FormatMigrationsEndPointMiddleware_InvalidContextType(contextTypeName); - logger.LogError(message); - await WriteErrorToResponse(context.Response, message).WithCurrentCulture(); - return null; - } - - var db = (DbContext)context.RequestServices.GetService(contextType); - if (db == null) - { - var message = Strings.FormatMigrationsEndPointMiddleware_ContextNotRegistered(contextType.FullName); - logger.LogError(message); - await WriteErrorToResponse(context.Response, message).WithCurrentCulture(); - return null; - } - - return db; - } - - private static async Task WriteErrorToResponse(HttpResponse response, string error) - { - response.StatusCode = (int)HttpStatusCode.BadRequest; - response.Headers.Add("Pragma", new[] { "no-cache" }); - response.Headers.Add("Cache-Control", new[] { "no-cache" }); - response.ContentType = "text/plain"; - - // Padding to >512 to ensure IE doesn't hide the message - // http://stackoverflow.com/questions/16741062/what-rules-does-ie-use-to-determine-whether-to-show-the-entity-body - await response.WriteAsync(error.PadRight(513)).WithCurrentCulture(); - } - } -} diff --git a/src/Microsoft.AspNet.Diagnostics.Entity/MigrationsEndPointOptions.cs b/src/Microsoft.AspNet.Diagnostics.Entity/MigrationsEndPointOptions.cs deleted file mode 100644 index 01b8852119..0000000000 --- a/src/Microsoft.AspNet.Diagnostics.Entity/MigrationsEndPointOptions.cs +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using Microsoft.AspNet.Diagnostics.Entity.Utilities; -using Microsoft.AspNet.Http; - -namespace Microsoft.AspNet.Diagnostics.Entity -{ - public class MigrationsEndPointOptions - { - public static PathString DefaultPath = new PathString("/ApplyDatabaseMigrations"); - private PathString _path = DefaultPath; - - public virtual PathString Path - { - get { return _path; } - set - { - Check.NotNull(value, "value"); - _path = value; - } - } - } -} diff --git a/src/Microsoft.AspNet.Diagnostics.Entity/Properties/Strings.Designer.cs b/src/Microsoft.AspNet.Diagnostics.Entity/Properties/Strings.Designer.cs index ad01a31304..9336c06e33 100644 --- a/src/Microsoft.AspNet.Diagnostics.Entity/Properties/Strings.Designer.cs +++ b/src/Microsoft.AspNet.Diagnostics.Entity/Properties/Strings.Designer.cs @@ -90,70 +90,6 @@ namespace Microsoft.AspNet.Diagnostics.Entity return GetString("DatabaseErrorPage_AddMigrationCommand"); } - /// - /// Apply Migrations - /// - internal static string DatabaseErrorPage_ApplyMigrationsButton - { - get { return GetString("DatabaseErrorPage_ApplyMigrationsButton"); } - } - - /// - /// Apply Migrations - /// - internal static string FormatDatabaseErrorPage_ApplyMigrationsButton() - { - return GetString("DatabaseErrorPage_ApplyMigrationsButton"); - } - - /// - /// Migrations Applied - /// - internal static string DatabaseErrorPage_ApplyMigrationsButtonDone - { - get { return GetString("DatabaseErrorPage_ApplyMigrationsButtonDone"); } - } - - /// - /// Migrations Applied - /// - internal static string FormatDatabaseErrorPage_ApplyMigrationsButtonDone() - { - return GetString("DatabaseErrorPage_ApplyMigrationsButtonDone"); - } - - /// - /// Applying Migrations... - /// - internal static string DatabaseErrorPage_ApplyMigrationsButtonRunning - { - get { return GetString("DatabaseErrorPage_ApplyMigrationsButtonRunning"); } - } - - /// - /// Applying Migrations... - /// - internal static string FormatDatabaseErrorPage_ApplyMigrationsButtonRunning() - { - return GetString("DatabaseErrorPage_ApplyMigrationsButtonRunning"); - } - - /// - /// An error occurred applying migrations, try applying them from the command line - /// - internal static string DatabaseErrorPage_ApplyMigrationsFailed - { - get { return GetString("DatabaseErrorPage_ApplyMigrationsFailed"); } - } - - /// - /// An error occurred applying migrations, try applying them from the command line - /// - internal static string FormatDatabaseErrorPage_ApplyMigrationsFailed() - { - return GetString("DatabaseErrorPage_ApplyMigrationsFailed"); - } - /// /// You can also apply migrations from the command line: /// @@ -170,22 +106,6 @@ namespace Microsoft.AspNet.Diagnostics.Entity return GetString("DatabaseErrorPage_HowToApplyFromCmd"); } - /// - /// Try refreshing the page - /// - internal static string DatabaseErrorPage_MigrationsAppliedRefresh - { - get { return GetString("DatabaseErrorPage_MigrationsAppliedRefresh"); } - } - - /// - /// Try refreshing the page - /// - internal static string FormatDatabaseErrorPage_MigrationsAppliedRefresh() - { - return GetString("DatabaseErrorPage_MigrationsAppliedRefresh"); - } - /// /// From the command line, scaffold a new migration and apply it to the database: /// @@ -314,118 +234,6 @@ namespace Microsoft.AspNet.Diagnostics.Entity return string.Format(CultureInfo.CurrentCulture, GetString("InvalidEnumValue", "argumentName", "enumType"), argumentName, enumType); } - /// - /// Migrations successfully applied for context '{0}'. - /// - internal static string MigrationsEndPointMiddleware_Applied - { - get { return GetString("MigrationsEndPointMiddleware_Applied"); } - } - - /// - /// Migrations successfully applied for context '{0}'. - /// - internal static string FormatMigrationsEndPointMiddleware_Applied(object p0) - { - return string.Format(CultureInfo.CurrentCulture, GetString("MigrationsEndPointMiddleware_Applied"), p0); - } - - /// - /// Request is valid, applying migrations for context '{0}'. - /// - internal static string MigrationsEndPointMiddleware_ApplyingMigrations - { - get { return GetString("MigrationsEndPointMiddleware_ApplyingMigrations"); } - } - - /// - /// Request is valid, applying migrations for context '{0}'. - /// - internal static string FormatMigrationsEndPointMiddleware_ApplyingMigrations(object p0) - { - return string.Format(CultureInfo.CurrentCulture, GetString("MigrationsEndPointMiddleware_ApplyingMigrations"), p0); - } - - /// - /// The context type '{0}' was not found in services. This usually means the context was not registered in services during startup. You probably want to call AddScoped<{0}>() inside the UseServices(...) call in your application startup code. - /// - internal static string MigrationsEndPointMiddleware_ContextNotRegistered - { - get { return GetString("MigrationsEndPointMiddleware_ContextNotRegistered"); } - } - - /// - /// The context type '{0}' was not found in services. This usually means the context was not registered in services during startup. You probably want to call AddScoped<{0}>() inside the UseServices(...) call in your application startup code. - /// - internal static string FormatMigrationsEndPointMiddleware_ContextNotRegistered(object p0) - { - return string.Format(CultureInfo.CurrentCulture, GetString("MigrationsEndPointMiddleware_ContextNotRegistered"), p0); - } - - /// - /// An error occurred while applying the migrations for '{0}'. See InnerException for details. - /// - internal static string MigrationsEndPointMiddleware_Exception - { - get { return GetString("MigrationsEndPointMiddleware_Exception"); } - } - - /// - /// An error occurred while applying the migrations for '{0}'. See InnerException for details. - /// - internal static string FormatMigrationsEndPointMiddleware_Exception(object p0) - { - return string.Format(CultureInfo.CurrentCulture, GetString("MigrationsEndPointMiddleware_Exception"), p0); - } - - /// - /// The context type '{0}' could not be loaded. Ensure this is the correct type name for the context you are trying to apply migrations for. - /// - internal static string MigrationsEndPointMiddleware_InvalidContextType - { - get { return GetString("MigrationsEndPointMiddleware_InvalidContextType"); } - } - - /// - /// The context type '{0}' could not be loaded. Ensure this is the correct type name for the context you are trying to apply migrations for. - /// - internal static string FormatMigrationsEndPointMiddleware_InvalidContextType(object p0) - { - return string.Format(CultureInfo.CurrentCulture, GetString("MigrationsEndPointMiddleware_InvalidContextType"), p0); - } - - /// - /// No context type was specified. Ensure the form data from the request includes a contextTypeName value, specifying the context to apply migrations for. - /// - internal static string MigrationsEndPointMiddleware_NoContextType - { - get { return GetString("MigrationsEndPointMiddleware_NoContextType"); } - } - - /// - /// No context type was specified. Ensure the form data from the request includes a contextTypeName value, specifying the context to apply migrations for. - /// - internal static string FormatMigrationsEndPointMiddleware_NoContextType() - { - return GetString("MigrationsEndPointMiddleware_NoContextType"); - } - - /// - /// Request path matched the path configured for this migrations endpoint ({0}). Attempting to process the migrations request. - /// - internal static string MigrationsEndPointMiddleware_RequestPathMatched - { - get { return GetString("MigrationsEndPointMiddleware_RequestPathMatched"); } - } - - /// - /// Request path matched the path configured for this migrations endpoint ({0}). Attempting to process the migrations request. - /// - internal static string FormatMigrationsEndPointMiddleware_RequestPathMatched(object p0) - { - return string.Format(CultureInfo.CurrentCulture, GetString("MigrationsEndPointMiddleware_RequestPathMatched"), p0); - } - /// /// A database operation failed while processing the request. /// @@ -442,22 +250,6 @@ namespace Microsoft.AspNet.Diagnostics.Entity return GetString("DatabaseErrorPage_Title"); } - /// - /// To use migrations from a command prompt you will need to <a href='http://go.microsoft.com/fwlink/?LinkId=518242'>install .NET Version Manager (dnvm)</a>. Once installed, you can run migration commands from a standard command prompt in the project directory. - /// - internal static string DatabaseErrorPage_EnableMigrationsCommandsInfo - { - get { return GetString("DatabaseErrorPage_EnableMigrationsCommandsInfo"); } - } - - /// - /// To use migrations from a command prompt you will need to <a href='http://go.microsoft.com/fwlink/?LinkId=518242'>install .NET Version Manager (dnvm)</a>. Once installed, you can run migration commands from a standard command prompt in the project directory. - /// - internal static string FormatDatabaseErrorPage_EnableMigrationsCommandsInfo() - { - return GetString("DatabaseErrorPage_EnableMigrationsCommandsInfo"); - } - /// /// {0} occurred, checking if Entity Framework recorded this exception as resulting from a failed database operation. /// diff --git a/src/Microsoft.AspNet.Diagnostics.Entity/Strings.resx b/src/Microsoft.AspNet.Diagnostics.Entity/Strings.resx index 7a0d569ae5..a7e9986403 100644 --- a/src/Microsoft.AspNet.Diagnostics.Entity/Strings.resx +++ b/src/Microsoft.AspNet.Diagnostics.Entity/Strings.resx @@ -132,24 +132,9 @@ > dnx . ef migration add [migration name] - - Apply Migrations - - - Migrations Applied - - - Applying Migrations... - - - An error occurred applying migrations, try applying them from the command line - You can also apply migrations from the command line: - - Try refreshing the page - From the command line, scaffold a new migration and apply it to the database: @@ -174,33 +159,9 @@ The value provided for argument '{argumentName}' must be a valid value of enum type '{enumType}'. - - Migrations successfully applied for context '{0}'. - - - Request is valid, applying migrations for context '{0}'. - - - The context type '{0}' was not found in services. This usually means the context was not registered in services during startup. You probably want to call AddScoped<{0}>() inside the UseServices(...) call in your application startup code. - - - An error occurred while applying the migrations for '{0}'. See InnerException for details. - - - The context type '{0}' could not be loaded. Ensure this is the correct type name for the context you are trying to apply migrations for. - - - No context type was specified. Ensure the form data from the request includes a contextTypeName value, specifying the context to apply migrations for. - - - Request path matched the path configured for this migrations endpoint ({0}). Attempting to process the migrations request. - A database operation failed while processing the request. - - To use migrations from a command prompt you will need to <a href='http://go.microsoft.com/fwlink/?LinkId=518242'>install .NET Version Manager (dnvm)</a>. Once installed, you can run migration commands from a standard command prompt in the project directory. - {0} occurred, checking if Entity Framework recorded this exception as resulting from a failed database operation. diff --git a/src/Microsoft.AspNet.Diagnostics.Entity/Views/DatabaseErrorPage.cs b/src/Microsoft.AspNet.Diagnostics.Entity/Views/DatabaseErrorPage.cs index 2c14bd0011..a5c276685e 100644 --- a/src/Microsoft.AspNet.Diagnostics.Entity/Views/DatabaseErrorPage.cs +++ b/src/Microsoft.AspNet.Diagnostics.Entity/Views/DatabaseErrorPage.cs @@ -181,14 +181,8 @@ using Microsoft.AspNet.Diagnostics.Entity.Views #line default #line hidden - WriteLiteral(" \r\n

"); -#line 61 "DatabaseErrorPage.cshtml" - WriteLiteral(Strings.DatabaseErrorPage_EnableMigrationsCommandsInfo); - -#line default -#line hidden - WriteLiteral("

\r\n
\r\n"); -#line 63 "DatabaseErrorPage.cshtml" + WriteLiteral(" \r\n
\r\n"); +#line 62 "DatabaseErrorPage.cshtml" } else if (Model.PendingMigrations.Any()) { @@ -197,25 +191,25 @@ using Microsoft.AspNet.Diagnostics.Entity.Views #line hidden WriteLiteral("
\r\n

"); -#line 67 "DatabaseErrorPage.cshtml" +#line 66 "DatabaseErrorPage.cshtml" Write(Strings.FormatDatabaseErrorPage_PendingMigrationsTitle(Model.ContextType.Name)); #line default #line hidden WriteLiteral("

\r\n

"); -#line 68 "DatabaseErrorPage.cshtml" +#line 67 "DatabaseErrorPage.cshtml" Write(Strings.FormatDatabaseErrorPage_PendingMigrationsInfo(Model.ContextType.Name)); #line default #line hidden WriteLiteral("

\r\n\r\n"); -#line 70 "DatabaseErrorPage.cshtml" +#line 69 "DatabaseErrorPage.cshtml" #line default #line hidden -#line 70 "DatabaseErrorPage.cshtml" +#line 69 "DatabaseErrorPage.cshtml" if (Model.Options.ListMigrations) { @@ -223,13 +217,13 @@ using Microsoft.AspNet.Diagnostics.Entity.Views #line hidden WriteLiteral("
    \r\n"); -#line 73 "DatabaseErrorPage.cshtml" +#line 72 "DatabaseErrorPage.cshtml" #line default #line hidden -#line 73 "DatabaseErrorPage.cshtml" +#line 72 "DatabaseErrorPage.cshtml" foreach (var migration in Model.PendingMigrations) { @@ -237,147 +231,39 @@ using Microsoft.AspNet.Diagnostics.Entity.Views #line hidden WriteLiteral("
  • "); -#line 75 "DatabaseErrorPage.cshtml" +#line 74 "DatabaseErrorPage.cshtml" Write(migration); #line default #line hidden WriteLiteral("
  • \r\n"); -#line 76 "DatabaseErrorPage.cshtml" +#line 75 "DatabaseErrorPage.cshtml" } #line default #line hidden WriteLiteral("
\r\n"); -#line 78 "DatabaseErrorPage.cshtml" - } - -#line default -#line hidden - - WriteLiteral("\r\n"); -#line 80 "DatabaseErrorPage.cshtml" - - -#line default -#line hidden - -#line 80 "DatabaseErrorPage.cshtml" - if (Model.Options.EnableMigrationCommands) - { - -#line default -#line hidden - - WriteLiteral("

\r\n - - -

- \r\n"); -#line 122 "DatabaseErrorPage.cshtml" +#line 77 "DatabaseErrorPage.cshtml" } #line default #line hidden WriteLiteral("\r\n

"); -#line 124 "DatabaseErrorPage.cshtml" +#line 79 "DatabaseErrorPage.cshtml" Write(Strings.DatabaseErrorPage_HowToApplyFromCmd); #line default #line hidden WriteLiteral("

\r\n "); -#line 125 "DatabaseErrorPage.cshtml" +#line 80 "DatabaseErrorPage.cshtml" Write(Strings.DatabaseErrorPage_ApplyMigrationsCommand); #line default #line hidden - WriteLiteral("\r\n

"); -#line 126 "DatabaseErrorPage.cshtml" - WriteLiteral(Strings.DatabaseErrorPage_EnableMigrationsCommandsInfo); - -#line default -#line hidden - WriteLiteral("

\r\n
\r\n
\r\n"); -#line 129 "DatabaseErrorPage.cshtml" + WriteLiteral("\r\n
\r\n \r\n"); +#line 83 "DatabaseErrorPage.cshtml" } else if (Model.PendingModelChanges) { @@ -386,37 +272,31 @@ using Microsoft.AspNet.Diagnostics.Entity.Views #line hidden WriteLiteral("
\r\n

"); -#line 133 "DatabaseErrorPage.cshtml" +#line 87 "DatabaseErrorPage.cshtml" Write(Strings.FormatDatabaseErrorPage_PendingChangesTitle(Model.ContextType.Name)); #line default #line hidden WriteLiteral("

\r\n

"); -#line 134 "DatabaseErrorPage.cshtml" +#line 88 "DatabaseErrorPage.cshtml" Write(Strings.DatabaseErrorPage_PendingChangesInfo); #line default #line hidden WriteLiteral("

\r\n "); -#line 135 "DatabaseErrorPage.cshtml" +#line 89 "DatabaseErrorPage.cshtml" Write(Strings.DatabaseErrorPage_AddMigrationCommand); #line default #line hidden WriteLiteral("\r\n
\r\n "); -#line 137 "DatabaseErrorPage.cshtml" +#line 91 "DatabaseErrorPage.cshtml" Write(Strings.DatabaseErrorPage_ApplyMigrationsCommand); #line default #line hidden - WriteLiteral("\r\n

"); -#line 138 "DatabaseErrorPage.cshtml" - WriteLiteral(Strings.DatabaseErrorPage_EnableMigrationsCommandsInfo); - -#line default -#line hidden - WriteLiteral("

\r\n
\r\n
\r\n"); -#line 141 "DatabaseErrorPage.cshtml" + WriteLiteral("\r\n
\r\n \r\n"); +#line 94 "DatabaseErrorPage.cshtml" } #line default diff --git a/src/Microsoft.AspNet.Diagnostics.Entity/Views/DatabaseErrorPage.cshtml b/src/Microsoft.AspNet.Diagnostics.Entity/Views/DatabaseErrorPage.cshtml index 9f32ce799b..3177479976 100644 --- a/src/Microsoft.AspNet.Diagnostics.Entity/Views/DatabaseErrorPage.cshtml +++ b/src/Microsoft.AspNet.Diagnostics.Entity/Views/DatabaseErrorPage.cshtml @@ -58,7 +58,6 @@ @Strings.DatabaseErrorPage_AddMigrationCommand
@Strings.DatabaseErrorPage_ApplyMigrationsCommand -

@Strings.DatabaseErrorPage_EnableMigrationsCommandsInfo


} else if (Model.PendingMigrations.Any()) @@ -77,53 +76,8 @@ } - @if (Model.Options.EnableMigrationCommands) - { -

- - - -

- - } -

@Strings.DatabaseErrorPage_HowToApplyFromCmd

@Strings.DatabaseErrorPage_ApplyMigrationsCommand -

@Strings.DatabaseErrorPage_EnableMigrationsCommandsInfo


} @@ -135,7 +89,6 @@ @Strings.DatabaseErrorPage_AddMigrationCommand
@Strings.DatabaseErrorPage_ApplyMigrationsCommand -

@Strings.DatabaseErrorPage_EnableMigrationsCommandsInfo


} diff --git a/src/PageGenerator/project.json b/src/PageGenerator/project.json index fc16b4d9fd..c875781092 100644 --- a/src/PageGenerator/project.json +++ b/src/PageGenerator/project.json @@ -2,6 +2,7 @@ "version": "1.0.0-*", "dependencies": { "Microsoft.AspNet.Diagnostics.Elm": "1.0.0-*", + "Microsoft.AspNet.Diagnostics.Entity": "7.0.0-*", "Microsoft.AspNet.Razor": "4.0.0-*" }, diff --git a/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/DatabaseErrorPageMiddlewareTest.cs b/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/DatabaseErrorPageMiddlewareTest.cs index 4959c4f430..07531a2e9c 100644 --- a/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/DatabaseErrorPageMiddlewareTest.cs +++ b/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/DatabaseErrorPageMiddlewareTest.cs @@ -171,91 +171,6 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests } } - [Fact] - public async Task Error_page_then_apply_migrations() - { - TestServer server = SetupTestServer(); - var client = server.CreateClient(); - - var expectedMigrationsEndpoint = "/ApplyDatabaseMigrations"; - var expectedContextType = typeof(BloggingContextWithMigrations).AssemblyQualifiedName; - - // Step One: Initial request with database failure - HttpResponseMessage response = await client.GetAsync("http://localhost/"); - Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode); - var content = await response.Content.ReadAsStringAsync(); - - // Ensure the url we're going to test is what the page is using in it's JavaScript - Assert.Contains("req.open(\"POST\", \"" + expectedMigrationsEndpoint + "\", true);", content); - Assert.Contains("var params = \"context=\" + encodeURIComponent(\"" + expectedContextType + "\");", content); - - // Step Two: Request to migrations endpoint - var formData = new FormUrlEncodedContent(new List> - { - new KeyValuePair("context", expectedContextType) - }); - - response = await client.PostAsync("http://localhost" + expectedMigrationsEndpoint, formData); - content = await response.Content.ReadAsStringAsync(); - Console.WriteLine(content); - Assert.Equal(HttpStatusCode.NoContent, response.StatusCode); - - // Step Three: Successful request after migrations applied - response = await client.GetAsync("http://localhost/"); - content = await response.Content.ReadAsStringAsync(); - Assert.Equal("Saved a Blog", content); - } - - class ApplyMigrationsMiddleware - { - public ApplyMigrationsMiddleware(RequestDelegate next) - { } - - public virtual async Task Invoke(HttpContext context) - { - using (var db = context.ApplicationServices.GetService()) - { - db.Blogs.Add(new Blog()); - db.SaveChanges(); - await context.Response.WriteAsync("Saved a Blog"); - } - } - } - - [Fact] - public async Task Customize_migrations_end_point() - { - var migrationsEndpoint = "/MyCustomEndPoints/ApplyMyMigrationsHere"; - - using (var database = SqlServerTestStore.CreateScratch()) - { - var server = TestServer.Create(app => - { - var options = DatabaseErrorPageOptions.ShowAll; - options.MigrationsEndPointPath = new PathString(migrationsEndpoint); - app.UseDatabaseErrorPage(options); - - app.UseMiddleware(); - }, - services => - { - services.AddEntityFramework().AddSqlServer(); - services.AddScoped(); - - var optionsBuilder = new DbContextOptionsBuilder(); - optionsBuilder.UseSqlServer(database.ConnectionString); - services.AddInstance(optionsBuilder.Options); - }); - - HttpResponseMessage response = await server.CreateClient().GetAsync("http://localhost/"); - - Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode); - - var content = await response.Content.ReadAsStringAsync(); - Assert.Contains("req.open(\"POST\", \"" + migrationsEndpoint + "\", true);", content); - } - } - [Fact] public async Task Pass_thru_when_context_not_in_services() { diff --git a/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/MigrationsEndPointMiddlewareTest.cs b/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/MigrationsEndPointMiddlewareTest.cs deleted file mode 100644 index ebf08ad148..0000000000 --- a/test/Microsoft.AspNet.Diagnostics.Entity.FunctionalTests/MigrationsEndPointMiddlewareTest.cs +++ /dev/null @@ -1,205 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// 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.Linq; -using System.Net; -using System.Net.Http; -using System.Threading.Tasks; -using Microsoft.AspNet.Builder; -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.Relational.Migrations.History; -using Microsoft.Framework.DependencyInjection; -using Xunit; -using Microsoft.AspNet.Diagnostics.Entity.Tests.Helpers; -using Microsoft.AspNet.Hosting.Startup; - -namespace Microsoft.AspNet.Diagnostics.Entity.Tests -{ - public class MigrationsEndPointMiddlewareTest - { - [Fact] - public async Task Non_migration_requests_pass_thru() - { - TestServer server = TestServer.Create(app => app - .UseMigrationsEndPoint() - .UseMiddleware()); - - HttpResponseMessage response = await server.CreateClient().GetAsync("http://localhost/"); - - Assert.Equal("Request Handled", await response.Content.ReadAsStringAsync()); - Assert.Equal(HttpStatusCode.OK, response.StatusCode); - } - - class SuccessMiddleware - { - public SuccessMiddleware(RequestDelegate next) - { } - - public virtual async Task Invoke(HttpContext context) - { - await context.Response.WriteAsync("Request Handled"); - context.Response.StatusCode = (int)HttpStatusCode.OK; - } - } - - [Fact] - public async Task Migration_request_default_path() - { - await Migration_request(useCustomPath: false); - } - - [Fact] - public async Task Migration_request_custom_path() - { - await Migration_request(useCustomPath: true); - } - - private async Task Migration_request(bool useCustomPath) - { - using (var database = SqlServerTestStore.CreateScratch()) - { - var optionsBuilder = new DbContextOptionsBuilder(); - optionsBuilder.UseSqlServer(database.ConnectionString); - - var path = useCustomPath ? new PathString("/EndPoints/ApplyMyMigrations") : MigrationsEndPointOptions.DefaultPath; - - TestServer server = TestServer.Create(app => - { - if (useCustomPath) - { - app.UseMigrationsEndPoint(new MigrationsEndPointOptions { Path = path }); - } - else - { - app.UseMigrationsEndPoint(); - } - }, - services => - { - services.AddEntityFramework().AddSqlServer(); - services.AddScoped(); - services.AddInstance(optionsBuilder.Options); - }); - - using (var db = BloggingContextWithMigrations.CreateWithoutExternalServiceProvider(optionsBuilder.Options)) - { - Assert.False(db.Database.AsRelational().Exists()); - - var formData = new FormUrlEncodedContent(new List> - { - new KeyValuePair("context", typeof(BloggingContextWithMigrations).AssemblyQualifiedName) - }); - - HttpResponseMessage response = await server.CreateClient() - .PostAsync("http://localhost" + path, formData); - - Assert.Equal(HttpStatusCode.NoContent, response.StatusCode); - - Assert.True(db.Database.AsRelational().Exists()); - - var historyRepository = ((IAccessor)db).Service.GetRequiredService(); - var appliedMigrations = historyRepository.GetAppliedMigrations(); - Assert.Equal(2, appliedMigrations.Count); - Assert.Equal("111111111111111_MigrationOne", appliedMigrations.ElementAt(0).MigrationId); - Assert.Equal("222222222222222_MigrationTwo", appliedMigrations.ElementAt(1).MigrationId); - } - } - } - - [Fact] - public async Task Context_type_not_specified() - { - var server = TestServer.Create(app => - { - app.UseMigrationsEndPoint(); - }); - - var formData = new FormUrlEncodedContent(new List>()); - - var response = await server.CreateClient().PostAsync("http://localhost" + MigrationsEndPointOptions.DefaultPath, formData); - var content = await response.Content.ReadAsStringAsync(); - - Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); - Assert.StartsWith(StringsHelpers.GetResourceString("FormatMigrationsEndPointMiddleware_NoContextType"), content); - Assert.True(content.Length > 512); - } - - [Fact] - public async Task Invalid_context_type_specified() - { - var server = TestServer.Create(app => - { - app.UseMigrationsEndPoint(); - }); - - var typeName = "You won't find this type ;)"; - var formData = new FormUrlEncodedContent(new List> - { - new KeyValuePair("context", typeName) - }); - - var response = await server.CreateClient().PostAsync("http://localhost" + MigrationsEndPointOptions.DefaultPath, formData); - var content = await response.Content.ReadAsStringAsync(); - - Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); - Assert.StartsWith(StringsHelpers.GetResourceString("FormatMigrationsEndPointMiddleware_InvalidContextType", typeName), content); - Assert.True(content.Length > 512); - } - - [Fact] - public async Task Context_not_registered_in_services() - { - var server = TestServer.Create( - app => app.UseMigrationsEndPoint(), - services => services.AddEntityFramework().AddSqlServer()); - - var formData = new FormUrlEncodedContent(new List> - { - new KeyValuePair("context", typeof(BloggingContext).AssemblyQualifiedName) - }); - - var response = await server.CreateClient().PostAsync("http://localhost" + MigrationsEndPointOptions.DefaultPath, formData); - var content = await response.Content.ReadAsStringAsync(); - - Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); - Assert.StartsWith(StringsHelpers.GetResourceString("FormatMigrationsEndPointMiddleware_ContextNotRegistered", typeof(BloggingContext)), content); - Assert.True(content.Length > 512); - } - - [Fact] - public async Task Exception_while_applying_migrations() - { - using (var database = SqlServerTestStore.CreateScratch()) - { - var optionsBuilder = new DbContextOptionsBuilder(); - optionsBuilder.UseSqlServer(database.ConnectionString); - - TestServer server = TestServer.Create( - app => app.UseMigrationsEndPoint(), - services => - { - services.AddEntityFramework().AddSqlServer(); - services.AddScoped(); - services.AddInstance(optionsBuilder.Options); - }); - - var formData = new FormUrlEncodedContent(new List> - { - new KeyValuePair("context", typeof(BloggingContextWithSnapshotThatThrows).AssemblyQualifiedName) - }); - - var ex = await Assert.ThrowsAsync(async () => - await server.CreateClient().PostAsync("http://localhost" + MigrationsEndPointOptions.DefaultPath, formData)); - - Assert.Equal(StringsHelpers.GetResourceString("FormatMigrationsEndPointMiddleware_Exception", typeof(BloggingContextWithSnapshotThatThrows)), ex.Message); - Assert.Equal("Welcome to the invalid migration!", ex.InnerException.Message); - } - } - } -} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Diagnostics.Entity.Tests/DatabaseErrorPageOptionsTest.cs b/test/Microsoft.AspNet.Diagnostics.Entity.Tests/DatabaseErrorPageOptionsTest.cs index d3c7d98109..bbfd13edde 100644 --- a/test/Microsoft.AspNet.Diagnostics.Entity.Tests/DatabaseErrorPageOptionsTest.cs +++ b/test/Microsoft.AspNet.Diagnostics.Entity.Tests/DatabaseErrorPageOptionsTest.cs @@ -14,7 +14,6 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests Assert.False(options.ShowExceptionDetails); Assert.False(options.ListMigrations); - Assert.False(options.EnableMigrationCommands); } [Fact] @@ -25,7 +24,6 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests Assert.True(options.ShowExceptionDetails); Assert.True(options.ListMigrations); - Assert.True(options.EnableMigrationCommands); } [Fact] @@ -36,7 +34,6 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests Assert.False(options.ShowExceptionDetails); Assert.True(options.ListMigrations); - Assert.True(options.EnableMigrationCommands); } [Fact] @@ -47,18 +44,6 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests Assert.True(options.ShowExceptionDetails); Assert.False(options.ListMigrations); - Assert.True(options.EnableMigrationCommands); - } - - [Fact] - public void EnableMigrationCommands_overides_default_visibility() - { - var options = new DatabaseErrorPageOptions { EnableMigrationCommands = false }; - options.SetDefaultVisibility(true); - - Assert.True(options.ShowExceptionDetails); - Assert.True(options.ListMigrations); - Assert.False(options.EnableMigrationCommands); } } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Diagnostics.Entity.Tests/DatabaseErrorPageTest.cs b/test/Microsoft.AspNet.Diagnostics.Entity.Tests/DatabaseErrorPageTest.cs index ad4364f224..ac52fe8ff7 100644 --- a/test/Microsoft.AspNet.Diagnostics.Entity.Tests/DatabaseErrorPageTest.cs +++ b/test/Microsoft.AspNet.Diagnostics.Entity.Tests/DatabaseErrorPageTest.cs @@ -198,45 +198,6 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests Assert.DoesNotContain("111_MigrationOne", content); } - [Fact] - public async Task EnableMigrationCommands_is_respected() - { - var options = new DatabaseErrorPageOptions { EnableMigrationCommands = false }; - options.SetDefaultVisibility(true); - - var model = new DatabaseErrorPageModel( - contextType: typeof(BloggingContext), - exception: new Exception(), - databaseExists: true, - pendingModelChanges: false, - pendingMigrations: new string[] { "111_MigrationOne" }, - options: options); - - var content = await ExecutePage(options, model); - - Assert.DoesNotContain(options.MigrationsEndPointPath.Value, content); - } - - [Fact] - public async Task MigrationsEndPointPath_is_respected() - { - var options = new DatabaseErrorPageOptions { MigrationsEndPointPath = new PathString("/HitThisEndPoint") }; - options.SetDefaultVisibility(true); - - var model = new DatabaseErrorPageModel( - contextType: typeof(BloggingContext), - exception: new Exception(), - databaseExists: true, - pendingModelChanges: false, - pendingMigrations: new string[] { "111_MigrationOne" }, - options: options); - - var content = await ExecutePage(options, model); - - Assert.Contains(options.MigrationsEndPointPath.Value, content); - } - - private static async Task ExecutePage(DatabaseErrorPageOptions options, DatabaseErrorPageModel model) { var page = new DatabaseErrorPage();