1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-10-30 15:32:31 +01:00

Marr is almost working.

This commit is contained in:
kay.one 2013-03-24 23:13:53 -07:00
parent 971053f8a2
commit ea4f0dbe5f
18 changed files with 300 additions and 126 deletions

View File

@ -1,44 +1,29 @@
using System;
using System.Data;
using System.Linq;
using FizzWare.NBuilder;
using FluentAssertions;
using Marr.Data.Mapping;
using NUnit.Framework;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Jobs;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.Datastore
{
public class BasicType : ModelBase
{
public string Name { get; set; }
public string Tilte { get; set; }
public string Address { get; set; }
}
[TestFixture]
public class
BasicRepositoryFixture : DbTest<BasicRepository<BasicType>, BasicType>
public class
BasicRepositoryFixture : DbTest<BasicRepository<JobDefinition>, JobDefinition>
{
private BasicType _basicType;
private JobDefinition _basicType;
[SetUp]
public void Setup()
{
_basicType = Builder<BasicType>
_basicType = Builder<JobDefinition>
.CreateNew()
.With(c => c.Id = 0)
.Build();
var mapping = new FluentMappings(true);
mapping.Entity<BasicType>()
.Columns.AutoMapSimpleTypeProperties()
.For(c => c.Id).SetAutoIncrement()
.SetPrimaryKey();
}
[Test]

View File

@ -6,19 +6,20 @@ using FizzWare.NBuilder;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Jobs;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.Datastore
{
[TestFixture]
public class ObjectDatabaseFixture : DbTest<BasicRepository<BasicType>, BasicType>
public class ObjectDatabaseFixture : DbTest<BasicRepository<JobDefinition>, JobDefinition>
{
private BasicType _sampleType;
private JobDefinition _sampleType;
[SetUp]
public void SetUp()
{
_sampleType = Builder<BasicType>
_sampleType = Builder<JobDefinition>
.CreateNew()
.With(s => s.Id = 0)
.Build();
@ -29,7 +30,7 @@ namespace NzbDrone.Core.Test.Datastore
public void should_be_able_to_write_to_database()
{
Subject.Insert(_sampleType);
Db.All<BasicType>().Should().HaveCount(1);
Db.All<JobDefinition>().Should().HaveCount(1);
}
[Test]
@ -49,7 +50,7 @@ namespace NzbDrone.Core.Test.Datastore
[Test]
public void should_be_able_to_store_empty_list()
{
var series = new List<BasicType>();
var series = new List<JobDefinition>();
Subject.InsertMany(series);
}
@ -68,7 +69,7 @@ namespace NzbDrone.Core.Test.Datastore
_sampleType.Id = 0;
Subject.Insert(_sampleType);
Db.All<BasicType>().Should().HaveCount(1);
Db.All<JobDefinition>().Should().HaveCount(1);
_sampleType.Id.Should().Be(1);
}
@ -80,7 +81,7 @@ namespace NzbDrone.Core.Test.Datastore
{
_sampleType.Id = 0;
Subject.Insert(_sampleType);
var item = Db.All<BasicType>();
var item = Db.All<JobDefinition>();
item.Should().HaveCount(1);
item.First().Id.Should().NotBe(0);
@ -92,7 +93,7 @@ namespace NzbDrone.Core.Test.Datastore
public void should_be_able_to_find_object_by_id()
{
Subject.Insert(_sampleType);
var item = Db.All<BasicType>().Single(c => c.Id == _sampleType.Id);
var item = Db.All<JobDefinition>().Single(c => c.Id == _sampleType.Id);
item.Id.Should().NotBe(0);
item.Id.Should().Be(_sampleType.Id);
@ -102,25 +103,25 @@ namespace NzbDrone.Core.Test.Datastore
[Test]
public void update_field_should_only_update_that_filed()
{
var childModel = new BasicType
var childModel = new JobDefinition
{
Address = "Address",
Type = "Address",
Name = "Name",
Tilte = "Title"
Interval = 12
};
Subject.Insert(childModel);
childModel.Address = "A";
childModel.Type = "A";
childModel.Name = "B";
childModel.Tilte = "C";
childModel.Interval = 0;
Subject.UpdateFields(childModel, t => t.Name);
Db.All<BasicType>().Single().Address.Should().Be("Address");
Db.All<BasicType>().Single().Name.Should().Be("B");
Db.All<BasicType>().Single().Tilte.Should().Be("Title");
Db.All<JobDefinition>().Single().Type.Should().Be("Address");
Db.All<JobDefinition>().Single().Name.Should().Be("B");
Db.All<JobDefinition>().Single().Interval.Should().Be(12);
}

View File

@ -7,6 +7,7 @@ using Marr.Data;
using NUnit.Framework;
using NzbDrone.Common;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Datastore.Migration.Framework;
namespace NzbDrone.Core.Test.Framework
{
@ -85,7 +86,7 @@ namespace NzbDrone.Core.Test.Framework
MapRepository.Instance.EnableTraceLogging = true;
var factory = new DbFactory();
var factory = new DbFactory(new MigrationController(new NlogAnnouncer()));
_database = factory.Create(_dbName);
_db = new TestTestDatabase(_database);
Mocker.SetConstant(_database);

View File

@ -79,6 +79,11 @@
</Reference>
<Reference Include="Marr.Data">
<HintPath>..\packages\MarrDataMapper.3.17.4747.34302\lib\Marr.Data.dll</HintPath>
<Reference Include="FluentMigrator">
<HintPath>..\packages\FluentMigrator.1.0.6.0\lib\40\FluentMigrator.dll</HintPath>
</Reference>
<Reference Include="FluentMigrator.Runner">
<HintPath>..\packages\FluentMigrator.1.0.6.0\tools\FluentMigrator.Runner.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.Practices.ServiceLocation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">

View File

@ -4,6 +4,7 @@
<package id="AutoMoq" version="1.6.1" targetFramework="net40" />
<package id="CommonServiceLocator" version="1.0" targetFramework="net40" />
<package id="FluentAssertions" version="2.0.0.1" targetFramework="net40" />
<package id="FluentMigrator" version="1.0.6.0" targetFramework="net40" />
<package id="Moq" version="4.0.10827" />
<package id="NBuilder" version="3.0.1.1" />
<package id="NCrunch.Framework" version="1.43.0.23" targetFramework="net40" />

View File

@ -0,0 +1,52 @@
using System;
using Marr.Data.Converters;
using Marr.Data.Mapping;
namespace NzbDrone.Core.Datastore.Converters
{
public class BooleanIntConverter : IConverter
{
public object FromDB(ColumnMap map, object dbValue)
{
if (dbValue == DBNull.Value)
{
return DBNull.Value;
}
var val = (Int64)dbValue;
switch (val)
{
case 1:
return true;
case 0:
return false;
default:
throw new ConversionException(string.Format("The BooleanCharConverter could not convert the value '{0}' to a Boolean.", dbValue));
}
}
public object ToDB(object clrValue)
{
var val = (Nullable<bool>)clrValue;
switch (val)
{
case true:
return 1;
case false:
return 0;
default:
return DBNull.Value;
}
}
public Type DbType
{
get
{
return typeof(int);
}
}
}
}

View File

@ -0,0 +1,31 @@
using System;
using Marr.Data.Converters;
using Marr.Data.Mapping;
namespace NzbDrone.Core.Datastore.Converters
{
public class Int32Converter : IConverter
{
public object FromDB(ColumnMap map, object dbValue)
{
if (dbValue == DBNull.Value)
{
return DBNull.Value;
}
if (dbValue is Int32)
{
return dbValue;
}
return Convert.ToInt32(dbValue);
}
public object ToDB(object clrValue)
{
return clrValue;
}
public Type DbType { get; private set; }
}
}

View File

@ -2,35 +2,35 @@
using System.Data;
using Marr.Data;
using Mono.Data.Sqlite;
using NzbDrone.Core.Datastore.Migration.Framework;
namespace NzbDrone.Core.Datastore
{
public interface IDbFactory
{
IDatabase Create(string dbPath = null);
IDatabase Create(string dbPath, MigrationType migrationType = MigrationType.Main);
}
public class DbFactory : IDbFactory
{
private const string MemoryConnectionString = "Data Source=:memory:;Version=3;New=True;";
private readonly IMigrationController _migrationController;
public IDatabase Create(string dbPath = null)
public DbFactory(IMigrationController migrationController)
{
var connectionString = MemoryConnectionString;
TableMapping.Map();
_migrationController = migrationController;
}
if (!string.IsNullOrWhiteSpace(dbPath))
{
connectionString = GetConnectionString(dbPath);
}
public IDatabase Create(string dbPath, MigrationType migrationType = MigrationType.Main)
{
var connectionString = GetConnectionString(dbPath);
MigrationHelper.MigrateToLatest(connectionString, MigrationType.Main);
_migrationController.MigrateToLatest(connectionString, migrationType);
var dataMapper = new DataMapper(SqliteFactory.Instance, connectionString);
return new Database(dataMapper);
}
private string GetConnectionString(string dbPath)
{
return String.Format("Data Source={0};Version=3;", dbPath);

View File

@ -0,0 +1,39 @@
using System.Reflection;
using FluentMigrator.Runner;
using FluentMigrator.Runner.Initialization;
using NzbDrone.Core.Datastore.Migration.Sqlite;
namespace NzbDrone.Core.Datastore.Migration.Framework
{
public interface IMigrationController
{
void MigrateToLatest(string connectionString, MigrationType migrationType);
}
public class MigrationController : IMigrationController
{
private readonly IAnnouncer _announcer;
public MigrationController(IAnnouncer announcer)
{
_announcer = announcer;
}
public void MigrateToLatest(string connectionString, MigrationType migrationType)
{
var assembly = Assembly.GetExecutingAssembly();
var migrationContext = new RunnerContext(_announcer)
{
Namespace = "NzbDrone.Core.Datastore.Migration",
ApplicationContext = migrationType
};
var options = new MigrationOptions { PreviewOnly = false, Timeout = 60 };
var factory = new MonoSqliteProcessorFactory();
var processor = factory.Create(connectionString, _announcer, options);
var runner = new MigrationRunner(assembly, migrationContext, processor);
runner.MigrateUp(true);
}
}
}

View File

@ -0,0 +1,10 @@
using FluentMigrator;
namespace NzbDrone.Core.Datastore.Migration.Framework
{
public class MigrationOptions : IMigrationProcessorOptions
{
public bool PreviewOnly { get; set; }
public int Timeout { get; set; }
}
}

View File

@ -1,11 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentMigrator.Runner.Announcers;
using FluentMigrator.Runner.Announcers;
using NLog;
namespace NzbDrone.Core.Datastore
namespace NzbDrone.Core.Datastore.Migration.Framework
{
public class NlogAnnouncer : Announcer
{

View File

@ -1,13 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentMigrator;
using NzbDrone.Common;
namespace NzbDrone.Core.Datastore.Migrations
namespace NzbDrone.Core.Datastore.Migration.Framework
{
public abstract class NzbDroneMigration : Migration
public abstract class NzbDroneMigrationBase : FluentMigrator.Migration
{
protected virtual void MainDbUpgrade()
{
@ -19,11 +14,11 @@ namespace NzbDrone.Core.Datastore.Migrations
public override void Up()
{
if ((MigrationType)this.ApplicationContext == MigrationType.Main)
if ((MigrationType)ApplicationContext == MigrationType.Main)
{
MainDbUpgrade();
}
else if ((MigrationType)this.ApplicationContext == MigrationType.Log)
else if ((MigrationType)ApplicationContext == MigrationType.Log)
{
LogDbUpgrade();
}

View File

@ -1,14 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentMigrator;
using FluentMigrator;
using NzbDrone.Core.Datastore.Migration.Framework;
namespace NzbDrone.Core.Datastore.Migrations
namespace NzbDrone.Core.Datastore.Migration
{
[Tags("")]
[Migration(20130324)]
public class Migration20130324 : NzbDroneMigration
public class Migration20130324 : NzbDroneMigrationBase
{
protected override void MainDbUpgrade()
{
@ -80,12 +77,6 @@ namespace NzbDrone.Core.Datastore.Migrations
.WithColumn("LastExecution").AsDateTime().NotNullable()
.WithColumn("Success").AsBoolean().NotNullable();
Create.Table("MetadataDefinitions")
.WithColumn("Id").AsInt32().PrimaryKey().Identity()
.WithColumn("Enable").AsBoolean().NotNullable()
.WithColumn("Type").AsString().NotNullable()
.WithColumn("Name").AsString().NotNullable();
Create.Table("NewznabDefinitions")
.WithColumn("Id").AsInt32().PrimaryKey().Identity()
.WithColumn("Enable").AsBoolean().NotNullable()

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentMigrator.Runner.Processors;
namespace NzbDrone.Core.Datastore.Migration.Sqlite
{
public class MonoSqliteDbFactory : ReflectionBasedDbFactory
{
public MonoSqliteDbFactory()
: base("Mono.Data.Sqlite", "Mono.Data.Sqlite.SqliteFactory")
{
}
}
}

View File

@ -0,0 +1,18 @@
using FluentMigrator;
using FluentMigrator.Runner;
using FluentMigrator.Runner.Generators.SQLite;
using FluentMigrator.Runner.Processors;
using FluentMigrator.Runner.Processors.Sqlite;
namespace NzbDrone.Core.Datastore.Migration.Sqlite
{
public class MonoSqliteProcessorFactory : MigrationProcessorFactory
{
public override IMigrationProcessor Create(string connectionString, IAnnouncer announcer, IMigrationProcessorOptions options)
{
var factory = new MonoSqliteDbFactory();
var connection = factory.CreateConnection(connectionString);
return new SqliteProcessor(connection, new SqliteGenerator(), announcer, options, factory);
}
}
}

View File

@ -1,38 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using FluentMigrator;
using FluentMigrator.Runner;
using FluentMigrator.Runner.Initialization;
namespace NzbDrone.Core.Datastore
{
public static class MigrationHelper
{
public static void MigrateToLatest(string connectionString, MigrationType migrationType)
{
var announcer = new NlogAnnouncer();
var assembly = Assembly.GetExecutingAssembly();
var migrationContext = new RunnerContext(announcer)
{
Namespace = "NzbDrone.Core.Datastore.Migrations",
ApplicationContext = migrationType
};
var options = new MigrationOptions { PreviewOnly = false, Timeout = 60 };
var factory = new FluentMigrator.Runner.Processors.Sqlite.SqliteProcessorFactory();
var processor = factory.Create(connectionString, announcer, options);
var runner = new MigrationRunner(assembly, migrationContext, processor);
runner.MigrateUp(true);
}
}
public class MigrationOptions : IMigrationProcessorOptions
{
public bool PreviewOnly { get; set; }
public int Timeout { get; set; }
}
}

View File

@ -0,0 +1,71 @@
using System;
using Marr.Data;
using Marr.Data.Mapping;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Datastore.Converters;
using NzbDrone.Core.ExternalNotification;
using NzbDrone.Core.Indexers;
using NzbDrone.Core.Instrumentation;
using NzbDrone.Core.Jobs;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.Qualities;
using NzbDrone.Core.ReferenceData;
using NzbDrone.Core.RootFolders;
using NzbDrone.Core.Tv;
using BooleanIntConverter = NzbDrone.Core.Datastore.Converters.BooleanIntConverter;
namespace NzbDrone.Core.Datastore
{
public static class TableMapping
{
private static readonly FluentMappings Mapper = new FluentMappings(true);
public static void Map()
{
RegisterMappers();
Mapper.Entity<Config>().RegisterModel("Config");
Mapper.Entity<RootFolder>().RegisterModel("RootFolders");
Mapper.Entity<Indexer>().RegisterModel("IndexerDefinitions");
Mapper.Entity<NewznabDefinition>().RegisterModel("NewznabDefinitions");
Mapper.Entity<JobDefinition>().RegisterModel("JobDefinitions");
Mapper.Entity<ExternalNotificationDefinition>().RegisterModel("ExternalNotificationDefinitions");
Mapper.Entity<SceneMapping>().RegisterModel("SceneMappings");
Mapper.Entity<History.History>().RegisterModel("History");
Mapper.Entity<Series>().RegisterModel("Series");
Mapper.Entity<Season>().RegisterModel("Seasons");
Mapper.Entity<Episode>().RegisterModel("Episodes");
Mapper.Entity<EpisodeFile>().RegisterModel("EpisodeFiles");
Mapper.Entity<QualityProfile>().RegisterModel("QualityProfiles");
Mapper.Entity<QualitySize>().RegisterModel("QualitySizes");
Mapper.Entity<Log>().RegisterModel("Logs");
}
private static void RegisterMappers()
{
MapRepository.Instance.RegisterTypeConverter(typeof(Int32), new Int32Converter());
MapRepository.Instance.RegisterTypeConverter(typeof(Boolean), new BooleanIntConverter());
}
private static void RegisterModel<T>(this FluentMappings.MappingsFluentEntity<T> mapBuilder, string tableName) where T : ModelBase
{
mapBuilder.Table.MapTable(tableName)
.Columns
.AutoMapSimpleTypeProperties()
.For(c => c.Id)
.SetPrimaryKey()
.SetReturnValue()
.SetAutoIncrement();
}
}
}

View File

@ -179,14 +179,6 @@
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Data.SQLite, Version=1.0.84.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=x86">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\System.Data.SQLite.x64.1.0.84.0\lib\net40\System.Data.SQLite.dll</HintPath>
</Reference>
<Reference Include="System.Data.SQLite.Linq, Version=1.0.84.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\System.Data.SQLite.x64.1.0.84.0\lib\net40\System.Data.SQLite.Linq.dll</HintPath>
</Reference>
<Reference Include="System.Drawing" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Web" />
@ -206,15 +198,21 @@
<Compile Include="Configuration\IConfigService.cs" />
<Compile Include="Constants.cs" />
<Compile Include="ContainerExtensions.cs" />
<Compile Include="Datastore\Converters\BooleanIntConverter.cs" />
<Compile Include="Datastore\Converters\Int32Converter.cs" />
<Compile Include="Datastore\Database.cs" />
<Compile Include="Datastore\DbFactory.cs" />
<Compile Include="Datastore\MigrationHelper.cs" />
<Compile Include="Datastore\Migrations\Migration20130324.cs" />
<Compile Include="Datastore\Migrations\NzbDroneMigration.cs" />
<Compile Include="Datastore\Migration\Framework\MigrationController.cs" />
<Compile Include="Datastore\Migration\Framework\MigrationOptions.cs" />
<Compile Include="Datastore\Migration\Framework\NlogAnnouncer.cs" />
<Compile Include="Datastore\Migration\Migration20130324.cs" />
<Compile Include="Datastore\Migration\Framework\NzbDroneMigrationBase.cs" />
<Compile Include="Datastore\MigrationType.cs" />
<Compile Include="Datastore\Migration\Sqlite\MonoSqliteDbFactory.cs" />
<Compile Include="Datastore\Migration\Sqlite\MonoSqliteProcessorFactory.cs" />
<Compile Include="Datastore\ModelBase.cs" />
<Compile Include="Datastore\BasicRepository.cs" />
<Compile Include="Datastore\NlogAnnouncer.cs" />
<Compile Include="Datastore\TableMapping.cs" />
<Compile Include="DecisionEngine\DownloadDecision.cs" />
<Compile Include="DecisionEngine\IFetchableSpecification.cs" />
<Compile Include="DecisionEngine\Specifications\AcceptableSizeSpecification.cs" />