1
0
Fork 0
mirror of https://github.com/Sonarr/Sonarr synced 2024-12-25 17:27:18 +00:00
Sonarr/NzbDrone.Services/NzbDrone.Services.Service/Migrations/MigrationHelper.cs

55 lines
2.2 KiB
C#
Raw Normal View History

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
{
var migrator = new Migrator.Migrator("sqlserver", connetionString, Assembly.GetAssembly(typeof(MigrationsHelper)), true, new MigrationLogger());
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 16:53:08 +00:00
public static readonly Column VersionColumn = new Column("Version", DbType.String, 50, ColumnProperty.NotNull);
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);
}
}