Added class property DatabaseName and cleaned up usage elsewhere
This commit is contained in:
@@ -10,6 +10,7 @@ namespace DbTools {
|
|||||||
public class Migrations {
|
public class Migrations {
|
||||||
public string MigrationPath { get; set; }
|
public string MigrationPath { get; set; }
|
||||||
public string CompletedMigrationFile { get; set; } = "migrations.txt";
|
public string CompletedMigrationFile { get; set; } = "migrations.txt";
|
||||||
|
public string DatabaseName { get; set; }
|
||||||
|
|
||||||
public int Pending { get; private set; }
|
public int Pending { get; private set; }
|
||||||
public int Successful { get; private set; }
|
public int Successful { get; private set; }
|
||||||
@@ -20,14 +21,15 @@ namespace DbTools {
|
|||||||
public event MigrationCompletedEventHandler MigrationCompleted;
|
public event MigrationCompletedEventHandler MigrationCompleted;
|
||||||
public event MigrationStartedEventHandler MigrationStarted;
|
public event MigrationStartedEventHandler MigrationStarted;
|
||||||
|
|
||||||
public Migrations(string migrationPath) {
|
public Migrations(string migrationPath, string databaseName) {
|
||||||
MigrationPath = migrationPath;
|
MigrationPath = migrationPath;
|
||||||
|
DatabaseName = databaseName;
|
||||||
if (!Directory.Exists(MigrationPath)) {
|
if (!Directory.Exists(MigrationPath)) {
|
||||||
Directory.CreateDirectory(MigrationPath);
|
Directory.CreateDirectory(MigrationPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string[] GetAvailableMigrations(string databaseName, bool includeFailed = false) {
|
public string[] GetAvailableMigrations(bool includeFailed = false) {
|
||||||
string completedFile = Path.Combine(MigrationPath, CompletedMigrationFile);
|
string completedFile = Path.Combine(MigrationPath, CompletedMigrationFile);
|
||||||
List<string> completedMigrations = new List<string>();
|
List<string> completedMigrations = new List<string>();
|
||||||
if (!File.Exists(completedFile)) {
|
if (!File.Exists(completedFile)) {
|
||||||
@@ -44,7 +46,7 @@ namespace DbTools {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var migrationFiles = Directory.GetFiles(MigrationPath, databaseName + "_*.sql")
|
var migrationFiles = Directory.GetFiles(MigrationPath, DatabaseName + "_*.sql")
|
||||||
.Where(f => !completedMigrations.Contains(Path.GetFileName(f)))
|
.Where(f => !completedMigrations.Contains(Path.GetFileName(f)))
|
||||||
.OrderBy(f => f);
|
.OrderBy(f => f);
|
||||||
|
|
||||||
@@ -58,12 +60,12 @@ namespace DbTools {
|
|||||||
|
|
||||||
public async Task ApplyMigrationsAsync(IDbConnection dbConnection, string databaseName, bool attemptFailed = false) {
|
public async Task ApplyMigrationsAsync(IDbConnection dbConnection, string databaseName, bool attemptFailed = false) {
|
||||||
await Task.Run(() => {
|
await Task.Run(() => {
|
||||||
ApplyMigrations(dbConnection, databaseName, attemptFailed);
|
ApplyMigrations(dbConnection, attemptFailed);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ApplyMigrations(IDbConnection dbConnection, string databaseName, bool attemptFailed = false) {
|
public void ApplyMigrations(IDbConnection dbConnection, bool attemptFailed = false) {
|
||||||
string[] migrationFiles = GetAvailableMigrations(databaseName, attemptFailed);
|
string[] migrationFiles = GetAvailableMigrations(attemptFailed);
|
||||||
Total = migrationFiles.Count();
|
Total = migrationFiles.Count();
|
||||||
Pending = Total;
|
Pending = Total;
|
||||||
Failed = 0;
|
Failed = 0;
|
||||||
@@ -75,7 +77,7 @@ namespace DbTools {
|
|||||||
MigrationStarted?.Invoke(this, new MigrationStartedEventArgs(migrationName, --Pending));
|
MigrationStarted?.Invoke(this, new MigrationStartedEventArgs(migrationName, --Pending));
|
||||||
MigrationProgressReported?.Invoke(this, new MigrationProgressEventArgs(Total, Pending, Successful, Failed, $"Started Migration '{migrationName}'..."));
|
MigrationProgressReported?.Invoke(this, new MigrationProgressEventArgs(Total, Pending, Successful, Failed, $"Started Migration '{migrationName}'..."));
|
||||||
|
|
||||||
// TODO : Do the work
|
// Do the work
|
||||||
Exception error = null;
|
Exception error = null;
|
||||||
bool wasSuccessful = false;
|
bool wasSuccessful = false;
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user