2012-02-05 09:34:36 +03:00
|
|
|
using System;
|
|
|
|
using System.Data;
|
|
|
|
using System.Reflection;
|
|
|
|
using System.Web.Hosting;
|
|
|
|
using Migrator.Framework;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Core.Datastore;
|
|
|
|
|
|
|
|
namespace NzbDrone.Services.Service.Migrations
|
|
|
|
{
|
|
|
|
public class MigrationsHelper
|
|
|
|
{
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
|
|
public static void Run(string connetionString)
|
|
|
|
{
|
|
|
|
logger.Info("Preparing to run database migration");
|
|
|
|
|
|
|
|
VerifyConnectionString(connetionString);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2012-03-01 08:10:56 +03:00
|
|
|
var migrator = new Migrator.Migrator("sqlserver", connetionString, Assembly.GetAssembly(typeof(MigrationsHelper)), true);
|
2012-02-05 09:34:36 +03:00
|
|
|
migrator.MigrateToLastVersion();
|
|
|
|
logger.Info("Database migration completed");
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
logger.FatalException("An error has occurred while migrating database", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void VerifyConnectionString(string connectionString)
|
|
|
|
{
|
|
|
|
if(connectionString == null) throw new ArgumentNullException("connectionString");
|
|
|
|
|
|
|
|
if (HostingEnvironment.ApplicationPhysicalPath != null && HostingEnvironment.ApplicationPhysicalPath.ToLower().Contains("stage") &&
|
|
|
|
!connectionString.ToLower().Contains("stage"))
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Attempting to migrate production database from staging environment");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string GetIndexName(string tableName, params string[] columns)
|
|
|
|
{
|
|
|
|
return String.Format("IX_{0}_{1}", tableName, String.Join("_", columns));
|
|
|
|
}
|
|
|
|
|
2012-02-05 19:53:08 +03:00
|
|
|
public static readonly Column VersionColumn = new Column("Version", DbType.String, 50, ColumnProperty.NotNull);
|
2012-02-05 09:34:36 +03:00
|
|
|
public static readonly Column ProductionColumn = new Column("IsProduction", DbType.Boolean, ColumnProperty.NotNull);
|
|
|
|
public static readonly Column TimestampColumn = new Column("TimeStamp", DbType.DateTime, ColumnProperty.NotNull);
|
|
|
|
public static readonly Column UGuidColumn = new Column("UGuid", DbType.Guid, ColumnProperty.Null);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|