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

#30 Add optional parameter for migration dependencies #31

Merged
merged 17 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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
7 changes: 7 additions & 0 deletions Curiosity.Migrations.sln
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Curiosity.Migrations.Utils"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Curiosity.Migrations.PostgreSQL.UnitTests", "tests\UnitTests\Curiosity.Migrations.PostgreSQL.UnitTests\Curiosity.Migrations.PostgreSQL.UnitTests.csproj", "{0FAA319D-210A-4FC4-AA3E-EC9BE03F024B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Curiosity.Migrations.DependencyTests", "tests\IntegrationTests\Curiosity.Migrations.DependencyTests\Curiosity.Migrations.DependencyTests.csproj", "{163A6F22-7218-43E1-8530-67EC45D26D2D}"
ShockThunder marked this conversation as resolved.
Show resolved Hide resolved
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -101,6 +103,7 @@ Global
{EC0D7D04-8030-41E3-BE96-47DB3ADF99E3} = {0E062AC8-023E-4D62-A696-8363130215DF}
{3FB22746-EBBF-4A2C-A0B3-73AD18AA42A5} = {22DB0148-0BD7-4846-B63F-421B490392AB}
{0FAA319D-210A-4FC4-AA3E-EC9BE03F024B} = {8DB6C739-ACB4-47CD-B72E-A5180C22B84F}
{163A6F22-7218-43E1-8530-67EC45D26D2D} = {98A180F1-B3B9-41F0-9974-2333B4F03390}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E2F484A1-607F-4193-9592-341EFE7890BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -131,5 +134,9 @@ Global
{0FAA319D-210A-4FC4-AA3E-EC9BE03F024B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0FAA319D-210A-4FC4-AA3E-EC9BE03F024B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0FAA319D-210A-4FC4-AA3E-EC9BE03F024B}.Release|Any CPU.Build.0 = Release|Any CPU
{163A6F22-7218-43E1-8530-67EC45D26D2D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{163A6F22-7218-43E1-8530-67EC45D26D2D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{163A6F22-7218-43E1-8530-67EC45D26D2D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{163A6F22-7218-43E1-8530-67EC45D26D2D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
13 changes: 13 additions & 0 deletions src/Curiosity.Migrations/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Changelog

## [4.2.1] - 2023-11-13
ShockThunder marked this conversation as resolved.
Show resolved Hide resolved

### Added

- Options for migration dependencies.
```
--CURIOSITY:Dependencies=Major1.Minor1 Major2.Minor2
```

### Changed

- New parameter at ScriptMigration constructor.

## [4.2.0] - 2023-04-21

### Changed
Expand Down
30 changes: 21 additions & 9 deletions src/Curiosity.Migrations/MigrationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private async Task<MigrationResult> MigrateAsync(
await _migrationConnection.CreateMigrationHistoryTableIfNotExistsAsync(cancellationToken);
_logger?.LogInformation($"Creating \"{_migrationConnection.MigrationHistoryTableName}\" table completed");
}

// get migrations to apply
var migrationsToApply = await GetMigrationsToApplyAsync(isUpgrade, false, cancellationToken);
if (migrationsToApply.Count == 0)
Expand Down Expand Up @@ -196,7 +196,7 @@ private async Task<MigrationResult> MigrateAsync(
var policy = isUpgrade
? _upgradePolicy
: _downgradePolicy;
var migrationResult = await MigrateAsync(migrationsToApply, isUpgrade, policy, cancellationToken);
var migrationResult = await ApplyMigrations(migrationsToApply, isUpgrade, policy, cancellationToken);

await _migrationConnection.CloseConnectionAsync();
_logger?.LogInformation($"Migrating database \"{_migrationConnection.DatabaseName}\" completed. Successfully applied {migrationsToApply.Count} migrations");
Expand Down Expand Up @@ -380,7 +380,7 @@ private async Task<bool> ExecutePreMigrationScriptsAsync(CancellationToken token
return true;
}

private async Task<(IReadOnlyList<MigrationInfo> Applied, IReadOnlyList<MigrationInfo> Skipped)> MigrateAsync(
private async Task<(IReadOnlyList<MigrationInfo> Applied, IReadOnlyList<MigrationInfo> Skipped)> ApplyMigrations(
IReadOnlyList<IMigration> orderedMigrations,
bool isUpgrade,
MigrationPolicy policy,
Expand All @@ -399,8 +399,9 @@ private async Task<bool> ExecutePreMigrationScriptsAsync(CancellationToken token
? "Upgrade"
: "Downgrade";

var appliedMigrations = new List<MigrationInfo>(orderedMigrations.Count);
var skippedMigrations = new List<MigrationInfo>();
var appliedMigrationVersions = (await _migrationConnection.GetAppliedMigrationVersionsAsync(cancellationToken)).ToList();
ShockThunder marked this conversation as resolved.
Show resolved Hide resolved
var currentAppliedMigrations = new List<MigrationInfo>(orderedMigrations.Count);
var currentSkippedMigrations = new List<MigrationInfo>();

for (var i = 0; i < orderedMigrations.Count; i++)
{
Expand All @@ -410,17 +411,27 @@ private async Task<bool> ExecutePreMigrationScriptsAsync(CancellationToken token
// check policies
if (!policy.HasFlag(MigrationPolicy.LongRunningAllowed) && migration.IsLongRunning)
{
skippedMigrations.Add(currentMigration);
currentSkippedMigrations.Add(currentMigration);
_logger?.LogWarning($"Skip long-running migration \"{migration.Version}\" due to policy restriction");
continue;
}
if (!policy.HasFlag(MigrationPolicy.ShortRunningAllowed) && !migration.IsLongRunning)
{
skippedMigrations.Add(currentMigration);
currentSkippedMigrations.Add(currentMigration);
_logger?.LogWarning($"Skip short-running migration \"{migration.Version}\" due to policy restriction");
continue;
}

if (migration.Dependencies.Any())
ShockThunder marked this conversation as resolved.
Show resolved Hide resolved
{
foreach (var dependency in migration.Dependencies)
{
if(!appliedMigrationVersions.Contains(dependency))
throw new MigrationException(MigrationErrorCode.MigratingError,
$"Migration with version \"{migration.Version}\" depends on unapplied migration \"{dependency}\"");
}
}

DbTransaction? transaction = null;

try
Expand Down Expand Up @@ -461,7 +472,8 @@ private async Task<bool> ExecutePreMigrationScriptsAsync(CancellationToken token
// when disposed if either commands fails
transaction?.Commit();

appliedMigrations.Add(currentMigration);
currentAppliedMigrations.Add(currentMigration);
appliedMigrationVersions.Add(currentMigration.Version);
_logger?.LogInformation($"{operationName} to \"{migration.Version}\" (database \"{_migrationConnection.DatabaseName}\") completed.");
}
catch (MigrationException e)
Expand All @@ -486,7 +498,7 @@ private async Task<bool> ExecutePreMigrationScriptsAsync(CancellationToken token
}
}

return (appliedMigrations, skippedMigrations);
return (currentAppliedMigrations, currentSkippedMigrations);
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,21 @@ private MigrationOptions ExtractMigrationOptions(string sourceScript)
throw new InvalidOperationException($"Value \"{optionsMatch.Groups[2].Value}\" is not assignable to the option \"{optionsMatch.Groups[1].Value}\"");
}

break;
case "DEPENDENCIES":
var migrationDependencies = optionsMatch.Groups[2].Value.ToUpper().Trim().TrimEnd(';');

if(string.IsNullOrWhiteSpace(migrationDependencies))
ShockThunder marked this conversation as resolved.
Show resolved Hide resolved
throw new InvalidOperationException($"Value \"{optionsMatch.Groups[2].Value}\" is not assignable to the option \"{optionsMatch.Groups[1].Value}\"");

var rawVersions = migrationDependencies.Split(' ');
ShockThunder marked this conversation as resolved.
Show resolved Hide resolved
var versions = rawVersions
.Select(x =>
MigrationVersion.TryParse(x, out var version)
ShockThunder marked this conversation as resolved.
Show resolved Hide resolved
? version
: throw new InvalidOperationException($"Can't parse migration dependency {version}"));
options.Dependencies.AddRange(versions);

break;
default:
throw new InvalidOperationException($"Option \"{optionsMatch.Groups[1].Value}\" is unknown");
Expand All @@ -324,7 +339,7 @@ private MigrationOptions ExtractMigrationOptions(string sourceScript)

return options;
}

/// <summary>
/// Creates script migration. Replace variables placeholders with real values
/// </summary>
Expand Down Expand Up @@ -370,15 +385,17 @@ private IMigration CreateScriptMigration(
downScript,
migrationScriptInfo.Comment,
migrationScriptInfo.Options.IsTransactionRequired,
migrationScriptInfo.Options.IsLongRunning)
migrationScriptInfo.Options.IsLongRunning,
migrationScriptInfo.Options.Dependencies)
: new ScriptMigration(
migrationLogger,
migrationConnection,
migrationVersion,
upScript,
migrationScriptInfo.Comment,
migrationScriptInfo.Options.IsTransactionRequired,
migrationScriptInfo.Options.IsLongRunning);
migrationScriptInfo.Options.IsLongRunning,
migrationScriptInfo.Options.Dependencies);
}

private struct ScriptParsingOptions
Expand Down Expand Up @@ -418,5 +435,6 @@ private class MigrationOptions
public bool IsTransactionRequired { get; set; } = true;

public bool IsLongRunning { get; set; }
public List<MigrationVersion> Dependencies { get; set; } = new();
}
}
4 changes: 4 additions & 0 deletions src/Curiosity.Migrations/Migrations/CodeMigration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public abstract class CodeMigration : IMigration
/// <inheritdoc />
public bool IsLongRunning { get; protected set; } = false;

/// <inheritdoc />
[SuppressMessage("ReSharper", "AutoPropertyCanBeMadeGetOnly.Global")]
public List<MigrationVersion> Dependencies { get; protected set;} = new();

/// <summary>
/// Provides access to underlying database.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ public DowngradeScriptMigration(
IReadOnlyList<ScriptMigrationBatch>? downScripts,
string? comment,
bool isTransactionRequired = true,
bool isLongRunning = false)
bool isLongRunning = false,
List<MigrationVersion>? dependencies = null)
: base(
migrationLogger,
migrationConnection,
version,
upScripts,
comment,
isTransactionRequired,
isLongRunning)
isLongRunning,
dependencies)
{
DownScripts = downScripts?.ToArray() ?? Array.Empty<ScriptMigrationBatch>();
}
Expand Down
9 changes: 9 additions & 0 deletions src/Curiosity.Migrations/Migrations/IMigration.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -35,6 +36,14 @@ public interface IMigration
///
/// </remarks>
bool IsLongRunning { get; }

