-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathSqlServerMigration.cs
52 lines (44 loc) · 2.08 KB
/
SqlServerMigration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/Beef
using DbEx;
using OnRamp;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Beef.Database.SqlServer
{
/// <summary>
/// Provides the <see href="https://docs.microsoft.com/en-us/sql/connect/ado-net/microsoft-ado-net-sql-server">SQL Server</see> migration orchestration extending <see cref="DbEx.SqlServer.Migration.SqlServerMigration"/>
/// to further enable <see cref="MigrationCommand.CodeGen"/>.
/// </summary>
public class SqlServerMigration : DbEx.SqlServer.Migration.SqlServerMigration
{
/// <summary>
/// Initializes an instance of the <see cref="SqlServerMigration"/> class.
/// </summary>
/// <param name="args">The <see cref="MigrationArgs"/>.</param>
public SqlServerMigration(MigrationArgs args) : base(args) => IsCodeGenEnabled = true;
/// <summary>
/// Gets the <see cref="MigrationArgs"/>.
/// </summary>
public new MigrationArgs Args => (MigrationArgs)base.Args;
/// <inheritdoc/>
protected override Task<(bool Success, string? Statistics)> DatabaseCodeGenAsync(CancellationToken cancellationToken = default)
{
var yaml = Args.GetParameter<string>("Param0");
if (yaml is null)
return this.ExecuteCodeGenAsync(cancellationToken);
var schema = Args.GetParameter<string>("Param1");
var tables = new List<string>();
for (int i = 2; true; i++)
{
var table = Args.GetParameter<string>($"Param{i}");
if (table is null)
break;
tables.Add(table);
}
if (schema is null || tables.Count == 0)
throw new CodeGenException($"A '{nameof(MigrationCommand.CodeGen)}' command for 'YAML' also requires schema and at least one table argument to be specified.");
return this.ExecuteYamlCodeGenAsync(schema, [.. tables], cancellationToken);
}
}
}