Skip to content

Commit

Permalink
Chore: Set verbosity to very low (Warning) on running internal migrat…
Browse files Browse the repository at this point in the history
…ions (#517)

* Chore: Set verbosity to very low (Warning) on running internal migrations
* Fixed some errors on ILLinking
* Use grate log output for unit tests too
  • Loading branch information
erikbra authored May 11, 2024
1 parent a4ca52f commit 1313227
Show file tree
Hide file tree
Showing 44 changed files with 203 additions and 81 deletions.
2 changes: 0 additions & 2 deletions src/grate.core/Configuration/GrateConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ public record GrateConfiguration

private static DirectoryInfo CurrentDirectory => new(Directory.GetCurrentDirectory());

public LogLevel Verbosity { get; init; } = LogLevel.Information;

/// <summary>
/// If true grate will not store script text in the database to save space in small/embedded databases.
/// </summary>
Expand Down
28 changes: 14 additions & 14 deletions src/grate.core/Migration/DbMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ namespace grate.Migration;

internal record DbMigrator : IDbMigrator
{
private readonly ILogger<DbMigrator> _logger;
public ILogger Logger { get; set; }
private readonly IHashGenerator _hashGenerator;

public DbMigrator(IDatabase database, ILogger<DbMigrator> logger, IHashGenerator hashGenerator, GrateConfiguration? configuration = null)
{
_logger = logger;
Logger = logger;
_hashGenerator = hashGenerator;
Configuration = configuration ?? throw new ArgumentException("No configuration passed to DbMigrator. Container setup error?", nameof(configuration));
Database = database;
Expand Down Expand Up @@ -54,7 +54,7 @@ public Task<long> VersionTheDatabase(string newVersion)
{
if (Configuration.DryRun)
{
_logger.LogDebug("Skipping writing database version row due to --dryrun");
Logger.LogDebug("Skipping writing database version row due to --dryrun");
return Task.FromResult(-1L);
}
else
Expand All @@ -78,7 +78,7 @@ public async Task<bool> RunSql(string sql, string scriptName, MigrationsFolder f

async Task<bool> LogAndRunSql()
{
_logger.LogInformation(" Running '{ScriptName}'.", scriptName);
Logger.LogInformation(" Running '{ScriptName}'.", scriptName);

if (Configuration.DryRun)
{
Expand Down Expand Up @@ -122,13 +122,13 @@ async Task<bool> LogAndRunSql()

case MigrationType.Once when changeHandling == ChangedScriptHandling.WarnAndRun:
LogScriptChangedWarning(scriptName);
_logger.LogDebug("Running script anyway due to WarnOnOneTimeScriptChanges option being set.");
Logger.LogDebug("Running script anyway due to WarnOnOneTimeScriptChanges option being set.");
theSqlWasRun = await LogAndRunSql();
break;

case MigrationType.Once when changeHandling == ChangedScriptHandling.WarnAndIgnore:
LogScriptChangedWarning(scriptName);
_logger.LogDebug("Ignoring script but marking as run due to WarnAndIgnoreOnOneTimeScriptChanges option being set.");
Logger.LogDebug("Ignoring script but marking as run due to WarnAndIgnoreOnOneTimeScriptChanges option being set.");
await RecordScriptInScriptsRunTable(scriptName, sql, type, versionId, transactionHandling);
break;

Expand All @@ -140,7 +140,7 @@ async Task<bool> LogAndRunSql()
}
else
{
_logger.LogDebug(" Skipped {ScriptName} - {Reason}.", scriptName, "No changes were found to run");
Logger.LogDebug(" Skipped {ScriptName} - {Reason}.", scriptName, "No changes were found to run");
}
}
else
Expand All @@ -160,7 +160,7 @@ public async Task<bool> RunSqlWithoutLogging(
{
async Task<bool> PrintLogAndRunSql()
{
_logger.LogInformation(" Running '{ScriptName}'.", scriptName);
Logger.LogInformation(" Running '{ScriptName}'.", scriptName);

if (Configuration.DryRun)
{
Expand Down Expand Up @@ -281,7 +281,7 @@ private async Task RunTheActualSql(string sql,
}
catch (Exception ex)
{
_logger.LogError("{ScriptName}: {ErrorMessage}", scriptName, ex.Message);
Logger.LogError("{ScriptName}: {ErrorMessage}", scriptName, ex.Message);

if (Transaction.Current is not null)
{
Expand Down Expand Up @@ -320,7 +320,7 @@ private async Task RunTheActualSqlWithoutLogging(
}
catch (Exception ex)
{
_logger.LogError("{ScriptName}: {ErrorMessage}", scriptName, ex.Message);
Logger.LogError("{ScriptName}: {ErrorMessage}", scriptName, ex.Message);

if (Transaction.Current is not null)
{
Expand All @@ -339,7 +339,7 @@ private async Task RunTheActualSqlWithoutLogging(

private void LogScriptChangedWarning(string scriptName)
{
_logger.LogWarning("{ScriptName} is a one time script that has changed since it was run.", scriptName);
Logger.LogWarning("{ScriptName} is a one time script that has changed since it was run.", scriptName);
}

/// <summary>
Expand All @@ -354,7 +354,7 @@ private void LogScriptChangedWarning(string scriptName)
/// <exception cref="Exceptions.OneTimeScriptChanged"></exception>
private async Task OneTimeScriptChanged(MigrationsFolder folder, string sql, string scriptName, long versionId)
{
_logger.LogError("{ScriptName}: {ErrorMessage}", scriptName, "One time script changed");
Logger.LogError("{ScriptName}: {ErrorMessage}", scriptName, "One time script changed");

Database.Rollback();
await Database.CloseConnection();
Expand Down Expand Up @@ -383,12 +383,12 @@ private Task RecordScriptInScriptsRunTable(string scriptName, string sql, Migrat

if (Configuration.DryRun)
{
_logger.LogTrace("Skipping recording {ScriptName} script ran on {ServerName} - {DatabaseName}, --dryrun prevents sql writes", scriptName, Database.ServerName, Database.DatabaseName);
Logger.LogTrace("Skipping recording {ScriptName} script ran on {ServerName} - {DatabaseName}, --dryrun prevents sql writes", scriptName, Database.ServerName, Database.DatabaseName);
return Task.CompletedTask;
}
else
{
_logger.LogTrace("Recording {ScriptName} script ran on {ServerName} - {DatabaseName}.", scriptName, Database.ServerName, Database.DatabaseName);
Logger.LogTrace("Recording {ScriptName} script ran on {ServerName} - {DatabaseName}.", scriptName, Database.ServerName, Database.DatabaseName);
return Database.InsertScriptRun(scriptName, sqlToStore, hash, migrationType == MigrationType.Once, versionId, transactionHandling);
}
}
Expand Down
17 changes: 13 additions & 4 deletions src/grate.core/Migration/GrateMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,23 @@ namespace grate.Migration;

internal record GrateMigrator : IGrateMigrator
{
private readonly ILogger<GrateMigrator> _logger;
private readonly ILoggerFactory _loggerFactory;
private readonly ILogger _logger;

internal IDbMigrator DbMigrator { get; private init; }

private string LogCategory => $"Grate.Migration{(IsInternalMigration() ? ".Internal" : "")}";

public GrateConfiguration Configuration
{
get => DbMigrator.Configuration;
private init
{
_logger = _loggerFactory.CreateLogger(LogCategory);

DbMigrator = (IDbMigrator) DbMigrator.Clone();
DbMigrator.Configuration = value;
DbMigrator.Logger = _logger;
}
}

Expand Down Expand Up @@ -49,10 +55,11 @@ public IGrateMigrator WithConfiguration(Action<GrateConfigurationBuilder> builde
public IGrateMigrator WithDatabase(IDatabase database) => this with { Database = database };


public GrateMigrator(ILogger<GrateMigrator> logger, IDbMigrator migrator)
public GrateMigrator(ILoggerFactory loggerFactory, IDbMigrator migrator)
{
_logger = logger;
DbMigrator = migrator;
_loggerFactory = loggerFactory;
_logger = loggerFactory.CreateLogger(LogCategory);
}

public async Task Migrate()
Expand Down Expand Up @@ -271,6 +278,9 @@ private async Task CreateGrateStructure(IDbMigrator dbMigrator)
}
}
}

private bool IsInternalMigration() => GrateEnvironment.Internal == this.Configuration?.Environment
|| GrateEnvironment.InternalBootstrap == this.Configuration?.Environment;


private async Task<(long, string)> VersionTheDatabase(IDbMigrator dbMigrator)
Expand Down Expand Up @@ -616,7 +626,6 @@ private async Task<GrateConfiguration> GetInternalGrateConfiguration(string inte
AccessToken = thisConfig.AccessToken,
CommandTimeout = thisConfig.CommandTimeout,
AdminCommandTimeout = thisConfig.AdminCommandTimeout,
Verbosity = LogLevel.Critical,
OutputPath = thisConfig.OutputPath.CreateSubdirectory("grate-internal"),

Baseline = baseline,
Expand Down
2 changes: 2 additions & 0 deletions src/grate.core/Migration/IDbMigrator.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using grate.Configuration;
using grate.Infrastructure;
using Microsoft.Extensions.Logging;

namespace grate.Migration;

internal interface IDbMigrator : IAsyncDisposable, ICloneable
{
GrateConfiguration Configuration { get; set; }
IDatabase Database { get; set; }
ILogger Logger { get; set; }
Task InitializeConnections();
Task<bool> CreateDatabase();

Expand Down
2 changes: 1 addition & 1 deletion src/grate.core/grate.core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
<RootNamespace>grate</RootNamespace>
<SourceRevisionId>build$([System.DateTime]::UtcNow.ToString("O"))</SourceRevisionId>

<IsPublishable>false</IsPublishable>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/grate.mariadb/grate.mariadb.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFrameworks>$(NetTargetFrameworks)</TargetFrameworks>
<Nullable>enable</Nullable>
<IsPublishable>false</IsPublishable>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/grate.oracle/grate.oracle.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFrameworks>$(NetTargetFrameworks)</TargetFrameworks>
<Nullable>enable</Nullable>
<IsPublishable>false</IsPublishable>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/grate.postgresql/grate.postgresql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFrameworks>$(NetTargetFrameworks)</TargetFrameworks>
<Nullable>enable</Nullable>
<IsPublishable>false</IsPublishable>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/grate.sqlite/grate.sqlite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFrameworks>$(NetTargetFrameworks)</TargetFrameworks>
<Nullable>enable</Nullable>
<IsPublishable>false</IsPublishable>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/grate.sqlserver/grate.sqlserver.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFrameworks>$(NetTargetFrameworks)</TargetFrameworks>
<Nullable>enable</Nullable>
<IsPublishable>false</IsPublishable>
</PropertyGroup>

<ItemGroup>
Expand Down
5 changes: 4 additions & 1 deletion src/grate/Configuration/CommandLineGrateConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Microsoft.Extensions.Logging;

namespace grate.Configuration;

internal record CommandLineGrateConfiguration : GrateConfiguration
Expand All @@ -6,5 +8,6 @@ internal record CommandLineGrateConfiguration : GrateConfiguration
/// Database type to use.
/// </summary>
public DatabaseType DatabaseType { get; set; }


public LogLevel Verbosity { get; init; } = LogLevel.Information;
}
1 change: 1 addition & 0 deletions src/grate/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ private static ServiceProvider BuildServiceProvider(CommandLineGrateConfiguratio
options.FormatterName = GrateConsoleFormatter.FormatterName;
options.LogToStandardErrorThreshold = LogLevel.Warning;
})
.AddFilter("Grate.Migration.Internal", LogLevel.Critical)
.SetMinimumLevel(config.Verbosity)
.AddConsoleFormatter<GrateConsoleFormatter, SimpleConsoleFormatterOptions>());

Expand Down
4 changes: 3 additions & 1 deletion src/grate/grate.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@
<InternalsVisibleTo Include="Docker.Oracle" />
<InternalsVisibleTo Include="Docker.PostgreSQL" />
<InternalsVisibleTo Include="Docker.Sqlite" />
<InternalsVisibleTo Include="Docker.SqlServer" />
<InternalsVisibleTo Include="Docker.SqlServer" />

<InternalsVisibleTo Include="TestCommon" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions unittests/Basic_tests/Basic_tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsPublishable>false</IsPublishable>
<IsTestProject>true</IsTestProject>
<RootNamespace>Basic_tests</RootNamespace>
</PropertyGroup>
Expand Down
51 changes: 37 additions & 14 deletions unittests/Basic_tests/GrateMigrator_.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentAssertions;
using Basic_tests.Infrastructure;
using FluentAssertions;
using grate.Configuration;
using grate.Infrastructure;
using grate.Migration;
Expand All @@ -10,43 +11,65 @@ namespace Basic_tests;
// ReSharper disable once InconsistentNaming
public class GrateMigrator_
{
private IDatabase _database = Substitute.For<IDatabase>();
private GrateConfiguration? _config = new GrateConfiguration();
private readonly IDatabase _database = Substitute.For<IDatabase>();
private readonly GrateConfiguration? _config = new GrateConfiguration();

[Fact]
public void Setting_the_config_does_not_change_the_original()
{
var config = new GrateConfiguration() { ConnectionString = "Server=server1" };
var dbMigrator = new DbMigrator(_database, null!, null!, config);
var grateMigrator = new GrateMigrator(null!, dbMigrator);

var grateMigrator = new GrateMigrator(new MockGrateLoggerFactory(), dbMigrator);

grateMigrator.Configuration.Should().BeEquivalentTo(config);

var changedConfig = config with { ConnectionString = "Server=server2" };
var changedMigrator = grateMigrator.WithConfiguration(changedConfig);

grateMigrator.Configuration.ConnectionString.Should().Be("Server=server1");
changedMigrator.Configuration.ConnectionString.Should().Be("Server=server2");
}

[Fact]
public void Setting_the_Database_does_not_change_the_original()
{
_database.DatabaseName.Returns("server1");
var dbMigrator = new DbMigrator(_database, null!, null!, _config);
var grateMigrator = new GrateMigrator(null!, dbMigrator);

var grateMigrator = new GrateMigrator(new MockGrateLoggerFactory(), dbMigrator);

grateMigrator.Database.DatabaseName.Should().Be("server1");

var changedDatabase = Substitute.For<IDatabase>();
changedDatabase.DatabaseName.Returns("server2");

var changedMigrator = grateMigrator.WithDatabase(changedDatabase) as GrateMigrator;

grateMigrator.Database.DatabaseName.Should().Be("server1");
changedMigrator!.Database.DatabaseName.Should().Be("server2");
}

[Theory]
[MemberData(nameof(Environments))]
public void Logger_has_the_correct_LogCategory(GrateEnvironment environment, string logCategory)
{
var config = new GrateConfiguration() { Environment = environment };
var dbMigrator = new DbMigrator(_database, null!, null!, config);

var loggerFactory = Substitute.For<ILoggerFactory>();
_ = new GrateMigrator(loggerFactory, dbMigrator);

loggerFactory.Received().CreateLogger(logCategory);
}

public static TheoryData<GrateEnvironment, string> Environments() => new()
{
{ GrateEnvironment.Internal, "Grate.Migration.Internal" },
{ GrateEnvironment.InternalBootstrap, "Grate.Migration.Internal" },
{ new GrateEnvironment("Development"), "Grate.Migration" },
{ new GrateEnvironment("Test"), "Grate.Migration" },
{ new GrateEnvironment("Production"), "Grate.Migration" },
};

}
3 changes: 3 additions & 0 deletions unittests/Basic_tests/Infrastructure/MockDbMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using grate.Infrastructure;
using grate.Migration;
using grate.Sqlite.Migration;
using Microsoft.Extensions.Logging;
using NSubstitute;

namespace Basic_tests.Infrastructure;
Expand All @@ -14,6 +15,8 @@ public record MockDbMigrator: IDbMigrator

public GrateConfiguration Configuration { get; set; } = null!;
public IDatabase Database { get; set; } = Substitute.For<IDatabase>();
public ILogger Logger { get; set; } = Substitute.For<ILogger>();

public Task InitializeConnections()
{
return Task.CompletedTask;
Expand Down
Loading

0 comments on commit 1313227

Please sign in to comment.