1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-13 14:32:38 +01:00
Radarr/NzbDrone.Core/Datastore/MigrationsHelper.cs

126 lines
4.1 KiB
C#
Raw Normal View History

2011-05-24 01:29:14 +02:00
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using Migrator.Framework;
2011-06-15 04:31:41 +02:00
using MigSharp;
2011-05-24 01:29:14 +02:00
using NLog;
using NzbDrone.Core.Repository;
2011-06-05 08:35:03 +02:00
using NzbDrone.Core.Repository.Quality;
2011-05-24 01:29:14 +02:00
using SubSonic.Extensions;
2011-06-05 08:35:03 +02:00
using SubSonic.Repository;
2011-05-24 01:29:14 +02:00
using SubSonic.Schema;
namespace NzbDrone.Core.Datastore
{
2011-06-15 04:31:41 +02:00
public class MigrationsHelper
2011-05-24 01:29:14 +02:00
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
2011-06-05 08:02:31 +02:00
public static void Run(string connetionString, bool trace)
2011-05-24 01:29:14 +02:00
{
2011-06-04 20:19:22 +02:00
Logger.Info("Preparing run database migration");
2011-05-24 01:29:14 +02:00
try
{
2011-06-05 08:02:31 +02:00
Migrator.Migrator migrator;
if (trace)
{
2011-06-15 04:31:41 +02:00
migrator = new Migrator.Migrator("Sqlite", connetionString, Assembly.GetAssembly(typeof(MigrationsHelper)), true, new MigrationLogger());
2011-06-05 08:02:31 +02:00
}
else
{
2011-06-15 04:31:41 +02:00
migrator = new Migrator.Migrator("Sqlite", connetionString, Assembly.GetAssembly(typeof(MigrationsHelper)));
2011-06-05 08:02:31 +02:00
}
2011-05-24 01:29:14 +02:00
2011-06-04 20:19:22 +02:00
migrator.MigrateToLastVersion();
2011-05-24 01:29:14 +02:00
2011-06-05 08:35:03 +02:00
ForceSubSonicMigration(Connection.CreateSimpleRepository(connetionString));
2011-05-24 01:29:14 +02:00
Logger.Info("Database migration completed");
2011-06-05 08:35:03 +02:00
2011-05-24 01:29:14 +02:00
}
catch (Exception e)
{
Logger.FatalException("An error has occured while migrating database", e);
}
}
2011-06-15 04:31:41 +02:00
public static void MigrateDatabase(string connectionString)
{
var migrator = new MigSharp.Migrator(connectionString, ProviderNames.SQLite);
migrator.MigrateAll(typeof(MigrationsHelper).Assembly);
}
2011-06-05 08:35:03 +02:00
public static void ForceSubSonicMigration(IRepository repository)
{
repository.Single<Series>(1);
repository.Single<Episode>(1);
repository.Single<EpisodeFile>(1);
repository.Single<QualityProfile>(1);
repository.Single<History>(1);
repository.Single<IndexerSetting>(1);
repository.Single<SceneNameMapping>(1);
2011-06-05 08:35:03 +02:00
}
2011-05-24 01:29:14 +02:00
public static void RemoveDeletedColumns(ITransformationProvider transformationProvider)
{
var provider = new RepositoryProvider();
var repoTypes = provider.GetRepositoryTypes();
foreach (var repoType in repoTypes)
{
var typeSchema = provider.GetSchemaFromType(repoType);
if (transformationProvider.TableExists(typeSchema.Name))
2011-05-24 01:29:14 +02:00
{
var dbColumns = provider.GetColumnsFromDatabase(transformationProvider, typeSchema.Name);
2011-05-24 01:29:14 +02:00
var deletedColumns = provider.GetDeletedColumns(typeSchema, dbColumns);
foreach (var deletedColumn in deletedColumns)
{
Logger.Info("Removing column '{0}' from '{1}'", deletedColumn.Name, repoType.Name);
transformationProvider.RemoveColumn(typeSchema.Name, deletedColumn.Name);
}
}
2011-05-24 01:29:14 +02:00
}
}
public static void AddNewColumns(ITransformationProvider transformationProvider)
{
var provider = new RepositoryProvider();
var repoTypes = provider.GetRepositoryTypes();
foreach (var repoType in repoTypes)
{
var typeSchema = provider.GetSchemaFromType(repoType);
if (transformationProvider.TableExists(typeSchema.Name))
{
var dbColumns = provider.GetColumnsFromDatabase(transformationProvider, typeSchema.Name);
2011-05-24 01:29:14 +02:00
var newColumns = provider.GetNewColumns(typeSchema, dbColumns);
2011-05-24 01:29:14 +02:00
foreach (var newColumn in newColumns)
{
Logger.Info("Adding column '{0}' to '{1}'", newColumn.Name, repoType.Name);
transformationProvider.AddColumn(typeSchema.Name, newColumn);
}
2011-05-24 01:29:14 +02:00
}
}
}
}
}