Lidarr/NzbDrone.Core/Datastore/MigrationsHelper.cs

60 lines
1.8 KiB
C#
Raw Normal View History

2013-02-16 21:10:20 +00:00
using System.Linq;
using System;
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
{
2013-02-04 04:18:59 +00:00
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
2011-05-23 23:29:14 +00:00
2013-02-04 04:18:59 +00:00
public static void Run(string connectionString, bool trace)
2011-05-23 23:29:14 +00:00
{
2013-02-04 04:18:59 +00:00
EnsureDatabase(connectionString);
2011-06-23 06:56:17 +00:00
2013-02-04 04:18:59 +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)
{
2013-02-04 04:18:59 +00:00
migrator = new Migrator.Migrator("sqlserverce", connectionString, Assembly.GetAssembly(typeof(MigrationsHelper)), true, new MigrationLogger());
2011-06-05 06:02:31 +00:00
}
else
{
2013-02-04 04:18:59 +00:00
migrator = new Migrator.Migrator("sqlserverce", connectionString, Assembly.GetAssembly(typeof(MigrationsHelper)));
2011-06-05 06:02:31 +00:00
}
2011-06-04 18:19:22 +00:00
migrator.MigrateToLastVersion();
2013-02-04 04:18:59 +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)
{
2013-02-04 04:18:59 +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 proxyType = Assembly.Load("NzbDrone.SqlCe").GetExportedTypes()[0];
var instance = Activator.CreateInstance(proxyType);
var factoryMethod = proxyType.GetMethod("EnsureDatabase");
factoryMethod.Invoke(instance, new object[] { constr });
2011-06-23 06:56:17 +00:00
}
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
}