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;
|
2011-05-24 00:34:57 +00:00
|
|
|
|
using NzbDrone.Core.Repository;
|
2011-05-23 23:29:14 +00:00
|
|
|
|
using SubSonic.Extensions;
|
|
|
|
|
using SubSonic.Schema;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Datastore
|
|
|
|
|
{
|
|
|
|
|
public class Migrations
|
|
|
|
|
{
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
2011-06-04 18:19:22 +00:00
|
|
|
|
public static void Run(string connetionString)
|
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-04 18:19:22 +00:00
|
|
|
|
var migrator = new Migrator.Migrator("Sqlite", connetionString,
|
2011-05-23 23:29:14 +00:00
|
|
|
|
Assembly.GetAssembly(typeof(Migrations)), true, new MigrationLogger());
|
|
|
|
|
|
2011-06-04 18:19:22 +00:00
|
|
|
|
migrator.MigrateToLastVersion();
|
2011-05-23 23:29:14 +00:00
|
|
|
|
|
|
|
|
|
Logger.Info("Database migration completed");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Logger.FatalException("An error has occured while migrating database", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void RemoveDeletedColumns(ITransformationProvider transformationProvider)
|
|
|
|
|
{
|
|
|
|
|
var provider = new RepositoryProvider();
|
|
|
|
|
var repoTypes = provider.GetRepositoryTypes();
|
|
|
|
|
|
|
|
|
|
foreach (var repoType in repoTypes)
|
|
|
|
|
{
|
|
|
|
|
var typeSchema = provider.GetSchemaFromType(repoType);
|
|
|
|
|
|
2011-05-24 00:34:57 +00:00
|
|
|
|
if (transformationProvider.TableExists(typeSchema.Name))
|
2011-05-23 23:29:14 +00:00
|
|
|
|
{
|
2011-05-24 00:34:57 +00:00
|
|
|
|
var dbColumns = provider.GetColumnsFromDatabase(transformationProvider, typeSchema.Name);
|
2011-05-23 23:29:14 +00:00
|
|
|
|
|
2011-05-24 00:34:57 +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);
|
2011-05-24 00:34:57 +00:00
|
|
|
|
if (transformationProvider.TableExists(typeSchema.Name))
|
|
|
|
|
{
|
|
|
|
|
var dbColumns = provider.GetColumnsFromDatabase(transformationProvider, typeSchema.Name);
|
2011-05-23 23:29:14 +00:00
|
|
|
|
|
2011-05-24 00:34:57 +00:00
|
|
|
|
var newColumns = provider.GetNewColumns(typeSchema, dbColumns);
|
2011-05-23 23:29:14 +00:00
|
|
|
|
|
2011-05-24 00:34:57 +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-05-24 00:34:57 +00:00
|
|
|
|
//Remove jobs table forcing it to repopulate
|
|
|
|
|
var repoProvider = new RepositoryProvider();
|
|
|
|
|
var jobTable = repoProvider.GetSchemaFromType(typeof(JobSetting));
|
|
|
|
|
|
|
|
|
|
Database.RemoveTable(jobTable.Name);
|
2011-06-04 01:56:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-24 00:34:57 +00:00
|
|
|
|
|
2011-05-23 23:29:14 +00:00
|
|
|
|
Migrations.RemoveDeletedColumns(Database);
|
|
|
|
|
Migrations.AddNewColumns(Database);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Down()
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|