Sonarr/NzbDrone.Core/Datastore/Migration/Framework/SqliteAlter.cs

42 lines
1.3 KiB
C#
Raw Normal View History

2013-07-05 03:56:27 +00:00
using System.Collections.Generic;
using System.Linq;
namespace NzbDrone.Core.Datastore.Migration.Framework
{
2013-07-05 03:56:27 +00:00
public interface ISQLiteAlter
{
2013-07-05 03:56:27 +00:00
void DropColumns(string tableName, IEnumerable<string> columns);
}
2013-07-05 03:56:27 +00:00
public class SQLiteAlter : ISQLiteAlter
{
private readonly ISQLiteMigrationHelper _sqLiteMigrationHelper;
2013-07-05 03:56:27 +00:00
public SQLiteAlter(ISQLiteMigrationHelper sqLiteMigrationHelper)
{
2013-07-05 03:56:27 +00:00
_sqLiteMigrationHelper = sqLiteMigrationHelper;
}
2013-07-05 03:56:27 +00:00
public void DropColumns(string tableName, IEnumerable<string> columns)
{
2013-07-05 03:56:27 +00:00
using (var transaction = _sqLiteMigrationHelper.BeginTransaction())
{
var originalColumns = _sqLiteMigrationHelper.GetColumns(tableName);
2013-07-05 03:56:27 +00:00
var newColumns = originalColumns.Where(c => !columns.Contains(c.Key)).Select(c => c.Value).ToList();
2013-07-05 03:56:27 +00:00
var tempTableName = tableName + "_temp";
2013-07-05 03:56:27 +00:00
_sqLiteMigrationHelper.CreateTable(tempTableName, newColumns);
2013-07-05 03:56:27 +00:00
_sqLiteMigrationHelper.CopyData(tableName, tempTableName, newColumns);
2013-07-05 03:56:27 +00:00
_sqLiteMigrationHelper.DropTable(tableName);
2013-07-05 03:56:27 +00:00
_sqLiteMigrationHelper.RenameTable(tempTableName, tableName);
2013-07-05 03:56:27 +00:00
transaction.Commit();
}
}
}
}