1
0
Fork 0
mirror of https://github.com/Radarr/Radarr synced 2024-12-26 01:38:24 +00:00
Radarr/NzbDrone.Core/Datastore/MigrationsHelper.cs

126 lines
4.1 KiB
C#
Raw Normal View History

2011-05-23 23:29:14 +00: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 02:31:41 +00:00
using MigSharp;
2011-05-23 23:29:14 +00:00
using NLog;
using NzbDrone.Core.Repository;
2011-06-05 06:35:03 +00:00
using NzbDrone.Core.Repository.Quality;
2011-05-23 23:29:14 +00:00
using SubSonic.Extensions;
2011-06-05 06:35:03 +00:00
using SubSonic.Repository;
2011-05-23 23:29:14 +00:00
using SubSonic.Schema;
namespace NzbDrone.Core.Datastore
{
2011-06-15 02:31:41 +00:00
public class MigrationsHelper
2011-05-23 23:29:14 +00:00
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
2011-06-05 06:02:31 +00:00
public static void Run(string connetionString, bool trace)
2011-05-23 23:29:14 +00:00
{
2011-06-04 18:19:22 +00:00
Logger.Info("Preparing 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)
{
2011-06-15 02:31:41 +00:00
migrator = new Migrator.Migrator("Sqlite", connetionString, Assembly.GetAssembly(typeof(MigrationsHelper)), true, new MigrationLogger());
2011-06-05 06:02:31 +00:00
}
else
{
2011-06-15 02:31:41 +00:00
migrator = new Migrator.Migrator("Sqlite", connetionString, Assembly.GetAssembly(typeof(MigrationsHelper)));
2011-06-05 06:02:31 +00:00
}
2011-05-23 23:29:14 +00:00
2011-06-04 18:19:22 +00:00
migrator.MigrateToLastVersion();
2011-05-23 23:29:14 +00:00
2011-06-05 06:35:03 +00:00
ForceSubSonicMigration(Connection.CreateSimpleRepository(connetionString));
2011-05-23 23:29:14 +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)
{
Logger.FatalException("An error has occured while migrating database", e);
}
}
2011-06-15 02:31:41 +00:00
public static void MigrateDatabase(string connectionString)
{
var migrator = new MigSharp.Migrator(connectionString, ProviderNames.SQLite);
migrator.MigrateAll(typeof(MigrationsHelper).Assembly);
}
2011-06-05 06:35:03 +00: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 06:35:03 +00:00
}
2011-05-23 23:29:14 +00: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-23 23:29:14 +00:00
{
var dbColumns = provider.GetColumnsFromDatabase(transformationProvider, typeSchema.Name);
2011-05-23 23:29:14 +00: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-23 23:29:14 +00: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-23 23:29:14 +00:00
var newColumns = provider.GetNewColumns(typeSchema, dbColumns);
2011-05-23 23:29:14 +00:00
foreach (var newColumn in newColumns)
{
Logger.Info("Adding column '{0}' to '{1}'", newColumn.Name, repoType.Name);
transformationProvider.AddColumn(typeSchema.Name, newColumn);
}
2011-05-23 23:29:14 +00:00
}
}
}
}
}