Sonarr/NzbDrone.Core/Datastore/Migrations.cs

176 lines
5.7 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;
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
{
public class Migrations
{
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)
{
migrator = new Migrator.Migrator("Sqlite", connetionString, Assembly.GetAssembly(typeof(Migrations)), true, new MigrationLogger());
}
else
{
migrator = new Migrator.Migrator("Sqlite", connetionString, Assembly.GetAssembly(typeof(Migrations)));
}
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-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);
}
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
}
}
}
}
[Migration(20110523)]
public class Migration20110523 : Migration
{
public override void Up()
{
2011-06-05 06:02:31 +00:00
Database.RemoveTable(RepositoryProvider.JobsSchema.Name);
}
public override void Down()
{
throw new NotImplementedException();
}
}
[Migration(20110603)]
public class Migration20110603 : Migration
{
public override void Up()
{
2011-06-04 18:19:22 +00:00
Database.RemoveTable("Seasons");
2011-05-23 23:29:14 +00:00
Migrations.RemoveDeletedColumns(Database);
Migrations.AddNewColumns(Database);
}
public override void Down()
{
throw new NotImplementedException();
}
}
2011-06-05 06:02:31 +00:00
[Migration(20110604)]
public class Migration20110604 : Migration
{
public override void Up()
{
2011-06-05 06:35:03 +00:00
Migrations.ForceSubSonicMigration(Connection.CreateSimpleRepository(Connection.MainConnectionString));
2011-06-05 06:02:31 +00:00
var episodesTable = RepositoryProvider.EpisodesSchema;
//Database.AddIndex("idx_episodes_series_season_episode", episodesTable.Name, true,
// episodesTable.GetColumnByPropertyName("SeriesId").Name,
// episodesTable.GetColumnByPropertyName("SeasonNumber").Name,
// episodesTable.GetColumnByPropertyName("EpisodeNumber").Name);
Database.AddIndex("idx_episodes_series_season", episodesTable.Name, false,
episodesTable.GetColumnByPropertyName("SeriesId").Name,
episodesTable.GetColumnByPropertyName("SeasonNumber").Name);
Database.AddIndex("idx_episodes_series", episodesTable.Name, false,
episodesTable.GetColumnByPropertyName("SeriesId").Name);
Migrations.RemoveDeletedColumns(Database);
Migrations.AddNewColumns(Database);
}
public override void Down()
{
throw new NotImplementedException();
}
}
2011-05-23 23:29:14 +00:00
}