From ec7f7495416ab630995228c5f33a872cf1d6f056 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Sun, 26 Mar 2017 21:22:58 +0200 Subject: [PATCH] Tweaked default config for extra files import. --- .../109_import_extra_files_configFixture.cs | 54 +++++++ .../110_fix_extra_files_configFixture.cs | 133 ++++++++++++++++++ .../Framework/DirectDataMapper.cs | 41 ++++-- .../NzbDrone.Core.Test.csproj | 2 + .../Configuration/ConfigService.cs | 2 +- .../Migration/110_fix_extra_files_config.cs | 57 ++++++++ src/NzbDrone.Core/NzbDrone.Core.csproj | 1 + .../Sorting/SortingViewTemplate.hbs | 7 +- 8 files changed, 279 insertions(+), 18 deletions(-) create mode 100644 src/NzbDrone.Core.Test/Datastore/Migration/109_import_extra_files_configFixture.cs create mode 100644 src/NzbDrone.Core.Test/Datastore/Migration/110_fix_extra_files_configFixture.cs create mode 100644 src/NzbDrone.Core/Datastore/Migration/110_fix_extra_files_config.cs diff --git a/src/NzbDrone.Core.Test/Datastore/Migration/109_import_extra_files_configFixture.cs b/src/NzbDrone.Core.Test/Datastore/Migration/109_import_extra_files_configFixture.cs new file mode 100644 index 000000000..4ef9e4979 --- /dev/null +++ b/src/NzbDrone.Core.Test/Datastore/Migration/109_import_extra_files_configFixture.cs @@ -0,0 +1,54 @@ +using System.Linq; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Core.Datastore.Migration; +using NzbDrone.Core.Parser; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.Datastore.Migration +{ + [TestFixture] + public class import_extra_files_configFixture : MigrationTest + { + [Test] + public void should_not_insert_if_missing() + { + var db = WithMigrationTestDb(); + + var items = db.QueryScalar("SELECT Value FROM Config WHERE Key = 'importextrafiles'"); + items.Should().BeNull(); + } + + [Test] + public void should_not_insert_if_empty() + { + var db = WithMigrationTestDb(c => + { + c.Insert.IntoTable("Config").Row(new + { + Key = "extrafileextensions", + Value = "" + }); + }); + + var items = db.QueryScalar("SELECT Value FROM Config WHERE Key = 'importextrafiles'"); + items.Should().BeNull(); + } + + [Test] + public void should_insert_True_if_configured() + { + var db = WithMigrationTestDb(c => + { + c.Insert.IntoTable("Config").Row(new + { + Key = "extrafileextensions", + Value = "srt" + }); + }); + + var items = db.QueryScalar("SELECT Value FROM Config WHERE Key = 'importextrafiles'"); + items.Should().Be("True"); + } + } +} diff --git a/src/NzbDrone.Core.Test/Datastore/Migration/110_fix_extra_files_configFixture.cs b/src/NzbDrone.Core.Test/Datastore/Migration/110_fix_extra_files_configFixture.cs new file mode 100644 index 000000000..1a38efc1c --- /dev/null +++ b/src/NzbDrone.Core.Test/Datastore/Migration/110_fix_extra_files_configFixture.cs @@ -0,0 +1,133 @@ +using System.Linq; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Core.Datastore.Migration; +using NzbDrone.Core.Parser; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.Datastore.Migration +{ + [TestFixture] + public class fix_extra_files_configFixture : MigrationTest + { + [Test] + public void should_not_update_importextrafiles_disabled() + { + var db = WithMigrationTestDb(); + + var itemEnabled = db.QueryScalar("SELECT Value FROM Config WHERE Key = 'importextrafiles'"); + itemEnabled.Should().BeNull(); + } + + [Test] + public void should_fix_importextrafiles_if_wrong() + { + var db = WithMigrationTestDb(c => + { + c.Insert.IntoTable("Config").Row(new + { + Key = "importextrafiles", + Value = 1 + }); + }); + + + var itemEnabled = db.QueryScalar("SELECT Value FROM Config WHERE Key = 'importextrafiles'"); + itemEnabled.Should().Be("True"); + } + + [Test] + public void should_fill_in_default_extensions() + { + var db = WithMigrationTestDb(c => + { + c.Insert.IntoTable("Config").Row(new + { + Key = "importextrafiles", + Value = "False" + }); + + c.Insert.IntoTable("Config").Row(new + { + Key = "extrafileextensions", + Value = "" + }); + }); + + var itemEnabled = db.QueryScalar("SELECT Value FROM Config WHERE Key = 'importextrafiles'"); + itemEnabled.Should().Be("False"); + + var itemExtensions = db.QueryScalar("SELECT Value FROM Config WHERE Key = 'extrafileextensions'"); + itemExtensions.Should().Be("srt"); + } + + [Test] + public void should_not_fill_in_default_extensions() + { + var db = WithMigrationTestDb(c => + { + c.Insert.IntoTable("Config").Row(new + { + Key = "importextrafiles", + Value = "True" + }); + + c.Insert.IntoTable("Config").Row(new + { + Key = "extrafileextensions", + Value = "" + }); + }); + + var itemEnabled = db.QueryScalar("SELECT Value FROM Config WHERE Key = 'importextrafiles'"); + itemEnabled.Should().Be("True"); + + var itemExtensions = db.QueryScalar("SELECT Value FROM Config WHERE Key = 'extrafileextensions'"); + itemExtensions.Should().Be(""); + } + + [Test] + public void should_not_fill_in_default_extensions_if_not_defined() + { + var db = WithMigrationTestDb(c => + { + c.Insert.IntoTable("Config").Row(new + { + Key = "importextrafiles", + Value = "False" + }); + }); + + var itemEnabled = db.QueryScalar("SELECT Value FROM Config WHERE Key = 'importextrafiles'"); + itemEnabled.Should().Be("False"); + + var itemExtensions = db.QueryScalar("SELECT Value FROM Config WHERE Key = 'extrafileextensions'"); + itemExtensions.Should().BeNull(); + } + + [Test] + public void should_not_fill_in_default_extensions_if_already_defined() + { + var db = WithMigrationTestDb(c => + { + c.Insert.IntoTable("Config").Row(new + { + Key = "importextrafiles", + Value = "False" + }); + + c.Insert.IntoTable("Config").Row(new + { + Key = "extrafileextensions", + Value = "sub" + }); + }); + + var itemEnabled = db.QueryScalar("SELECT Value FROM Config WHERE Key = 'importextrafiles'"); + itemEnabled.Should().Be("False"); + + var itemExtensions = db.QueryScalar("SELECT Value FROM Config WHERE Key = 'extrafileextensions'"); + itemExtensions.Should().Be("sub"); + } + } +} diff --git a/src/NzbDrone.Core.Test/Framework/DirectDataMapper.cs b/src/NzbDrone.Core.Test/Framework/DirectDataMapper.cs index 27b4354c1..a75daf167 100644 --- a/src/NzbDrone.Core.Test/Framework/DirectDataMapper.cs +++ b/src/NzbDrone.Core.Test/Framework/DirectDataMapper.cs @@ -12,6 +12,7 @@ namespace NzbDrone.Core.Test.Framework { List> Query(string sql); List Query(string sql) where T : new(); + T QueryScalar(string sql); } public class DirectDataMapper : IDirectDataMapper @@ -25,7 +26,7 @@ namespace NzbDrone.Core.Test.Framework _providerFactory = dataMapper.ProviderFactory; _connectionString = dataMapper.ConnectionString; } - + private DbConnection OpenConnection() { var connection = _providerFactory.CreateConnection(); @@ -62,6 +63,13 @@ namespace NzbDrone.Core.Test.Framework return dataTable.Rows.Cast().Select(MapToObject).ToList(); } + public T QueryScalar(string sql) + { + var dataTable = GetDataTable(sql); + + return dataTable.Rows.Cast().Select(d => MapValue(d, 0, typeof(T))).Cast().FirstOrDefault(); + } + protected Dictionary MapToDictionary(DataRow dataRow) { var item = new Dictionary(); @@ -107,24 +115,29 @@ namespace NzbDrone.Core.Test.Framework propertyType = propertyType.GetGenericArguments()[0]; } - object value; - if (dataRow.ItemArray[i] == DBNull.Value) - { - value = null; - } - else if (dataRow.Table.Columns[i].DataType == typeof(string) && propertyType != typeof(string)) - { - value = Json.Deserialize((string)dataRow.ItemArray[i], propertyType); - } - else - { - value = Convert.ChangeType(dataRow.ItemArray[i], propertyType); - } + object value = MapValue(dataRow, i, propertyType); + propertyInfo.SetValue(item, value, null); } return item; } + + private object MapValue(DataRow dataRow, int i, Type targetType) + { + if (dataRow.ItemArray[i] == DBNull.Value) + { + return null; + } + else if (dataRow.Table.Columns[i].DataType == typeof(string) && targetType != typeof(string)) + { + return Json.Deserialize((string)dataRow.ItemArray[i], targetType); + } + else + { + return Convert.ChangeType(dataRow.ItemArray[i], targetType); + } + } } } diff --git a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 9d279753e..f8e7b07c9 100644 --- a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -120,6 +120,8 @@ + + diff --git a/src/NzbDrone.Core/Configuration/ConfigService.cs b/src/NzbDrone.Core/Configuration/ConfigService.cs index 06f7c3006..d9b57d68a 100644 --- a/src/NzbDrone.Core/Configuration/ConfigService.cs +++ b/src/NzbDrone.Core/Configuration/ConfigService.cs @@ -212,7 +212,7 @@ namespace NzbDrone.Core.Configuration public string ExtraFileExtensions { - get { return GetValue("ExtraFileExtensions", ""); } + get { return GetValue("ExtraFileExtensions", "srt"); } set { SetValue("ExtraFileExtensions", value); } } diff --git a/src/NzbDrone.Core/Datastore/Migration/110_fix_extra_files_config.cs b/src/NzbDrone.Core/Datastore/Migration/110_fix_extra_files_config.cs new file mode 100644 index 000000000..5f136deb1 --- /dev/null +++ b/src/NzbDrone.Core/Datastore/Migration/110_fix_extra_files_config.cs @@ -0,0 +1,57 @@ +using System.Data; +using FluentMigrator; +using NzbDrone.Common.Extensions; +using NzbDrone.Core.Datastore.Migration.Framework; + +namespace NzbDrone.Core.Datastore.Migration +{ + [Migration(110)] + public class fix_extra_files_config : NzbDroneMigrationBase + { + protected override void MainDbUpgrade() + { + Execute.WithConnection(FixExtraFilesConfig); + } + + private void FixExtraFilesConfig(IDbConnection conn, IDbTransaction tran) + { + string extraFileExtensions; + string importExtraFiles; + + using (var cmd = conn.CreateCommand()) + { + cmd.Transaction = tran; + cmd.CommandText = "SELECT Value FROM Config WHERE Key = 'extrafileextensions'"; + + extraFileExtensions = (string)cmd.ExecuteScalar(); + } + + using (var cmd = conn.CreateCommand()) + { + cmd.Transaction = tran; + cmd.CommandText = "SELECT Value FROM Config WHERE Key = 'importextrafiles'"; + + importExtraFiles = (string)cmd.ExecuteScalar(); + } + + if (importExtraFiles == "1" || importExtraFiles == "True") + { + using (var insertCmd = conn.CreateCommand()) + { + insertCmd.Transaction = tran; + insertCmd.CommandText = "UPDATE Config SET Value = 'True' WHERE Key = 'importextrafiles'"; + insertCmd.ExecuteNonQuery(); + } + } + else if (extraFileExtensions.IsNullOrWhiteSpace()) + { + using (var insertCmd = conn.CreateCommand()) + { + insertCmd.Transaction = tran; + insertCmd.CommandText = "UPDATE Config SET Value = 'srt' WHERE Key = 'extrafileextensions'"; + insertCmd.ExecuteNonQuery(); + } + } + } + } +} diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index ee6f77d07..9e238632b 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -248,6 +248,7 @@ + diff --git a/src/UI/Settings/MediaManagement/Sorting/SortingViewTemplate.hbs b/src/UI/Settings/MediaManagement/Sorting/SortingViewTemplate.hbs index 0e37e2a95..f3612203f 100644 --- a/src/UI/Settings/MediaManagement/Sorting/SortingViewTemplate.hbs +++ b/src/UI/Settings/MediaManagement/Sorting/SortingViewTemplate.hbs @@ -25,11 +25,11 @@ -
+
Importing {{#if_mono}} -
+
@@ -53,7 +53,7 @@
{{/if_mono}} -
+
@@ -111,4 +111,5 @@
+