From 7603d8e1babf19d8681a88422aeff02434a105d6 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sun, 24 Mar 2013 03:09:32 -0700 Subject: [PATCH] auto increment ID. model tables are automatically created. --- .../Configuration/ConfigCachingFixture.cs | 2 +- .../Configuration/ConfigServiceFixture.cs | 2 +- .../NzbDrone.Core.Test.ncrunchproject | 4 ++-- NzbDrone.Core/Configuration/Config.cs | 2 +- NzbDrone.Core/Datastore/DbFactory.cs | 6 ++++- NzbDrone.Core/Datastore/Migration.cs | 22 +++++++++++++++++++ NzbDrone.Core/Datastore/ModelBase.cs | 2 ++ NzbDrone.Core/NzbDrone.Core.csproj | 1 + 8 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 NzbDrone.Core/Datastore/Migration.cs diff --git a/NzbDrone.Core.Test/Configuration/ConfigCachingFixture.cs b/NzbDrone.Core.Test/Configuration/ConfigCachingFixture.cs index b4893b8b5..a81808ada 100644 --- a/NzbDrone.Core.Test/Configuration/ConfigCachingFixture.cs +++ b/NzbDrone.Core.Test/Configuration/ConfigCachingFixture.cs @@ -16,7 +16,7 @@ public class ConfigCachingFixture : CoreTest public void Setup() { Mocker.GetMock().Setup(c => c.All()) - .Returns(new List { new Config { Key = "Key1", Value = "Value1" } }); + .Returns(new List { new Config { Key = "key1", Value = "Value1" } }); } diff --git a/NzbDrone.Core.Test/Configuration/ConfigServiceFixture.cs b/NzbDrone.Core.Test/Configuration/ConfigServiceFixture.cs index 581191620..48a95ada2 100644 --- a/NzbDrone.Core.Test/Configuration/ConfigServiceFixture.cs +++ b/NzbDrone.Core.Test/Configuration/ConfigServiceFixture.cs @@ -150,7 +150,7 @@ public void config_properties_should_write_and_read_using_same_key() if (propertyInfo.PropertyType == typeof(string)) { - value = new Guid().ToString(); + value = Guid.NewGuid().ToString(); } else if (propertyInfo.PropertyType == typeof(int)) { diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.ncrunchproject b/NzbDrone.Core.Test/NzbDrone.Core.Test.ncrunchproject index a6f0b3036..93cb9aec5 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.ncrunchproject +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.ncrunchproject @@ -5,8 +5,8 @@ true false false - false - false + true + true true true false diff --git a/NzbDrone.Core/Configuration/Config.cs b/NzbDrone.Core/Configuration/Config.cs index 8a561180d..c8e569fb0 100644 --- a/NzbDrone.Core/Configuration/Config.cs +++ b/NzbDrone.Core/Configuration/Config.cs @@ -6,7 +6,7 @@ namespace NzbDrone.Core.Configuration public class Config : ModelBase { public string Key { get; set; } - + public string Value { get; set; } } } \ No newline at end of file diff --git a/NzbDrone.Core/Datastore/DbFactory.cs b/NzbDrone.Core/Datastore/DbFactory.cs index 0e4a79f1b..17d14578c 100644 --- a/NzbDrone.Core/Datastore/DbFactory.cs +++ b/NzbDrone.Core/Datastore/DbFactory.cs @@ -30,7 +30,11 @@ public IDbConnection Create(string dbPath = null) OrmLiteConfig.DialectProvider = new SqliteOrmLiteDialectProvider(); var dbFactory = new OrmLiteConnectionFactory(connectionString); - return dbFactory.Open(); + var connection = dbFactory.Open(); + + Migration.CreateTables(connection); + + return connection; } private string GetConnectionString(string dbPath) diff --git a/NzbDrone.Core/Datastore/Migration.cs b/NzbDrone.Core/Datastore/Migration.cs new file mode 100644 index 000000000..a43391811 --- /dev/null +++ b/NzbDrone.Core/Datastore/Migration.cs @@ -0,0 +1,22 @@ +using System.Data; +using System.Linq; +using ServiceStack.OrmLite; + +namespace NzbDrone.Core.Datastore +{ + public static class Migration + { + public static void CreateTables(IDbConnection dbConnection) + { + var types = typeof(ModelBase).Assembly.GetTypes(); + + var models = types.Where(c => c.BaseType == typeof(ModelBase)); + + foreach (var model in models) + { + dbConnection.CreateTable(true, model); + } + } + + } +} diff --git a/NzbDrone.Core/Datastore/ModelBase.cs b/NzbDrone.Core/Datastore/ModelBase.cs index 4c2c9a43c..264fff0b8 100644 --- a/NzbDrone.Core/Datastore/ModelBase.cs +++ b/NzbDrone.Core/Datastore/ModelBase.cs @@ -1,10 +1,12 @@ using System.Diagnostics; +using ServiceStack.DataAnnotations; namespace NzbDrone.Core.Datastore { [DebuggerDisplay("{GetType()} ID = {Id}")] public abstract class ModelBase { + [AutoIncrement] public int Id { get; set; } } } diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index eb52a0919..e3c36e5ec 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -201,6 +201,7 @@ +