Created
February 5, 2019 05:15
-
-
Save danielmackay/a51c314235e389a100f83d0754a08f8d to your computer and use it in GitHub Desktop.
DB Up Wrapper
This file contains hidden or 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
public class DatabaseMigrator : IDatabaseMigrator | |
{ | |
private readonly ILog log = LogManager.GetLogger(typeof(DatabaseMigrator)); | |
private UpgradeEngine engine; | |
private readonly string connectionString; | |
private const string JournalSchema = "dbo"; | |
private const string JournalTable = "__SchemaVersions"; | |
public DatabaseMigrator(string connectionString) | |
{ | |
this.connectionString = connectionString; | |
} | |
public void UseEmbeddedScripts(Assembly assembly) | |
{ | |
engine = DeployChanges.To | |
.SqlDatabase(connectionString) | |
.JournalToSqlTable(JournalSchema, JournalTable) | |
.WithScriptsEmbeddedInAssembly(assembly) | |
.LogTo(GetLogger()) | |
.Build(); | |
} | |
public void UseDirectoryPath(string path) | |
{ | |
engine = DeployChanges.To | |
.SqlDatabase(connectionString) | |
.JournalToSqlTable(JournalSchema, JournalTable) | |
.WithScriptsFromFileSystem(path) | |
.LogTo(GetLogger()) | |
.Build(); | |
} | |
public void Upgrade() | |
{ | |
var result = engine.PerformUpgrade(); | |
if (!result.Successful) | |
log.Error(result.Error); | |
} | |
public void Skip(string scriptName) => engine.MarkAsExecuted(scriptName); | |
public List<SqlScript> GetScriptsToExecute() => engine.GetScriptsToExecute(); | |
private IUpgradeLog GetLogger() => new UpgradeLogger(log); | |
private class UpgradeLogger : IUpgradeLog | |
{ | |
private readonly ILog log; | |
public UpgradeLogger(ILog log) | |
{ | |
this.log = log; | |
} | |
public void WriteError(string format, params object[] args) | |
{ | |
log.ErrorFormat(format, args); | |
} | |
public void WriteInformation(string format, params object[] args) | |
{ | |
log.InfoFormat(format, args); | |
} | |
public void WriteWarning(string format, params object[] args) | |
{ | |
log.WarnFormat(format, args); | |
} | |
} | |
} |
This file contains hidden or 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
public interface IDatabaseMigrator | |
{ | |
void UseEmbeddedScripts(Assembly assembly); | |
void UseDirectoryPath(string path); | |
void Upgrade(); | |
void Skip(string scriptName); | |
List<SqlScript> GetScriptsToExecute(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment