Starting to add ALTER COLUMN to SQLite.

This commit is contained in:
Keivan Beigi 2013-07-03 18:01:49 -07:00 committed by kay.one
parent 50ee2ee357
commit 1c5a74df98
6 changed files with 151 additions and 4 deletions

View File

@ -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<IDatabase>().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");
}
}
}

View File

@ -97,7 +97,7 @@ namespace NzbDrone.Core.Test.Framework
var factory = new DbFactory(new MigrationController(new MigrationLogger(TestLogger)), Mocker.GetMock<IAppDirectoryInfo>().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>(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<IMessageAggregator>().Object;
_dbConnection = dbConnection;

View File

@ -124,6 +124,7 @@
<Compile Include="Datastore\PagingSpecExtenstionsTests\ToSortDirectionFixture.cs" />
<Compile Include="Datastore\PagingSpecExtenstionsTests\PagingOffsetFixture.cs" />
<Compile Include="Datastore\ReflectionStrategyFixture\Benchmarks.cs" />
<Compile Include="Datastore\SQLiteAlterFixture.cs" />
<Compile Include="DecisionEngineTests\NotRestrictedNzbSpecificationFixture.cs" />
<Compile Include="Download\DownloadApprovedReportsTests\DownloadApprovedFixture.cs" />
<Compile Include="Download\DownloadApprovedReportsTests\GetQualifiedReportsFixture.cs" />

View File

@ -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");
}
}*/
}

View File

@ -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(@"[\""\[](?<name>\w+)[\""\]]\s(?<schema>[\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<String, SQLiteColumn> GetColumns(string tableName)
{
var originalSql = GetOriginalSql(tableName);
var matches = SchemaRegex.Matches(originalSql);
return matches.Cast<Match>().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<string, SQLiteColumn>.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);
}
}
}

View File

@ -212,6 +212,7 @@
<Compile Include="Datastore\Migration\005_added_eventtype_to_history.cs" />
<Compile Include="Datastore\Migration\006_add_index_to_log_time.cs" />
<Compile Include="Datastore\Migration\007_add_renameEpisodes_to_naming.cs" />
<Compile Include="Datastore\Migration\007_remove_backlog.cs" />
<Compile Include="Datastore\Migration\Framework\MigrationController.cs" />
<Compile Include="Datastore\Migration\Framework\MigrationExtension.cs" />
<Compile Include="Datastore\Migration\Framework\MigrationOptions.cs" />
@ -219,6 +220,7 @@
<Compile Include="Datastore\Migration\001_initialSetup.cs" />
<Compile Include="Datastore\Migration\Framework\NzbDroneMigrationBase.cs" />
<Compile Include="Datastore\MigrationType.cs" />
<Compile Include="Datastore\Migration\Framework\SqliteAlter.cs" />
<Compile Include="Datastore\ModelBase.cs" />
<Compile Include="Datastore\BasicRepository.cs" />
<Compile Include="Datastore\PagingSpec.cs" />
@ -594,4 +596,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>