1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-05 02:22:31 +01:00

Tweaked default config for extra files import.

This commit is contained in:
Taloth Saldono 2017-03-26 21:22:58 +02:00
parent 56ecbf4a31
commit ec7f749541
8 changed files with 279 additions and 18 deletions

View File

@ -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<import_extra_files>
{
[Test]
public void should_not_insert_if_missing()
{
var db = WithMigrationTestDb();
var items = db.QueryScalar<string>("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<string>("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<string>("SELECT Value FROM Config WHERE Key = 'importextrafiles'");
items.Should().Be("True");
}
}
}

View File

@ -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<fix_extra_files_config>
{
[Test]
public void should_not_update_importextrafiles_disabled()
{
var db = WithMigrationTestDb();
var itemEnabled = db.QueryScalar<string>("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<string>("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<string>("SELECT Value FROM Config WHERE Key = 'importextrafiles'");
itemEnabled.Should().Be("False");
var itemExtensions = db.QueryScalar<string>("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<string>("SELECT Value FROM Config WHERE Key = 'importextrafiles'");
itemEnabled.Should().Be("True");
var itemExtensions = db.QueryScalar<string>("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<string>("SELECT Value FROM Config WHERE Key = 'importextrafiles'");
itemEnabled.Should().Be("False");
var itemExtensions = db.QueryScalar<string>("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<string>("SELECT Value FROM Config WHERE Key = 'importextrafiles'");
itemEnabled.Should().Be("False");
var itemExtensions = db.QueryScalar<string>("SELECT Value FROM Config WHERE Key = 'extrafileextensions'");
itemExtensions.Should().Be("sub");
}
}
}

View File

@ -12,6 +12,7 @@ public interface IDirectDataMapper
{
List<Dictionary<string, object>> Query(string sql);
List<T> Query<T>(string sql) where T : new();
T QueryScalar<T>(string sql);
}
public class DirectDataMapper : IDirectDataMapper
@ -25,7 +26,7 @@ public DirectDataMapper(IDatabase database)
_providerFactory = dataMapper.ProviderFactory;
_connectionString = dataMapper.ConnectionString;
}
private DbConnection OpenConnection()
{
var connection = _providerFactory.CreateConnection();
@ -62,6 +63,13 @@ public List<Dictionary<string, object>> Query(string sql)
return dataTable.Rows.Cast<DataRow>().Select(MapToObject<T>).ToList();
}
public T QueryScalar<T>(string sql)
{
var dataTable = GetDataTable(sql);
return dataTable.Rows.Cast<DataRow>().Select(d => MapValue(d, 0, typeof(T))).Cast<T>().FirstOrDefault();
}
protected Dictionary<string, object> MapToDictionary(DataRow dataRow)
{
var item = new Dictionary<string, object>();
@ -107,24 +115,29 @@ protected Dictionary<string, object> MapToDictionary(DataRow dataRow)
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);
}
}
}
}

View File

@ -120,6 +120,8 @@
<Compile Include="Datastore\MappingExtentionFixture.cs" />
<Compile Include="Datastore\MarrDataLazyLoadingFixture.cs" />
<Compile Include="Datastore\Migration\108_fix_metadata_file_extensionsFixture.cs" />
<Compile Include="Datastore\Migration\109_import_extra_files_configFixture.cs" />
<Compile Include="Datastore\Migration\110_fix_extra_files_configFixture.cs" />
<Compile Include="Datastore\Migration\106_update_btn_urlFixture.cs" />
<Compile Include="Datastore\Migration\103_fix_metadata_file_extensionsFixture.cs" />
<Compile Include="Datastore\Migration\099_extra_and_subtitle_filesFixture.cs" />

View File

@ -212,7 +212,7 @@ public bool ImportExtraFiles
public string ExtraFileExtensions
{
get { return GetValue("ExtraFileExtensions", ""); }
get { return GetValue("ExtraFileExtensions", "srt"); }
set { SetValue("ExtraFileExtensions", value); }
}

View File

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

View File

@ -248,6 +248,7 @@
<Compile Include="Datastore\Migration\068_add_release_restrictions.cs" />
<Compile Include="Datastore\Migration\069_quality_proper.cs" />
<Compile Include="Datastore\Migration\070_delay_profile.cs" />
<Compile Include="Datastore\Migration\110_fix_extra_files_config.cs" />
<Compile Include="Datastore\Migration\109_import_extra_files.cs" />
<Compile Include="Datastore\Migration\108_fix_extra_file_extension.cs" />
<Compile Include="Datastore\Migration\107_remove_wombles.cs" />

View File

@ -25,11 +25,11 @@
</div>
</fieldset>
<fieldset class="advanced-setting">
<fieldset>
<legend>Importing</legend>
{{#if_mono}}
<div class="form-group">
<div class="form-group advanced-setting">
<label class="col-sm-3 control-label">Skip Free Space Check</label>
<div class="col-sm-9">
@ -53,7 +53,7 @@
</div>
{{/if_mono}}
<div class="form-group">
<div class="form-group advanced-setting">
<label class="col-sm-3 control-label">Use Hardlinks instead of Copy</label>
<div class="col-sm-9">
@ -111,4 +111,5 @@
<input type="text" name="extraFileExtensions" class="form-control"/>
</div>
</div>
</fieldset>