2011-05-23 16:29:14 -07:00
|
|
|
using System;
|
2011-06-17 19:51:53 -07:00
|
|
|
using System.Collections.Generic;
|
2011-06-22 23:56:17 -07:00
|
|
|
using System.Data.SqlServerCe;
|
|
|
|
using System.IO;
|
2011-05-23 16:29:14 -07:00
|
|
|
using System.Reflection;
|
|
|
|
using NLog;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Datastore
|
|
|
|
{
|
2011-06-14 19:31:41 -07:00
|
|
|
public class MigrationsHelper
|
2011-05-23 16:29:14 -07:00
|
|
|
{
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
2011-06-17 19:51:53 -07:00
|
|
|
public static readonly Dictionary<String, String> _migrated = new Dictionary<string, string>();
|
2011-06-16 23:58:50 -07:00
|
|
|
|
2011-06-04 23:02:31 -07:00
|
|
|
public static void Run(string connetionString, bool trace)
|
2011-05-23 16:29:14 -07:00
|
|
|
{
|
2011-06-17 19:51:53 -07:00
|
|
|
if (_migrated.ContainsKey(connetionString)) return;
|
|
|
|
_migrated.Add(connetionString, string.Empty);
|
|
|
|
|
2011-06-22 23:56:17 -07:00
|
|
|
EnsureDatabase(connetionString);
|
|
|
|
|
2011-10-16 19:03:54 -07:00
|
|
|
Logger.Trace("Preparing to run database migration");
|
2011-05-23 16:29:14 -07:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2011-06-04 23:02:31 -07:00
|
|
|
Migrator.Migrator migrator;
|
|
|
|
if (trace)
|
|
|
|
{
|
2011-06-22 23:56:17 -07:00
|
|
|
migrator = new Migrator.Migrator("sqlserverce", connetionString, Assembly.GetAssembly(typeof(MigrationsHelper)), true, new MigrationLogger());
|
2011-06-04 23:02:31 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-06-22 23:56:17 -07:00
|
|
|
migrator = new Migrator.Migrator("sqlserverce", connetionString, Assembly.GetAssembly(typeof(MigrationsHelper)));
|
2011-06-04 23:02:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-23 16:29:14 -07:00
|
|
|
|
2011-06-04 11:19:22 -07:00
|
|
|
migrator.MigrateToLastVersion();
|
2011-05-23 16:29:14 -07:00
|
|
|
|
2011-06-17 16:01:09 -07:00
|
|
|
//ForceSubSonicMigration(Connection.CreateSimpleRepository(connetionString));
|
2011-06-04 23:35:03 -07:00
|
|
|
|
2011-05-23 16:29:14 -07:00
|
|
|
Logger.Info("Database migration completed");
|
2011-06-04 23:35:03 -07:00
|
|
|
|
|
|
|
|
2011-05-23 16:29:14 -07:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2011-07-06 00:37:58 -07:00
|
|
|
Logger.FatalException("An error has occurred while migrating database", e);
|
2011-05-23 16:29:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-22 23:56:17 -07:00
|
|
|
private static void EnsureDatabase(string constr)
|
|
|
|
{
|
|
|
|
var connection = new SqlCeConnection(constr);
|
2011-11-13 16:22:18 -08:00
|
|
|
|
2011-06-22 23:56:17 -07:00
|
|
|
if (!File.Exists(connection.Database))
|
|
|
|
{
|
|
|
|
var engine = new SqlCeEngine(constr);
|
|
|
|
engine.CreateDatabase();
|
|
|
|
}
|
|
|
|
}
|
2011-06-23 19:04:07 -07:00
|
|
|
|
|
|
|
public static string GetIndexName(string tableName, params string[] columns)
|
|
|
|
{
|
|
|
|
return String.Format("IX_{0}_{1}", tableName, String.Join("_", columns));
|
|
|
|
}
|
2011-05-23 16:29:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-22 23:56:17 -07:00
|
|
|
|
|
|
|
|
2011-05-23 16:29:14 -07:00
|
|
|
}
|