Skip to content

Commit

Permalink
Tested on commandline. Seems to work now
Browse files Browse the repository at this point in the history
  • Loading branch information
erikbra committed May 27, 2024
1 parent 2103f34 commit e36339b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/grate.core/Configuration/GrateConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,5 @@ public record GrateConfiguration
/// </summary>
internal bool DeferWritingToRunTables { get; set; }

public bool IsUpToDateCheck { get; set; }
public bool UpToDateCheck { get; set; }
}
4 changes: 2 additions & 2 deletions src/grate.core/Migration/GrateMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,11 @@ public async Task Migrate()
}

// If it's an up-to-date check, we output on the console if it's up-to-date or not.
if (config.IsUpToDateCheck)
if (config.UpToDateCheck)
{
var logger = _loggerFactory.CreateLogger(LogCategory + ".IsUpToDate");

logger.LogInformation("Database is up to date: {IsUpToDate}", MigrationResult.IsUpToDate);
logger.LogInformation("Up to date: {IsUpToDate}", MigrationResult.IsUpToDate);
foreach (var script in MigrationResult.ScriptsRun)
{
logger.LogDebug("Changed script: {ScriptName}", script);
Expand Down
5 changes: 5 additions & 0 deletions src/grate/Commands/MigrateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public MigrateCommand(IGrateMigrator mi) : base("Migrates the database")
Add(DryRun());
Add(Restore());
Add(IgnoreDirectoryNames());
Add(UpToDateCheck());

Handler = CommandHandler.Create(
async () =>
Expand Down Expand Up @@ -334,4 +335,8 @@ private static Option<bool> IgnoreDirectoryNames() =>
new[] { "--ignoredirectorynames", "--searchallinsteadoftraverse", "--searchallsubdirectoriesinsteadoftraverse" },
"IgnoreDirectoryNames - By default, scripts are ordered by relative path including subdirectories. This option searches subdirectories, but order is based on filename alone."
);

internal static Option<bool> UpToDateCheck() => new(
["--uptodatecheck", "--isuptodate"],
"Outputs whether the database is up to date or not (whether any non-everytime scripts would be run)");
}
8 changes: 1 addition & 7 deletions src/grate/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static async Task<int> Main(string[] args)
// Temporarily parse the configuration, to get the verbosity level, and potentially set parameters
// to support the "IsUpToDate" check.
var cfg = await ParseGrateConfiguration(args);
if (cfg.IsUpToDateCheck)
if (cfg.UpToDateCheck)
{
cfg = cfg with { Verbosity = LogLevel.Critical, DryRun = true };
}
Expand Down Expand Up @@ -88,7 +88,6 @@ private static async Task<CommandLineGrateConfiguration> ParseGrateConfiguration
var cmd = new MigrateCommand(null!)
{
Verbosity(),
IsUpToDateCheck(),
};

ParseResult p =
Expand Down Expand Up @@ -158,11 +157,6 @@ private static ServiceProvider BuildServiceProvider(CommandLineGrateConfiguratio
internal static Option<LogLevel> Verbosity() => new(
["-v", "--verbosity"],
"Verbosity level (as defined here: https://docs.microsoft.com/dotnet/api/Microsoft.Extensions.Logging.LogLevel)");

internal static Option<bool> IsUpToDateCheck() => new(
["--isuptodate"],
"Outputs whether the database is up to date or not (whether any non-everytime scripts would be run)");


private static T Create<T>() where T : notnull => _serviceProvider.GetRequiredService<T>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,18 @@ public async Task IgnoreDirectoryNames(string args, bool expected)
var cfg = await ParseGrateConfiguration(args);
cfg?.IgnoreDirectoryNames.Should().Be(expected);
}

[Theory]
[InlineData("", false)]
[InlineData("--isuptodate", true)]
[InlineData("--isuptodate false", false)]
[InlineData("--uptodatecheck", true)]
[InlineData("--uptodatecheck false", false)]
public async Task UpToDateCheck(string args, bool expected)
{
var cfg = await ParseGrateConfiguration(args);
cfg?.UpToDateCheck.Should().Be(expected);
}

private static async Task<CommandLineGrateConfiguration?> ParseGrateConfiguration(string commandline)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task False_if_scripts_run(bool dryRun)
grateMigrator.MigrationResult.Should().NotBeNull();
grateMigrator.MigrationResult.IsUpToDate.Should().BeFalse();

_logger.LoggedMessages.Should().Contain("Database is up to date: False");
_logger.LoggedMessages.Should().Contain("Up to date: False");
_logger.LoggedMessages.Should().Contain("Changed script: script_that_is_run.sql");
}

Expand Down Expand Up @@ -106,7 +106,7 @@ private GrateMigrator CreateMigrator(Dictionary<string, List<(string, string)>>
SqlFilesDirectory = SqlFilesDirectory,
NonInteractive = true,
DryRun = dryRun,
IsUpToDateCheck = true
UpToDateCheck = true
};

var dbMigrator = Substitute.ForPartsOf<MockDbMigrator>();
Expand Down

0 comments on commit e36339b

Please sign in to comment.