/// <summary>
/// Migrations, that should be applied before
/// </summary>
/// <remarks>
///
/// </remarks>
List<MigrationVersion> Dependencies { get; }
ShockThunder marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Upgrades database to the version specified in <see cref="Version"/>.
Expand Down
9 changes: 8 additions & 1 deletion src/Curiosity.Migrations/Migrations/ScriptMigration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public class ScriptMigration : IMigration
[SuppressMessage("ReSharper", "AutoPropertyCanBeMadeGetOnly.Global")]
public bool IsLongRunning { get; protected set; }

/// <inheritdoc />
[SuppressMessage("ReSharper", "AutoPropertyCanBeMadeGetOnly.Global")]
public List<MigrationVersion> Dependencies { get; protected set;} = new();

/// <summary>
/// SQL script to apply migration split into batches.
/// </summary>
Expand All @@ -46,7 +50,9 @@ public ScriptMigration(
IReadOnlyList<ScriptMigrationBatch> upScripts,
string? comment,
bool isTransactionRequired = true,
bool isLongRunning = false)
bool isLongRunning = false,
List<MigrationVersion>? dependencies = null
)
{
Guard.AssertNotNull(migrationConnection, nameof(migrationConnection));
Guard.AssertNotEmpty(upScripts, nameof(upScripts));
Expand All @@ -59,6 +65,7 @@ public ScriptMigration(
Comment = comment;
IsTransactionRequired = isTransactionRequired;
IsLongRunning = isLongRunning;
Dependencies = dependencies ?? new List<MigrationVersion>();
ShockThunder marked this conversation as resolved.
Show resolved Hide resolved
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;

namespace Curiosity.Migrations.DependencyTests.CodeMigrations;

public class CodeMigration_2_0 : CodeMigration
{
/// <inheritdoc />
public override MigrationVersion Version { get; } = new(2);

/// <inheritdoc />
public override string Comment => "Correct script via provider";

/// <inheritdoc />
public override async Task UpgradeAsync(DbTransaction? transaction = null, CancellationToken cancellationToken = default)
{
await MigrationConnection.ExecuteNonQuerySqlAsync("select 1;", null, cancellationToken);
}
}
Loading
Loading