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