Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/6.0-preview7] Add Migration Bundles #25256

Merged
merged 1 commit into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/EFCore.Design/Design/Internal/ContextInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ namespace Microsoft.EntityFrameworkCore.Design.Internal
/// </summary>
public class ContextInfo
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual string Type { get; set; } = null!;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand Down
5 changes: 4 additions & 1 deletion src/EFCore.Design/Design/Internal/DbContextOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,10 @@ where i.IsGenericType
public virtual ContextInfo GetContextInfo(string? contextType)
{
using var context = CreateContext(contextType);
var info = new ContextInfo();
var info = new ContextInfo
{
Type = context.GetType().FullName!
};

var provider = context.GetService<IDatabaseProvider>();
info.ProviderName = provider.Name;
Expand Down
104 changes: 104 additions & 0 deletions src/EFCore.Design/Design/MigrationsBundle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// 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 System.Reflection;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.EntityFrameworkCore.Design.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Tools;

namespace Microsoft.EntityFrameworkCore.Design
{
/// <summary>
/// Defines the entry point for Migations bundles.
/// </summary>
public static class MigrationsBundle
{
/// <summary>
/// The entry point for Migrations bundles.
/// </summary>
/// <param name="context"> The DbContext to use. </param>
/// <param name="assembly"> The Migrations assembly. </param>
/// <param name="startupAssembly"> The startup assembly. </param>
/// <param name="args"> The command-line arguemnts. </param>
/// <returns> Zero if the command succeedes; otherwise, one. </returns>
public static int Execute(string? context, Assembly assembly, Assembly startupAssembly, string[] args)
{
var app = new CommandLineApplication
{
Name = "bundle",
FullName = DesignStrings.BundleFullName,
HandleResponseFiles = true
};

var migration = app.Argument("<MIGRATION>", DesignStrings.MigrationDescription);
var connection = app.Option("--connection <CONNECTION>", DesignStrings.ConnectionDescription);

app.VersionOption("--version", ProductInfo.GetVersion);
app.HelpOption("-h|--help");
var verbose = app.Option("-v|--verbose", DesignStrings.VerboseDescription);
var noColor = app.Option("--no-color", DesignStrings.NoColorDescription);
var prefixOutput = app.Option("--prefix-output", DesignStrings.PrefixDescription);

app.OnExecute(
args =>
{
Reporter.IsVerbose = verbose.HasValue();
Reporter.NoColor = noColor.HasValue();
Reporter.PrefixOutput = prefixOutput.HasValue();

ExecuteInternal(context, assembly, startupAssembly, args, migration.Value, connection.Value());

return 0;
});

try
{
return app.Execute(args);
}
catch (Exception ex)
{
if (ex is CommandParsingException
|| ex is OperationException)
{
Reporter.WriteVerbose(ex.ToString());
}
else
{
Reporter.WriteInformation(ex.ToString());
}

Reporter.WriteError(ex.Message);

return 1;
}
}

private static void ExecuteInternal(
string? context,
Assembly assembly,
Assembly startupAssembly,
string[] args,
string? migration,
string? connection)
{
new MigrationsOperations(
new OperationReporter(
new OperationReportHandler(
Reporter.WriteError,
Reporter.WriteWarning,
Reporter.WriteInformation,
Reporter.WriteVerbose)),
assembly,
startupAssembly,
projectDir: string.Empty,
rootNamespace: null,
language: null,
nullable: false,
args)
.UpdateDatabase(migration, connection, context);
}
}
}
1 change: 1 addition & 0 deletions src/EFCore.Design/Design/OperationExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ private IDictionary GetContextInfoImpl(string? contextType)
var info = ContextOperations.GetContextInfo(contextType);
return new Hashtable
{
["Type"] = info.Type,
["ProviderName"] = info.ProviderName,
["DatabaseName"] = info.DatabaseName,
["DataSource"] = info.DataSource,
Expand Down
5 changes: 5 additions & 0 deletions src/EFCore.Design/EFCore.Design.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="..\ef\CommandLineUtils\*.cs" Link="CommandLineUtils\%(Filename).cs" />
<Compile Include="..\ef\Reporter.cs" />
<Compile Include="..\ef\AnsiConstants.cs" />
<Compile Include="..\ef\AnsiConsole.cs" />
<Compile Include="..\ef\AnsiTextWriter.cs" />
<Compile Include="..\Shared\*.cs" />
</ItemGroup>

Expand Down
36 changes: 36 additions & 0 deletions src/EFCore.Design/Properties/DesignStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions src/EFCore.Design/Properties/DesignStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@
<data name="BadSequenceType" xml:space="preserve">
<value>Cannot scaffold sequence '{sequenceName}' because it uses type '{typeName}' which is unsupported.</value>
</data>
<data name="BundleFullName" xml:space="preserve">
<value>Entity Framework Core Migrations Bundle</value>
bricelam marked this conversation as resolved.
Show resolved Hide resolved
</data>
<data name="CannotFindDesignTimeProviderAssemblyAttribute" xml:space="preserve">
<value>Unable to find expected assembly attribute [DesignTimeProviderServices] in provider assembly '{runtimeProviderAssemblyName}'. This attribute is required to identify the class which acts as the design-time service provider factory for the provider.</value>
</data>
Expand Down Expand Up @@ -162,6 +165,9 @@
<data name="ConflictingContextAndMigrationName" xml:space="preserve">
<value>The name you have chosen for the migration, '{name}', is the same as the context class name. Please choose a different name for your migration. Might we suggest 'InitialCreate' for your first migration?</value>
</data>
<data name="ConnectionDescription" xml:space="preserve">
<value>The connection string to the database. Defaults to the one specified in AddDbContext or OnConfiguring.</value>
</data>
<data name="ContextClassNotValidCSharpIdentifier" xml:space="preserve">
<value>The context class name '{contextClassName}' is not a valid C# identifier.</value>
</data>
Expand Down Expand Up @@ -252,6 +258,9 @@
<data name="ManuallyDeleted" xml:space="preserve">
<value>The model snapshot and the backing model of the last migration are different. Continuing under the assumption that the last migration was deleted manually.</value>
</data>
<data name="MigrationDescription" xml:space="preserve">
<value>The target migration. If '0', all migrations will be reverted. Defaults to the last migration.</value>
</data>
<data name="MigrationsAssemblyMismatch" xml:space="preserve">
<value>Your target project '{assembly}' doesn't match your migrations assembly '{migrationsAssembly}'. Either change your target project or change your migrations assembly.
Change your migrations assembly by using DbContextOptionsBuilder. E.g. options.UseSqlServer(connection, b =&gt; b.MigrationsAssembly("{assembly}")). By default, the migrations assembly is the assembly containing the DbContext.
Expand All @@ -269,6 +278,9 @@ Change your target project to the migrations project by using the Package Manage
<data name="MultipleContextsWithQualifiedName" xml:space="preserve">
<value>More than one DbContext named '{name}' was found. Specify which one to use by providing its fully qualified name using its exact case.</value>
</data>
<data name="NoColorDescription" xml:space="preserve">
<value>Don't colorize output.</value>
</data>
<data name="NoContext" xml:space="preserve">
<value>No DbContext was found in assembly '{assembly}'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.</value>
</data>
Expand Down Expand Up @@ -317,6 +329,9 @@ Change your target project to the migrations project by using the Package Manage
<data name="NotExistDatabase" xml:space="preserve">
<value>Database '{name}' did not exist, no action was taken.</value>
</data>
<data name="PrefixDescription" xml:space="preserve">
<value>Prefix output with level.</value>
</data>
<data name="PrimaryKeyErrorPropertyNotFound" xml:space="preserve">
<value>Could not scaffold the primary key for '{tableName}'. The following columns in the primary key could not be scaffolded: {columnNames}.</value>
</data>
Expand Down Expand Up @@ -390,6 +405,9 @@ Change your target project to the migrations project by using the Package Manage
<data name="UsingReferencedServices" xml:space="preserve">
<value>Using design-time services from assembly '{referencedAssembly}'.</value>
</data>
<data name="VerboseDescription" xml:space="preserve">
<value>Show verbose output.</value>
</data>
<data name="VersionMismatch" xml:space="preserve">
<value>The Entity Framework tools version '{toolsVersion}' is older than that of the runtime '{runtimeVersion}'. Update the tools for the latest features and bug fixes. See https://aka.ms/AAc1fbw for more information.</value>
</data>
Expand Down
1 change: 1 addition & 0 deletions src/EFCore.Tools/EFCore.Tools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

Enables these commonly used commands:
Add-Migration
Bundle-Migration
Drop-Database
Get-DbContext
Get-Migration
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Tools/EFCore.Tools.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
$CommonMetadataElements$
<minClientVersion>3.6</minClientVersion>
<dependencies>
<group targetFramework=".NET5.0">
<group targetFramework=".NET6.0">
<dependency id="Microsoft.EntityFrameworkCore.Design" version="$version$" />
</group>
</dependencies>
Expand Down
1 change: 1 addition & 0 deletions src/EFCore.Tools/EntityFrameworkCore.PS2.psd1.in
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ NestedModules = @()
# Functions to export from this module
FunctionsToExport = (
'Add-Migration',
'Bundle-Migration',
'Drop-Database',
'Enable-Migrations',
'Get-DbContext',
Expand Down
1 change: 1 addition & 0 deletions src/EFCore.Tools/EntityFrameworkCore.psd1.in
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ DotNetFrameworkVersion = '4.0'
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = (
'Add-Migration',
'Bundle-Migration',
'Drop-Database',
'Enable-Migrations',
'Get-DbContext',
Expand Down
49 changes: 49 additions & 0 deletions src/EFCore.Tools/tools/EntityFrameworkCore.PS2.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,55 @@ function Add-Migration(
throw $UpdatePowerShell
}

<#
.SYNOPSIS
Creates an executable to update the database.

.DESCRIPTION
Creates an executable to update the database.

.PARAMETER SelfContained
Also bundle the .NET runtime so it doesn't need to be installed on the machine.

.PARAMETER Runtime
The target runtime to bundle for.

.PARAMETER Configuration
The configuration to use.

.PARAMETER Context
The DbContext to use.

.PARAMETER Project
The project to use.

.PARAMETER StartupProject
The startup project to use. Defaults to the solution's startup project.

.PARAMETER Namespace
The namespace to use. Matches the directory by default.

.PARAMETER Args
Arguments passed to the application.

.LINK
Script-Migration
Update-Database
about_EntityFrameworkCore
#>
function Bundle-Migration(
[switch] $SelfContained,
$Runtime,
$Configuration,
$Context,
$Project,
$StartupProject,
$Namespace,
$Args)
{
throw $UpdatePowerShell
}

<#
.SYNOPSIS
Drops the database.
Expand Down
Loading