-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from datalust/dev
Release
- Loading branch information
Showing
23 changed files
with
302 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System.Text; | ||
using Datalust.Piggy.Database; | ||
using Datalust.Piggy.History; | ||
using Datalust.Piggy.Status; | ||
using Npgsql; | ||
using Serilog; | ||
|
||
namespace Datalust.Piggy.Baseline | ||
{ | ||
class BaselineSession | ||
{ | ||
public static void BaselineDatabase(string host, string database, string username, string password, string scriptRoot) | ||
{ | ||
using (var connection = DatabaseConnector.Connect(host, database, username, password, false)) | ||
{ | ||
var scripts = DatabaseStatus.GetPendingScripts(connection, scriptRoot); | ||
|
||
Log.Information("Found {Count} script files without change log records", scripts.Length); | ||
|
||
if (scripts.Length != 0) | ||
{ | ||
var commandText = new StringBuilder(); | ||
commandText.AppendLine("START TRANSACTION ISOLATION LEVEL REPEATABLE READ;"); | ||
commandText.AppendLine(AppliedChangeScriptLog.ChangesTableCreateScript); | ||
|
||
foreach (var script in scripts) | ||
{ | ||
Log.Information("Recording {FullPath} as {ScriptFile}", script.FullPath, script.RelativeName); | ||
commandText.AppendLine(AppliedChangeScriptLog.CreateApplyLogScriptFor(script)); | ||
} | ||
|
||
commandText.AppendLine("COMMIT TRANSACTION;"); | ||
|
||
Log.Information("Writing change log"); | ||
using (var command = new NpgsqlCommand(commandText.ToString(), connection)) | ||
{ | ||
command.ExecuteNonQuery(); | ||
} | ||
} | ||
|
||
Log.Information("Done"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,12 @@ | ||
using System.Collections.Generic; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Datalust.Piggy.Cli | ||
{ | ||
public abstract class CommandFeature | ||
abstract class CommandFeature | ||
{ | ||
protected CommandFeature() | ||
{ | ||
Errors = new List<string>(); | ||
} | ||
|
||
public abstract void Enable(OptionSet options); | ||
|
||
public IList<string> Errors { get; private set; } | ||
public virtual IEnumerable<string> GetUsageErrors() => Array.Empty<string>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using Datalust.Piggy.Baseline; | ||
using Datalust.Piggy.Cli.Features; | ||
using Datalust.Piggy.Update; | ||
using Npgsql; | ||
using Serilog; | ||
|
||
namespace Datalust.Piggy.Cli.Commands | ||
{ | ||
[Command("baseline", "Add scripts to the change log without running them")] | ||
class BaselineCommand : Command | ||
{ | ||
readonly UsernamePasswordFeature _usernamePasswordFeature; | ||
readonly DatabaseFeature _databaseFeature; | ||
readonly LoggingFeature _loggingFeature; | ||
readonly ScriptRootFeature _scriptRootFeature; | ||
|
||
public BaselineCommand() | ||
{ | ||
_databaseFeature = Enable<DatabaseFeature>(); | ||
_usernamePasswordFeature = Enable<UsernamePasswordFeature>(); | ||
_scriptRootFeature = Enable<ScriptRootFeature>(); | ||
_loggingFeature = Enable<LoggingFeature>(); | ||
} | ||
|
||
protected override int Run() | ||
{ | ||
_loggingFeature.Configure(); | ||
|
||
try | ||
{ | ||
BaselineSession.BaselineDatabase( | ||
_databaseFeature.Host, _databaseFeature.Database, _usernamePasswordFeature.Username, _usernamePasswordFeature.Password, | ||
_scriptRootFeature.ScriptRoot); | ||
|
||
return 0; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Log.Fatal(ex, "Could not baseline the database"); | ||
return -1; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using Datalust.Piggy.Cli.Features; | ||
using Datalust.Piggy.Database; | ||
using Datalust.Piggy.Status; | ||
using Serilog; | ||
|
||
namespace Datalust.Piggy.Cli.Commands | ||
{ | ||
[Command("pending", "Determine which scripts will be run in an update")] | ||
class PendingCommand : Command | ||
{ | ||
readonly UsernamePasswordFeature _usernamePasswordFeature; | ||
readonly DatabaseFeature _databaseFeature; | ||
readonly LoggingFeature _loggingFeature; | ||
readonly ScriptRootFeature _scriptRootFeature; | ||
|
||
public PendingCommand() | ||
{ | ||
_databaseFeature = Enable<DatabaseFeature>(); | ||
_usernamePasswordFeature = Enable<UsernamePasswordFeature>(); | ||
_scriptRootFeature = Enable<ScriptRootFeature>(); | ||
_loggingFeature = Enable<LoggingFeature>(); | ||
} | ||
|
||
protected override int Run() | ||
{ | ||
_loggingFeature.Configure(); | ||
|
||
try | ||
{ | ||
using (var connection = DatabaseConnector.Connect(_databaseFeature.Host, _databaseFeature.Database, | ||
_usernamePasswordFeature.Username, _usernamePasswordFeature.Password, false)) | ||
{ | ||
foreach (var pending in DatabaseStatus.GetPendingScripts(connection, _scriptRootFeature.ScriptRoot)) | ||
{ | ||
Console.WriteLine($"{pending.RelativeName}"); | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Log.Fatal(ex, "Could not determine pending change scripts"); | ||
return -1; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.