Skip to content

Commit

Permalink
v2.4.0 (#50)
Browse files Browse the repository at this point in the history
* MigrationAssemblyArgs

* Update packages.
  • Loading branch information
chullybun authored Jan 16, 2024
1 parent e450b7c commit df5ddd6
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 70 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

Represents the **NuGet** versions.

## v2.4.0
- *Enhancement:* Added `MigrationAssemblyArgs` to allow for the specification of zero or more `Data` folder names.
- *Fixed:* Updated `CoreEx` to version `3.9.0`.


## v2.3.15
- *Fixed:* Updated `CoreEx` to version `3.8.0`.
- *Fixed:* Updated `OnRamp` to version `2.0.0` which necessitated internal change from `Newtonsoft.Json` (now deprecated) to `System.Text.Json`; additionally, the `DataParser` was refactored accordingly.
Expand Down
2 changes: 1 addition & 1 deletion Common.targets
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>2.3.15</Version>
<Version>2.4.0</Version>
<LangVersion>preview</LangVersion>
<Authors>Avanade</Authors>
<Company>Avanade</Company>
Expand Down
2 changes: 1 addition & 1 deletion src/DbEx.MySql/DbEx.MySql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CoreEx.Database.MySql" Version="3.8.0" />
<PackageReference Include="CoreEx.Database.MySql" Version="3.9.0" />
<PackageReference Include="dbup-mysql" Version="5.0.37" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions src/DbEx.MySql/Migration/MySqlMigration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class MySqlMigration : DatabaseMigrationBase
private readonly string _databaseName;
private readonly IDatabase _database;
private readonly IDatabase _masterDatabase;
private readonly List<string> _resetBypass = new();
private readonly List<string> _resetBypass = [];

/// <summary>
/// Initializes an instance of the <see cref="MySqlMigration"/> class.
Expand All @@ -48,7 +48,7 @@ public MySqlMigration(MigrationArgsBase args) : base(args)

// Defaults the schema object types unless already specified.
if (SchemaObjectTypes.Length == 0)
SchemaObjectTypes = new string[] { "FUNCTION", "VIEW", "PROCEDURE" };
SchemaObjectTypes = ["FUNCTION", "VIEW", "PROCEDURE"];

// Add/set standard parameters.
Args.Parameter(MigrationArgsBase.DatabaseNameParamName, _databaseName, true);
Expand Down
2 changes: 1 addition & 1 deletion src/DbEx.SqlServer/DbEx.SqlServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CoreEx.Database.SqlServer" Version="3.8.0" />
<PackageReference Include="CoreEx.Database.SqlServer" Version="3.9.0" />
<PackageReference Include="dbup-sqlserver" Version="5.0.37" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions src/DbEx.SqlServer/Migration/SqlServerMigration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class SqlServerMigration : DatabaseMigrationBase
private readonly string _databaseName;
private readonly IDatabase _database;
private readonly IDatabase _masterDatabase;
private readonly List<string> _resetBypass = new();
private readonly List<string> _resetBypass = [];

/// <summary>
/// Initializes an instance of the <see cref="SqlServerMigration"/> class.
Expand All @@ -50,7 +50,7 @@ public SqlServerMigration(MigrationArgsBase args) : base(args)

// Defaults the schema object types unless already specified.
if (SchemaObjectTypes.Length == 0)
SchemaObjectTypes = new string[] { "TYPE", "FUNCTION", "VIEW", "PROCEDURE", "PROC" };
SchemaObjectTypes = ["TYPE", "FUNCTION", "VIEW", "PROCEDURE", "PROC"];

// Always add the dbo schema _first_ unless already specified.
if (!Args.SchemaOrder.Contains("dbo"))
Expand Down
15 changes: 5 additions & 10 deletions src/DbEx/Console/AssemblyValidator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/DbEx

using CoreEx;
using DbEx.Migration;
using McMaster.Extensions.CommandLineUtils;
using McMaster.Extensions.CommandLineUtils.Validation;
Expand All @@ -15,15 +16,10 @@ namespace DbEx.Console
/// <summary>
/// Validates the assembly name(s).
/// </summary>
public class AssemblyValidator : IOptionValidator
/// <param name="args">The <see cref="MigrationArgs"/> to update.</param>
public class AssemblyValidator(MigrationArgsBase args) : IOptionValidator
{
private readonly MigrationArgsBase _args;

/// <summary>
/// Initilizes a new instance of the <see cref="AssemblyValidator"/> class.
/// </summary>
/// <param name="args">The <see cref="MigrationArgs"/> to update.</param>
public AssemblyValidator(MigrationArgsBase args) => _args = args ?? throw new ArgumentNullException(nameof(args));
private readonly MigrationArgsBase _args = args.ThrowIfNull(nameof(args));

/// <summary>
/// Performs the validation.
Expand All @@ -45,15 +41,14 @@ public ValidationResult GetValidationResult(CommandOption option, ValidationCont
try
{
// Load from the specified file on the file system or by using its long form name.
list.Add(File.Exists(name) ? Assembly.LoadFrom(name!) : Assembly.Load(name!));
_args.AddAssembly(File.Exists(name) ? Assembly.LoadFrom(name!) : Assembly.Load(name!));
}
catch (Exception ex)
{
return new ValidationResult($"The specified assembly '{name}' is invalid: {ex.Message}");
}
}

_args.Assemblies.InsertRange(0, list.ToArray());
return ValidationResult.Success!;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/DbEx/Console/MigrationConsoleBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public async Task<int> RunAsync(string[] args, CancellationToken cancellationTok
UpdateBooleanOption(EntryAssemblyOnlyOptionName, () =>
{
Args.Assemblies.Clear();
Args.ClearAssemblies();
Args.AddAssembly(Assembly.GetEntryAssembly()!);
});
Expand Down Expand Up @@ -416,10 +416,10 @@ public static void WriteStandardizedArgs(DatabaseMigrationBase migrator, Action<
migrator.Args.Logger.LogInformation("{Content}", $" {p.Key} = {p.Value}");
}

migrator.Args.Logger.LogInformation("{Content}", $"Assemblies{(migrator.Args.Assemblies.Count == 0 ? " = none" : ":")}");
migrator.Args.Logger.LogInformation("{Content}", $"Assemblies{(!migrator.Args.Assemblies.Any() ? " = none" : ":")}");
foreach (var a in migrator.Args.Assemblies)
{
migrator.Args.Logger.LogInformation("{Content}", $" {a.FullName}");
migrator.Args.Logger.LogInformation("{Content}", $" {a.Assembly.FullName}");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/DbEx/DbEx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CoreEx.Database" Version="3.8.0" />
<PackageReference Include="CoreEx.Database" Version="3.9.0" />
<PackageReference Include="OnRamp" Version="2.0.0" />
</ItemGroup>

Expand Down
57 changes: 33 additions & 24 deletions src/DbEx/Migration/DatabaseMigrationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public abstract class DatabaseMigrationBase : IDisposable
{
private const string NothingFoundText = " ** Nothing found. **";
private const string OnDatabaseCreateName = "post.database.create";
private static readonly string[] _splitters = ["\r\n", "\r", "\n"];
private HandlebarsCodeGenerator? _dataCodeGen;
private bool _hasInitialized = false;

Expand Down Expand Up @@ -119,11 +120,6 @@ protected DatabaseMigrationBase(MigrationArgsBase args)
/// </summary>
public string SchemaNamespace { get; set; } = "Schema";

/// <summary>
/// Gets or sets the <b>Migrations</b> scripts namespace part name.
/// </summary>
public string DataNamespace { get; set; } = "Data";

/// <summary>
/// Gets or sets the list of supported schema object types in the order of precedence.
/// </summary>
Expand Down Expand Up @@ -235,7 +231,7 @@ private void PreExecutionInitialization()
_hasInitialized = true;

var list = (List<string>)Namespaces;
Args.ProbeAssemblies.ForEach(x => list.Add(x.GetName().Name!));
Args.ProbeAssemblies.ForEach(x => list.Add(x.Assembly.GetName().Name!));

// Walk the assembly hierarchy.
var alist = new List<Assembly>();
Expand Down Expand Up @@ -441,9 +437,9 @@ protected virtual async Task<bool> DatabaseCreateAsync(CancellationToken cancell
var scripts = new List<DatabaseMigrationScript>();
foreach (var ass in Args.ProbeAssemblies)
{
foreach (var name in ass.GetManifestResourceNames().Where(rn => Namespaces.Any(ns => rn.StartsWith($"{ns}.{MigrationsNamespace}.", StringComparison.InvariantCulture) && rn.EndsWith($".{OnDatabaseCreateName}.sql", StringComparison.InvariantCultureIgnoreCase))).OrderBy(x => x))
foreach (var name in ass.Assembly.GetManifestResourceNames().Where(rn => Namespaces.Any(ns => rn.StartsWith($"{ns}.{MigrationsNamespace}.", StringComparison.InvariantCulture) && rn.EndsWith($".{OnDatabaseCreateName}.sql", StringComparison.InvariantCultureIgnoreCase))).OrderBy(x => x))
{
scripts.Add(new DatabaseMigrationScript(ass, name) { RunAlways = true });
scripts.Add(new DatabaseMigrationScript(ass.Assembly, name) { RunAlways = true });
}
}

Expand All @@ -467,7 +463,7 @@ private async Task<bool> DatabaseMigrateAsync(CancellationToken cancellationToke
var scripts = new List<DatabaseMigrationScript>();
foreach (var ass in Args.ProbeAssemblies)
{
foreach (var name in ass.GetManifestResourceNames().Where(rn => Namespaces.Any(ns => rn.StartsWith($"{ns}.{MigrationsNamespace}.", StringComparison.InvariantCulture))).OrderBy(x => x))
foreach (var name in ass.Assembly.GetManifestResourceNames().Where(rn => Namespaces.Any(ns => rn.StartsWith($"{ns}.{MigrationsNamespace}.", StringComparison.InvariantCulture))).OrderBy(x => x))
{
// Ignore any/all database create scripts.
if (name.EndsWith($".{OnDatabaseCreateName}.sql", StringComparison.InvariantCultureIgnoreCase))
Expand All @@ -477,7 +473,7 @@ private async Task<bool> DatabaseMigrateAsync(CancellationToken cancellationToke
var order = name.EndsWith(".pre.deploy.sql", StringComparison.InvariantCultureIgnoreCase) ? 1 :
name.EndsWith(".post.deploy.sql", StringComparison.InvariantCultureIgnoreCase) ? 3 : 2;

scripts.Add(new DatabaseMigrationScript(ass, name) { GroupOrder = order, RunAlways = order != 2 });
scripts.Add(new DatabaseMigrationScript(ass.Assembly, name) { GroupOrder = order, RunAlways = order != 2 });
}
}

Expand Down Expand Up @@ -534,7 +530,7 @@ private async Task<bool> DatabaseSchemaAsync(CancellationToken cancellationToken
Logger.LogInformation("{Content}", $" Probing for embedded resources: {string.Join(", ", GetNamespacesWithSuffix($"{SchemaNamespace}.*.sql"))}");
foreach (var ass in Args.ProbeAssemblies)
{
foreach (var rn in ass.GetManifestResourceNames().OrderBy(x => x))
foreach (var rn in ass.Assembly.GetManifestResourceNames().OrderBy(x => x))
{
// Filter on schema namespace prefix and suffix of '.sql'.
if (!(Namespaces.Any(x => rn.StartsWith($"{x}.{SchemaNamespace}.", StringComparison.InvariantCulture) && rn.EndsWith(".sql", StringComparison.InvariantCultureIgnoreCase))))
Expand All @@ -544,7 +540,7 @@ private async Task<bool> DatabaseSchemaAsync(CancellationToken cancellationToken
if (scripts.Any(x => x.Name == rn))
continue;

scripts.Add(new DatabaseMigrationScript(ass, rn));
scripts.Add(new DatabaseMigrationScript(ass.Assembly, rn));
}
}

Expand Down Expand Up @@ -695,20 +691,32 @@ protected virtual async Task<bool> DatabaseResetAsync(CancellationToken cancella
/// </summary>
private async Task<bool> DatabaseDataAsync(CancellationToken cancellationToken)
{
Logger.LogInformation("{Content}", $" Probing for embedded resources: {string.Join(", ", GetNamespacesWithSuffix($"{DataNamespace}.*", true))}");
var names = new List<string>();
foreach (var ass in Args.Assemblies)
{
foreach (var dns in ass.DataNamespaces)
{
names.Add($"{ass.Assembly.GetName().Name}.{dns}.*");
}
}

Logger.LogInformation("{Content}", $" Probing for embedded resources: {string.Join(", ", names)}");

var list = new List<(Assembly Assembly, string ResourceName)>();
foreach (var ass in Args.Assemblies.Distinct()) // Assumed data builds on top of earlier (do not use ProbeAssemblies as this is reversed).
foreach (var ass in Args.Assemblies)
{
foreach (var rn in ass.GetManifestResourceNames().OrderBy(x => x))
foreach (var rn in ass.Assembly.GetManifestResourceNames().OrderBy(x => x))
{
// Filter on schema namespace prefix and supported suffixes.
if (!Namespaces.Any(x => rn.StartsWith($"{x}.{DataNamespace}.", StringComparison.InvariantCulture) && (rn.EndsWith(".sql", StringComparison.InvariantCultureIgnoreCase)
|| rn.EndsWith(".yaml", StringComparison.InvariantCultureIgnoreCase) || rn.EndsWith(".yml", StringComparison.InvariantCultureIgnoreCase)
|| rn.EndsWith(".json", StringComparison.InvariantCultureIgnoreCase) || rn.EndsWith(".jsn", StringComparison.InvariantCultureIgnoreCase))))
continue;
foreach (var dns in ass.DataNamespaces)
{
// Filter on schema namespace prefix and supported suffixes.
if (!Namespaces.Any(x => rn.StartsWith($"{x}.{dns}.", StringComparison.InvariantCulture) && (rn.EndsWith(".sql", StringComparison.InvariantCultureIgnoreCase)
|| rn.EndsWith(".yaml", StringComparison.InvariantCultureIgnoreCase) || rn.EndsWith(".yml", StringComparison.InvariantCultureIgnoreCase)
|| rn.EndsWith(".json", StringComparison.InvariantCultureIgnoreCase) || rn.EndsWith(".jsn", StringComparison.InvariantCultureIgnoreCase))))
continue;

list.Add((ass, rn));
list.Add((ass.Assembly, rn));
}
}
}

Expand Down Expand Up @@ -818,7 +826,7 @@ protected virtual async Task<bool> DatabaseDataAsync(List<DataTable> dataTables,
/// <summary>
/// Gets the <see cref="Namespaces"/> with the specified namespace suffix applied.
/// </summary>
private IEnumerable<string> GetNamespacesWithSuffix(string suffix, bool reverse = false)
private string[] GetNamespacesWithSuffix(string suffix, bool reverse = false)
{
if (suffix == null)
throw new ArgumentNullException(nameof(suffix));
Expand All @@ -829,7 +837,7 @@ private IEnumerable<string> GetNamespacesWithSuffix(string suffix, bool reverse
list.Add($"{ns}.{suffix}");
}

return list.Count == 0 ? new string[] { "(none)" } : [.. list];
return list.Count == 0 ? ["(none)"] : [.. list];
}

/// <summary>
Expand All @@ -850,6 +858,7 @@ public async Task<bool> CreateScriptAsync(string? name = null, IDictionary<strin
/// </summary>
private async Task<bool> CreateScriptInternalAsync(string? name, IDictionary<string, string?>? parameters, CancellationToken cancellationToken)
{

name ??= "Default";
var rn = $"Script{name}_sql";

Expand All @@ -867,7 +876,7 @@ private async Task<bool> CreateScriptInternalAsync(string? name, IDictionary<str

// Extract the filename from content if specified.
var data = new { Parameters = parameters ?? new Dictionary<string, string?>() };
var lines = txt.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
var lines = txt.Split(_splitters, StringSplitOptions.None);
string fn = "new-script";
foreach (var line in lines)
{
Expand Down
Loading

0 comments on commit df5ddd6

Please sign in to comment.