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