Skip to content

Commit

Permalink
feat #36:First cut at --donotstorescriptsruntext (#52)
Browse files Browse the repository at this point in the history
* feat #36:First cut at --donotstorescriptsruntext

* Fixed syntax error after manual merge conflict resolution

Co-authored-by: Erik A. Brandstadmoen <[email protected]>
  • Loading branch information
wokket and erikbra authored Sep 26, 2021
1 parent 68c30f5 commit c229b7b
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 11 deletions.
3 changes: 1 addition & 2 deletions docs/MigratingFromRoundhousE.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ Expect this list to shrink over time.

- `--baseline`
- `--defaultencoding`
- `--donotstorescriptsruntext`
- `--dryrun`
- `--isuptodate`
- MSBuild Task.
Expand All @@ -43,4 +42,4 @@ Expect this list to shrink over time.
- `--runallanytimescripts`
- Recovery Modes (`--simple`, `--recoverymode` etc). An `up` script using `alter {{DatabaseName}} ...` may work for you in the meantime.
- Restore Options (`--restorefrompath`, `--restoreoptions`, `--restoretimeout` etc)
- Version info sourced from a file. `--version=<VALUE>` support is available on the command line
- Version info sourced from a file. `--version=<VALUE>` support is available on the command line
7 changes: 7 additions & 0 deletions grate.unittests/CommandLineParsing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ public async Task WarnOnOneTimeScriptChanges(string args, bool expected)
}

[TestCase("", false)]
[TestCase("--donotstorescriptsruntext", true)]
public async Task DoNotStoreScriptsRunText(string args, bool expected)
{
var cfg = await ParseGrateConfiguration(args);
cfg?.DoNotStoreScriptsRunText.Should().Be(expected);
}

[TestCase("--warnandignoreononetimescriptchanges", true)]
public async Task WarnAndIgnoreOnOneTimeScriptChanges(string args, bool expected)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,87 @@ public async Task Are_run_again_if_changed_between_runs()
scripts.Last().Should().Be(Context.Sql.SelectCurrentDatabase);
}
}

[Test]
public async Task Do_not_have_text_logged_if_flag_set()
{
var db = TestConfig.RandomDatabase();

GrateMigrator? migrator;

var knownFolders = KnownFolders.In(CreateRandomTempDirectory());
CreateDummySql(knownFolders.Sprocs);

var config = new GrateConfiguration
{
DoNotStoreScriptsRunText = true, // important
CreateDatabase = true,
ConnectionString = Context.ConnectionString(db),
AdminConnectionString = Context.AdminConnectionString,
Version = "a.b.c.d",
KnownFolders = knownFolders,
AlterDatabase = true,
NonInteractive = true,
Transaction = true,
DatabaseType = Context.DatabaseType
};

await using (migrator = Context.GetMigrator(config))
{
await migrator.Migrate();
}

string[] scripts;
string sql = $"SELECT text_of_script FROM {Context.Syntax.TableWithSchema("grate", "ScriptsRun")}";

await using (var conn = Context.CreateDbConnection(Context.ConnectionString(db)))
{
scripts = (await conn.QueryAsync<string>(sql)).ToArray();
}

scripts.Should().HaveCount(1);
scripts.Single().Should().Be(null);
}

[Test]
public async Task Do_have_text_logged_by_default()
{
var db = TestConfig.RandomDatabase();

GrateMigrator? migrator;

var knownFolders = KnownFolders.In(CreateRandomTempDirectory());
CreateDummySql(knownFolders.Sprocs);

var config = new GrateConfiguration
{
DoNotStoreScriptsRunText = false, // important
CreateDatabase = true,
ConnectionString = Context.ConnectionString(db),
AdminConnectionString = Context.AdminConnectionString,
Version = "a.b.c.d",
KnownFolders = knownFolders,
AlterDatabase = true,
NonInteractive = true,
Transaction = true,
DatabaseType = Context.DatabaseType
};

await using (migrator = Context.GetMigrator(config))
{
await migrator.Migrate();
}

string[] scripts;
string sql = $"SELECT text_of_script FROM {Context.Syntax.TableWithSchema("grate", "ScriptsRun")}";

await using (var conn = Context.CreateDbConnection(Context.ConnectionString(db)))
{
scripts = (await conn.QueryAsync<string>(sql)).ToArray();
}

scripts.Should().HaveCount(1);
scripts.Single().Should().Be(Context.Sql.SelectVersion);
}
}
}
7 changes: 7 additions & 0 deletions grate/Commands/MigrateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public MigrateCommand(GrateMigrator mi) : base("Migrates the database")
Add(WarnAndRunOnScriptChange());
Add(WarnAndIgnoreOnScriptChange());
Add(UserTokens());
Add(DoNotStoreScriptText());

