Fixed: All migrations are now transactional and will rollback if failed

This commit is contained in:
Taloth Saldono
2014-08-25 23:29:37 +02:00
committed by Mark McDowall
parent 2be35dfc37
commit b9623957fd
32 changed files with 842 additions and 652 deletions
@@ -1,142 +0,0 @@
using System.Collections.Generic;
using FizzWare.NBuilder;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Datastore.Migration.Framework;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv;
using System.Linq;
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.Datastore.SQLiteMigrationHelperTests
{
[TestFixture]
public class AlterFixture : DbTest
{
private SqLiteMigrationHelper _subject;
[SetUp]
public void SetUp()
{
_subject = Mocker.Resolve<SqLiteMigrationHelper>();
}
[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, new List<SQLiteIndex>());
var newColumns = _subject.GetColumns("Series_New");
newColumns.Values.Should().HaveSameCount(columns.Values);
newColumns.Should().NotContainKey("Title");
}
[Test]
public void should_be_able_to_transfer_empty_tables()
{
var columns = _subject.GetColumns("Series");
var indexes = _subject.GetIndexes("Series");
columns.Remove("Title");
_subject.CreateTable("Series_New", columns.Values, indexes);
_subject.CopyData("Series", "Series_New", columns.Values);
}
[Test]
public void should_transfer_table_with_data()
{
var originalEpisodes = Builder<Episode>.CreateListOfSize(10).BuildListOfNew();
Mocker.Resolve<EpisodeRepository>().InsertMany(originalEpisodes);
var columns = _subject.GetColumns("Episodes");
var indexes = _subject.GetIndexes("Episodes");
columns.Remove("Title");
_subject.CreateTable("Episodes_New", columns.Values, indexes);
_subject.CopyData("Episodes", "Episodes_New", columns.Values);
_subject.GetRowCount("Episodes_New").Should().Be(originalEpisodes.Count);
}
[Test]
public void should_read_existing_indexes()
{
var indexes = _subject.GetIndexes("QualityDefinitions");
indexes.Should().NotBeEmpty();
indexes.Should().OnlyContain(c => c != null);
indexes.Should().OnlyContain(c => !string.IsNullOrWhiteSpace(c.Column));
indexes.Should().OnlyContain(c => c.Table == "QualityDefinitions");
indexes.Should().OnlyContain(c => c.Unique);
}
[Test]
public void should_add_indexes_when_creating_new_table()
{
var columns = _subject.GetColumns("QualityDefinitions");
var indexes = _subject.GetIndexes("QualityDefinitions");
_subject.CreateTable("QualityDefinitionsB", columns.Values, indexes);
var newIndexes = _subject.GetIndexes("QualityDefinitionsB");
newIndexes.Should().HaveSameCount(indexes);
newIndexes.Select(c=>c.Column).Should().BeEquivalentTo(indexes.Select(c=>c.Column));
}
[Test]
public void should_be_able_to_create_table_with_new_indexes()
{
var columns = _subject.GetColumns("Series");
columns.Remove("Title");
_subject.CreateTable("Series_New", columns.Values, new List<SQLiteIndex>{new SQLiteIndex{Column = "AirTime", Table = "Series_New", Unique = true}});
var newColumns = _subject.GetColumns("Series_New");
var newIndexes = _subject.GetIndexes("Series_New");
newColumns.Values.Should().HaveSameCount(columns.Values);
newIndexes.Should().Contain(i=>i.Column == "AirTime");
}
[Test]
public void should_create_indexes_with_the_same_uniqueness()
{
var columns = _subject.GetColumns("Series");
var indexes = _subject.GetIndexes("Series");
var tempIndexes = indexes.JsonClone();
tempIndexes[0].Unique = false;
tempIndexes[1].Unique = true;
_subject.CreateTable("Series_New", columns.Values, tempIndexes);
var newIndexes = _subject.GetIndexes("Series_New");
newIndexes.Should().HaveSameCount(tempIndexes);
newIndexes.ShouldAllBeEquivalentTo(tempIndexes, options => options.Excluding(o => o.IndexName).Excluding(o => o.Table));
}
}
}
@@ -1,41 +0,0 @@
using System.Linq;
using FizzWare.NBuilder;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Datastore.Migration.Framework;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Test.Datastore.SQLiteMigrationHelperTests
{
[TestFixture]
public class DuplicateFixture : DbTest
{
private SqLiteMigrationHelper _subject;
[SetUp]
public void SetUp()
{
_subject = Mocker.Resolve<SqLiteMigrationHelper>();
}
[Test]
public void get_duplicates()
{
var series = Builder<Series>.CreateListOfSize(10)
.Random(3)
.With(c => c.ProfileId = 100)
.BuildListOfNew();
Db.InsertMany(series);
var duplicates = _subject.GetDuplicates<int>("series", "ProfileId").ToList();
duplicates.Should().HaveCount(1);
duplicates.First().Should().HaveCount(3);
}
}
}
@@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Datastore.Migration.Framework;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.Datastore.SqliteSchemaDumperTests
{
[TestFixture]
public class SqliteSchemaDumperFixture
{
public SqliteSchemaDumper Subject { get; private set; }
[SetUp]
public void Setup()
{
Subject = new SqliteSchemaDumper(null, null);
}
[TestCase(@"CREATE TABLE TestTable (MyId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)", "TestTable", "MyId")]
[TestCase(@"CREATE TABLE ""TestTable"" (""MyId"" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)", "TestTable", "MyId")]
[TestCase(@"CREATE TABLE [TestTable] ([MyId] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)", "TestTable", "MyId")]
[TestCase(@"CREATE TABLE `TestTable` (`MyId` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)", "TestTable", "MyId")]
[TestCase(@"CREATE TABLE ""Test """"Table"" (""My""""Id"" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)", "Test \"Table", "My\"Id")]
[TestCase(@"CREATE TABLE [Test Table] ([My Id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)", "Test Table", "My Id")]
[TestCase(@" CREATE TABLE `Test ``Table` ( `My`` Id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT ) ", "Test `Table", "My` Id")]
public void should_parse_table_language_flavors(String sql, String tableName, String columnName)
{
var result = Subject.ReadTableSchema(sql);
result.Name.Should().Be(tableName);
result.Columns.Count.Should().Be(1);
result.Columns.First().Name.Should().Be(columnName);
}
[TestCase(@"CREATE INDEX TestIndex ON TestTable (MyId)", "TestIndex", "TestTable", "MyId")]
[TestCase(@"CREATE INDEX ""TestIndex"" ON ""TestTable"" (""MyId"" ASC)", "TestIndex", "TestTable", "MyId")]
[TestCase(@"CREATE INDEX [TestIndex] ON ""TestTable"" ([MyId] DESC)", "TestIndex", "TestTable", "MyId")]
[TestCase(@"CREATE INDEX `TestIndex` ON `TestTable` (`MyId` COLLATE abc ASC)", "TestIndex", "TestTable", "MyId")]
[TestCase(@"CREATE INDEX ""Test """"Index"" ON ""TestTable"" (""My""""Id"" ASC)", "Test \"Index", "TestTable", "My\"Id")]
[TestCase(@"CREATE INDEX [Test Index] ON [TestTable] ([My Id]) ", "Test Index", "TestTable", "My Id")]
[TestCase(@" CREATE INDEX `Test ``Index` ON ""TestTable"" ( `My`` Id` ASC) ", "Test `Index", "TestTable", "My` Id")]
public void should_parse_index_language_flavors(String sql, String indexName, String tableName, String columnName)
{
var result = Subject.ReadIndexSchema(sql);
result.Name.Should().Be(indexName);
result.TableName.Should().Be(tableName);
result.Columns.Count.Should().Be(1);
result.Columns.First().Name.Should().Be(columnName);
}
[TestCase(@"CREATE TABLE TestTable (MyId)")]
[TestCase(@"CREATE TABLE TestTable (MyId NOT NULL PRIMARY KEY AUTOINCREMENT)")]
[TestCase("CREATE TABLE TestTable\r\n(\t`MyId`\t NOT NULL PRIMARY KEY AUTOINCREMENT\n)")]
public void should_parse_column_attributes(String sql)
{
var result = Subject.ReadTableSchema(sql);
result.Name.Should().Be("TestTable");
result.Columns.Count.Should().Be(1);
result.Columns.First().Name.Should().Be("MyId");
result.Columns.First().Type.Should().BeNull();
}
[Test]
public void should_ignore_unknown_symbols()
{
var result = Subject.ReadTableSchema("CREATE TABLE TestTable (MyId INTEGER DEFAULT 10 CHECK (Some weir +1e3 expression), CONSTRAINT NULL, MyCol INTEGER)");
result.Name.Should().Be("TestTable");
result.Columns.Count.Should().Be(2);
result.Columns.First().Name.Should().Be("MyId");
result.Columns.First().Type.Should().Be(DbType.Int64);
result.Columns.Last().Name.Should().Be("MyCol");
result.Columns.Last().Type.Should().Be(DbType.Int64);
}
}
}
@@ -92,8 +92,6 @@ namespace NzbDrone.Core.Test.Framework
Mocker.SetConstant<IAnnouncer>(Mocker.Resolve<MigrationLogger>());
Mocker.SetConstant<IConnectionStringFactory>(Mocker.Resolve<ConnectionStringFactory>());
Mocker.SetConstant<ISqLiteMigrationHelper>(Mocker.Resolve<SqLiteMigrationHelper>());
Mocker.SetConstant<ISQLiteAlter>(Mocker.Resolve<SQLiteAlter>());
Mocker.SetConstant<IMigrationController>(Mocker.Resolve<MigrationController>());
MapRepository.Instance.EnableTraceLogging = true;
@@ -117,8 +117,7 @@
<Compile Include="Datastore\PagingSpecExtensionsTests\PagingOffsetFixture.cs" />
<Compile Include="Datastore\PagingSpecExtensionsTests\ToSortDirectionFixture.cs" />
<Compile Include="Datastore\ReflectionStrategyFixture\Benchmarks.cs" />
<Compile Include="Datastore\SQLiteMigrationHelperTests\AlterFixture.cs" />
<Compile Include="Datastore\SQLiteMigrationHelperTests\DuplicateFixture.cs" />
<Compile Include="Datastore\SqliteSchemaDumperTests\SqliteSchemaDumperFixture.cs" />
<Compile Include="DecisionEngineTests\AcceptableSizeSpecificationFixture.cs" />
<Compile Include="DecisionEngineTests\CutoffSpecificationFixture.cs" />
<Compile Include="DecisionEngineTests\DownloadDecisionMakerFixture.cs" />