Permalink
Browse files

giant refactor in order to start wpf project

  • Loading branch information...
1 parent 1e8cc6f commit 215e1256aada6d9bdbe90b2a3c775d5404408e99 @soitgoes committed Sep 21, 2012
@@ -12,15 +12,23 @@ namespace Mite.Builder {
public static class MigratorFactory {
public static Migrator GetMigratorFromConfig(string config, string directoryPath) {
var databaseRepositories = InstancesOf<IDatabaseRepository>();
- var jobj = JObject.Parse(config);
- var repoName = jobj.Value<string>("repositoryName");
- var connString = jobj.Value<string>("connectionString");
+ var options = JObject.Parse(config);
+ var repoName = options.Value<string>("repositoryName");
+ var connString = options.Value<string>("connectionString");
+
+ if (string.IsNullOrEmpty(repoName)) {
+ throw new Exception("Invalid Config - repositoryName is required.");
+ }
+ if (string.IsNullOrEmpty(connString)) {
+ throw new Exception("Invalid Config - connectionString is requied.");
+ }
object[] args = new object[]{connString, directoryPath};
foreach (var repoType in databaseRepositories)
{
if (repoName.ToLower() == repoType.Name.ToLower())
{
var dynamicRepo = (IDatabaseRepository)Activator.CreateInstance(repoType, BindingFlags.CreateInstance, null, args, null);
+
var miteDb = dynamicRepo.Create();
return new Migrator(miteDb, dynamicRepo);
}
@@ -12,7 +12,7 @@ public abstract class AnsiDatabaseRepository : IDatabaseRepository
protected IDbConnection connection;
protected string filePath;
-
+
public virtual void DropMigrationTable()
{
this.connection.Open();
@@ -25,14 +25,19 @@ public virtual void DropMigrationTable()
public abstract bool MigrationTableExists();
public abstract string GenerateSqlScript(bool includeData);
- public abstract MiteDatabase Init();
+ public abstract MigrationTracker Init();
public void Dispose()
{
connection.Dispose();
}
-
+
+ public IDbConnection Connection
+ {
+ get { return connection; }
+ set { connection = value; }
+ }
public virtual bool CheckConnection()
{
@@ -50,10 +55,12 @@ public virtual bool CheckConnection()
connection.Close();
}
}
- public virtual MiteDatabase Create()
+ public virtual MigrationTracker Create()
{
+ if (!DatabaseExists())
+ CreateDatabaseIfNotExists();
var hashes = new Dictionary<string, string>();
- //read all the migrations from the database and filesystem and create a MiteDatabase
+ //read all the migrations from the database and filesystem and create a MigrationTracker
//this.DatabaseExists() &&
if (this.MigrationTableExists())
{
@@ -69,12 +76,12 @@ public virtual MiteDatabase Create()
}
connection.Close();
}
- return new MiteDatabase(MigrationHelper.ReadFromDirectory(filePath).ToList(), hashes);
+ return new MigrationTracker(MigrationHelper.ReadFromDirectory(filePath).ToList(), hashes);
}
- public virtual MiteDatabase RecordMigration(Migration migration)
+ public virtual MigrationTracker RecordMigration(Migration migration)
{
if (!MigrationTableExists())
{
@@ -85,31 +92,25 @@ public virtual MiteDatabase RecordMigration(Migration migration)
cmd.ExecuteNonQuery();
connection.Close();
return Create();
- }
- public virtual void CreateDatabase()
- {
- using(var conn = GetConnWithoutDatabaseSpecified())
- {
- conn.Open();
- var cmd = conn.CreateCommand();
- cmd.CommandText = "create database " + connection.Database;
- cmd.ExecuteNonQuery();
- conn.Close();
- }
- }
+ }
+ /// <summary>
+ /// Create a database if it doesn't exists. Throw exception if the database already exists. Used to create a temporary database for verification
+ /// </summary>
+ public abstract void CreateDatabaseIfNotExists();
+
public virtual void DropDatabase()
{
using (var conn = GetConnWithoutDatabaseSpecified())
{
conn.Open();
var cmd = conn.CreateCommand();
- cmd.CommandText = "drop database " + connection.Database;
+ cmd.CommandText = "drop database " + this.DatabaseName;
cmd.ExecuteNonQuery();
conn.Close();
}
}
- public virtual MiteDatabase ExecuteUp(Migration migration)
+ public virtual MigrationTracker ExecuteUp(Migration migration)
{
if (!MigrationTableExists())
Init();
@@ -139,7 +140,7 @@ public virtual MiteDatabase ExecuteUp(Migration migration)
protected abstract IDbCommand GetMigrationCmd(Migration migration);
- public virtual MiteDatabase ExecuteDown(Migration migration)
+ public virtual MigrationTracker ExecuteDown(Migration migration)
{
if (!MigrationTableExists())
Init();
@@ -175,10 +176,15 @@ public virtual MiteDatabase ExecuteDown(Migration migration)
public string DatabaseName
{
get { return connection.Database; }
- set { connection.ChangeDatabase(value); }
+ set
+ {
+ connection.Open();
+ connection.ChangeDatabase(value);
+ connection.Close();
+ }
}
- protected abstract IDbConnection GetConnWithoutDatabaseSpecified();
+ public abstract IDbConnection GetConnWithoutDatabaseSpecified();
public virtual bool DatabaseExists()
{
@@ -187,7 +193,7 @@ public virtual bool DatabaseExists()
{
conn.Open();
var cmd = conn.CreateCommand();
- cmd.CommandText = "use " + connection.Database;
+ cmd.CommandText = "use " + DatabaseName;
try
{
cmd.ExecuteNonQuery();
@@ -1,20 +1,22 @@
-using System;
-
+using System;
+using System.Data;
+
namespace Mite.Core
{
public interface IDatabaseRepository :IDisposable
{
- MiteDatabase Init();
- MiteDatabase Create();
+ MigrationTracker Init();
+ MigrationTracker Create();
bool DatabaseExists();
- void CreateDatabase();
+ void CreateDatabaseIfNotExists();
/// <summary>
/// This method executes the sql for the migration and records the record in the migrations table (it is not responsible for whether or not to execute the migration).
/// </summary>
/// <param name="migration"></param>
- MiteDatabase ExecuteUp(Migration migration);
- MiteDatabase ExecuteDown(Migration migration);
- string DatabaseName { get; set; }
+ MigrationTracker ExecuteUp(Migration migration);
+ MigrationTracker ExecuteDown(Migration migration);
+ string DatabaseName { get; set; }
+ IDbConnection Connection { get; set; }
bool CheckConnection();
void DropMigrationTable();
bool MigrationTableExists();
@@ -25,8 +27,9 @@ public interface IDatabaseRepository :IDisposable
/// </summary>
/// <param name="migration"></param>
/// <returns></returns>
- MiteDatabase RecordMigration(Migration migration);
+ MigrationTracker RecordMigration(Migration migration);
- void DropDatabase();
+ void DropDatabase();
+ IDbConnection GetConnWithoutDatabaseSpecified();
}
}
@@ -4,14 +4,20 @@
namespace Mite.Core
{
- public interface IMiteDatabase
+ /// <summary>
+ /// Determines state of the database given provided migrations
+ /// </summary>
+ public interface IMigrationTracker
{
IEnumerable<Migration> MigrationsSince(DateTime dateTime);
IEnumerable<Migration> UnexcutedMigrations { get; }
bool IsHashMismatch();
bool IsMigrationGap();
bool IsValidState();
- Migration LastValidMigration { get; }
+ bool CanGoUp { get; }
+ bool CanGoDown { get; }
+
+ Migration LastValidMigration { get; }
IEnumerable<Migration> Migrations { get; }
IEnumerable<Migration> InvalidMigrations();
string Version { get; }
@@ -1,23 +1,23 @@
-using System;
-
-namespace Mite.Core
-{
- [Serializable]
- public class MiteException: Exception
- {
- private readonly Migration migration;
- private readonly MigrationDirection direction;
- private readonly Exception ex;
-
- public MiteException(Migration migration, MigrationDirection direction, Exception ex)
- {
- this.migration = migration;
- this.direction = direction;
- this.ex = ex;
- }
-
- public Exception Exception { get { return ex; } }
- public Migration Migration { get { return migration; } }
- public MigrationDirection Direction { get { return direction; } }
- }
+using System;
+
+namespace Mite.Core
+{
+ [Serializable]
+ public class MigrationException: Exception
+ {
+ private readonly Migration migration;
+ private readonly MigrationDirection direction;
+ private readonly Exception ex;
+
+ public MigrationException(Migration migration, MigrationDirection direction, Exception ex)
+ {
+ this.migration = migration;
+ this.direction = direction;
+ this.ex = ex;
+ }
+
+ public Exception Exception { get { return ex; } }
+ public Migration Migration { get { return migration; } }
+ public MigrationDirection Direction { get { return direction; } }
+ }
}
@@ -1,33 +1,31 @@
namespace Mite.Core
{
+ /// <summary>
+ /// Successful migration is assumed since MigrationException is thrown in the event of a failure
+ /// </summary>
public class MigrationResult
{
- private readonly bool success;
private readonly string message;
private readonly string priorToMigration;
private readonly string afterMigration;
- public MigrationResult(bool success, string message, string priorToMigration, string afterMigration)
+ public MigrationResult(string message, string priorToMigration, string afterMigration)
{
- this.success = success;
this.message = message;
this.priorToMigration = priorToMigration;
this.afterMigration = afterMigration;
}
- public MigrationResult(bool success, string message)
+ public MigrationResult( string message)
{
- this.success = success;
this.message = message;
}
- public MigrationResult(bool success, string priorToMigration, string afterMigration)
+ public MigrationResult(string priorToMigration, string afterMigration)
{
- this.success = success;
this.priorToMigration = priorToMigration;
this.afterMigration = afterMigration;
this.message = string.Format("Migration from {0} to {1} successful", priorToMigration, afterMigration);
}
- public bool Success { get { return success; } }
public string Message { get { return message; } }
public string PriorToMigration { get { return priorToMigration; } }
public string AfterMigration { get { return afterMigration; } }
Oops, something went wrong.

0 comments on commit 215e125

Please sign in to comment.