Handler = CommandHandler.Create(
async () =>
Expand Down Expand Up @@ -169,5 +170,11 @@ private static Option<IEnumerable<string>> UserTokens() =>
new[] { "--ut", "--usertoken" },
"User Tokens - Allows grate to perform token replacement on custom tokens ({{my_token}}). Set as a key=value pair, eg '--ut=my_token=myvalue'. Can be specified multiple times."
);

private static Option<bool> DoNotStoreScriptText() =>
new(
new[] { "--donotstorescriptsruntext" },
"DoNotStoreScriptsRunText - This instructs grate to not store the full script text in the database. Defaults to false."
);
}
}
5 changes: 5 additions & 0 deletions grate/Configuration/GrateConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,10 @@ public string? AdminConnectionString
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>
public bool DoNotStoreScriptsRunText { get; init; }
}
}
4 changes: 2 additions & 2 deletions grate/Migration/AnsiSqlDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ SELECT 1 FROM {ScriptsRunTable}
return run ?? false;
}

public async Task InsertScriptRun(string scriptName, string sql, string hash, bool runOnce, object versionId)
public async Task InsertScriptRun(string scriptName, string? sql, string hash, bool runOnce, object versionId)
{
var cache = await GetScriptsRunCache();
cache.Remove(scriptName);
Expand All @@ -416,7 +416,7 @@ INSERT INTO {ScriptsRunTable}
await Connection.ExecuteAsync(insertSql, scriptRun);
}

public async Task InsertScriptRunError(string scriptName, string sql, string errorSql, string errorMessage, long versionId)
public async Task InsertScriptRunError(string scriptName, string? sql, string errorSql, string errorMessage, long versionId)
{
var insertSql = $@"
INSERT INTO {ScriptsRunErrorsTable}
Expand Down
8 changes: 5 additions & 3 deletions grate/Migration/DbMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,16 @@ private async Task OneTimeScriptChanged(string sql, string scriptName, long vers
private Task RecordScriptInScriptsRunTable(string scriptName, string sql, MigrationType migrationType, long versionId)
{
var hash = _hashGenerator.Hash(sql);

var sqlToStore = Configuration.DoNotStoreScriptsRunText ? null : sql;

_logger.LogDebug("Recording {scriptName} script ran on {serverName} - {databaseName}.", scriptName, Database.ServerName, Database.DatabaseName);
return Database.InsertScriptRun(scriptName, sql, hash, migrationType == MigrationType.Once, versionId);
return Database.InsertScriptRun(scriptName, sqlToStore, hash, migrationType == MigrationType.Once, versionId);
}

private Task RecordScriptInScriptsRunErrorsTable(string scriptName, string sql, string errorSql, string errorMessage, long versionId)
{
return Database.InsertScriptRunError(scriptName, sql, errorSql, errorMessage, versionId);
var sqlToStore = Configuration.DoNotStoreScriptsRunText ? null : sql;
return Database.InsertScriptRunError(scriptName, sqlToStore, errorSql, errorMessage, versionId);
}

public async ValueTask DisposeAsync()
Expand Down
4 changes: 2 additions & 2 deletions grate/Migration/IDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public interface IDatabase : IAsyncDisposable
Task RunSql(string sql, ConnectionType connectionType);
Task<string?> GetCurrentHash(string scriptName);
Task<bool> HasRun(string scriptName);
Task InsertScriptRun(string scriptName, string sql, string hash, bool runOnce, object versionId);
Task InsertScriptRunError(string scriptName, string sql, string errorSql, string errorMessage, long versionId);
Task InsertScriptRun(string scriptName, string? sql, string hash, bool runOnce, object versionId);
Task InsertScriptRunError(string scriptName, string? sql, string errorSql, string errorMessage, long versionId);
}
}
4 changes: 2 additions & 2 deletions grate/Migration/OracleDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ public Task<bool> HasRun(string scriptName)
throw new System.NotImplementedException();
}

public Task InsertScriptRun(string scriptName, string sql, string hash, bool runOnce, object versionId)
public Task InsertScriptRun(string scriptName, string? sql, string hash, bool runOnce, object versionId)
{
throw new System.NotImplementedException();
}

public Task InsertScriptRunError(string scriptName, string sql, string errorSql, string errorMessage, long versionId)
public Task InsertScriptRunError(string scriptName, string? sql, string errorSql, string errorMessage, long versionId)
{
throw new System.NotImplementedException();
}
Expand Down

0 comments on commit c229b7b

Please sign in to comment.