diff --git a/NzbDrone.Core.Test/Datastore/SQLiteAlterFixture.cs b/NzbDrone.Core.Test/Datastore/SQLiteAlterFixture.cs new file mode 100644 index 000000000..a8c665ab9 --- /dev/null +++ b/NzbDrone.Core.Test/Datastore/SQLiteAlterFixture.cs @@ -0,0 +1,49 @@ +using System.Linq; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Core.Datastore; +using NzbDrone.Core.Datastore.Migration.Framework; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.Datastore +{ + [TestFixture] + public class SQLiteAlterFixture : DbTest + { + private SQLiteAlter Subject; + + [SetUp] + public void SetUp() + { + var connection = Mocker.Resolve().DataMapper.ConnectionString; + Subject = new SQLiteAlter(connection); + } + + + + [Test] + public void should_parse_existing_columns() + { + var columns = Subject.GetColumns("Series"); + + columns.Should().NotBeEmpty(); + + columns.Values.Should().NotContain(c => string.IsNullOrWhiteSpace(c.Name)); + columns.Values.Should().NotContain(c => string.IsNullOrWhiteSpace(c.Schema)); + } + + [Test] + public void should_create_table_from_column_list() + { + var columns = Subject.GetColumns("Series"); + columns.Remove("Title"); + + Subject.CreateTable("Series_New", columns.Values); + + var newColumns = Subject.GetColumns("Series_New"); + + newColumns.Values.Should().HaveSameCount(columns.Values); + newColumns.Should().NotContainKey("Title"); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core.Test/Framework/DbTest.cs b/NzbDrone.Core.Test/Framework/DbTest.cs index 896e7829d..d01a21836 100644 --- a/NzbDrone.Core.Test/Framework/DbTest.cs +++ b/NzbDrone.Core.Test/Framework/DbTest.cs @@ -97,7 +97,7 @@ namespace NzbDrone.Core.Test.Framework var factory = new DbFactory(new MigrationController(new MigrationLogger(TestLogger)), Mocker.GetMock().Object); _database = factory.Create(MigrationType); - _db = new TestTestDatabase(_database); + _db = new TestDatabase(_database); Mocker.SetConstant(_database); } @@ -140,12 +140,12 @@ namespace NzbDrone.Core.Test.Framework void Delete(T childModel) where T : ModelBase, new(); } - public class TestTestDatabase : ITestDatabase + public class TestDatabase : ITestDatabase { private readonly IDatabase _dbConnection; private IMessageAggregator _messageAggregator; - public TestTestDatabase(IDatabase dbConnection) + public TestDatabase(IDatabase dbConnection) { _messageAggregator = new Mock().Object; _dbConnection = dbConnection; diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 1c6aebb1d..fa00cec5a 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -124,6 +124,7 @@ + diff --git a/NzbDrone.Core/Datastore/Migration/007_remove_backlog.cs b/NzbDrone.Core/Datastore/Migration/007_remove_backlog.cs new file mode 100644 index 000000000..f768590be --- /dev/null +++ b/NzbDrone.Core/Datastore/Migration/007_remove_backlog.cs @@ -0,0 +1,26 @@ +using FluentMigrator; +using NzbDrone.Core.Datastore.Migration.Framework; + +namespace NzbDrone.Core.Datastore.Migration +{ +/* [Tags("")] + [Migration(7)] + public class remove_backlog : NzbDroneMigrationBase + { + protected override void MainDbUpgrade() + { + var newSeriesTable = "CREATE TABLE [Series_new] ([Id] integer NOT NULL PRIMARY KEY AUTOINCREMENT, [TvdbId] integer NOT NULL, " + + "[TvRageId] integer NOT NULL, [ImdbId] text NOT NULL, [Title] text NOT NULL, [TitleSlug] text NOT NULL, " + + "[CleanTitle] text NOT NULL, [Status] integer NOT NULL, [Overview] text, [AirTime] text, " + + "[Images] text NOT NULL, [Path] text NOT NULL, [Monitored] integer NOT NULL, [QualityProfileId] integer NOT NULL, " + + "[SeasonFolder] integer NOT NULL, [LastInfoSync] datetime, [LastDiskSync] datetime, [Runtime] integer NOT NULL, " + + "[SeriesType] integer NOT NULL, [Network] text, [CustomStartDate] datetime, " + + "[UseSceneNumbering] integer NOT NULL, [FirstAired] datetime)"; + + Execute.Sql(newSeriesTable); + + + Execute.Sql("INSERT INTO Series_new SELECT * FROM Series"); + } + }*/ +} diff --git a/NzbDrone.Core/Datastore/Migration/Framework/SqliteAlter.cs b/NzbDrone.Core/Datastore/Migration/Framework/SqliteAlter.cs new file mode 100644 index 000000000..943409916 --- /dev/null +++ b/NzbDrone.Core/Datastore/Migration/Framework/SqliteAlter.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Data.SQLite; +using System.Linq; +using System.Text.RegularExpressions; +using FluentMigrator.Builders.Execute; + +namespace NzbDrone.Core.Datastore.Migration.Framework +{ + public class SQLiteAlter + { + private readonly SQLiteConnection _connection; + + private static readonly Regex SchemaRegex = new Regex(@"[\""\[](?\w+)[\""\]]\s(?[\w-\s]+)", + RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline); + + public SQLiteAlter(string connectionString) + { + _connection = new SQLiteConnection(connectionString); + _connection.Open(); + } + + private string GetOriginalSql(string tableName) + { + var command = + new SQLiteCommand(string.Format("SELECT sql FROM sqlite_master WHERE type='table' AND name ='{0}'", + tableName)); + + command.Connection = _connection; + return (string)command.ExecuteScalar(); + } + + public Dictionary GetColumns(string tableName) + { + var originalSql = GetOriginalSql(tableName); + + var matches = SchemaRegex.Matches(originalSql); + + return matches.Cast().ToDictionary( + match => match.Groups["name"].Value.Trim(), + match => new SQLiteColumn + { + Name = match.Groups["name"].Value.Trim(), + Schema = match.Groups["schema"].Value.Trim() + }); + } + + public void CreateTable(string tableName, Dictionary.ValueCollection values) + { + var columns = String.Join(",", values.Select(c => c.ToString())); + + var command = new SQLiteCommand(string.Format("CREATE TABLE [{0}] ({1})", tableName, columns)); + command.Connection = _connection; + + command.ExecuteNonQuery(); + } + } + + public class SQLiteColumn + { + public string Name { get; set; } + public string Schema { get; set; } + + public override string ToString() + { + return string.Format("[{0}] {1}", Name, Schema); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 095c3be64..bc1056aa1 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -212,6 +212,7 @@ + @@ -219,6 +220,7 @@ + @@ -594,4 +596,4 @@ --> - \ No newline at end of file +