diff --git a/src/NzbDrone.Core.Test/Datastore/Migration/022_orpheus_apiFixture.cs b/src/NzbDrone.Core.Test/Datastore/Migration/022_orpheus_apiFixture.cs new file mode 100644 index 000000000..656e2a992 --- /dev/null +++ b/src/NzbDrone.Core.Test/Datastore/Migration/022_orpheus_apiFixture.cs @@ -0,0 +1,58 @@ +using System; +using System.Linq; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Common.Serializer; +using NzbDrone.Core.Datastore.Migration; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.Datastore.Migration +{ + [TestFixture] + public class orpheus_apiFixture : MigrationTest + { + [Test] + public void should_convert_and_disable_orpheus_instance() + { + var db = WithMigrationTestDb(c => + { + c.Insert.IntoTable("Indexers").Row(new + { + Enable = true, + Name = "Orpheus", + Priority = 25, + Added = DateTime.UtcNow, + Implementation = "Orpheus", + Settings = new GazelleIndexerSettings021 + { + Username = "some name", + Password = "some pass" + }.ToJson(), + ConfigContract = "GazelleSettings" + }); + }); + + var items = db.Query("SELECT \"Id\", \"Enable\", \"ConfigContract\", \"Settings\" FROM \"Indexers\""); + + items.Should().HaveCount(1); + items.First().ConfigContract.Should().Be("OrpheusSettings"); + items.First().Enable.Should().Be(false); + items.First().Settings.Should().NotContain("username"); + items.First().Settings.Should().NotContain("password"); + } + } + + public class IndexerDefinition022 + { + public int Id { get; set; } + public bool Enable { get; set; } + public string ConfigContract { get; set; } + public string Settings { get; set; } + } + + public class GazelleIndexerSettings021 + { + public string Username { get; set; } + public string Password { get; set; } + } +} diff --git a/src/NzbDrone.Core.Test/Prowlarr.Core.Test.csproj b/src/NzbDrone.Core.Test/Prowlarr.Core.Test.csproj index 109a8badb..6d48fa0a6 100644 --- a/src/NzbDrone.Core.Test/Prowlarr.Core.Test.csproj +++ b/src/NzbDrone.Core.Test/Prowlarr.Core.Test.csproj @@ -21,7 +21,4 @@ PreserveNewest - - - diff --git a/src/NzbDrone.Core/Datastore/Migration/008_redacted_api.cs b/src/NzbDrone.Core/Datastore/Migration/008_redacted_api.cs index 796e74d13..4524af9e6 100644 --- a/src/NzbDrone.Core/Datastore/Migration/008_redacted_api.cs +++ b/src/NzbDrone.Core/Datastore/Migration/008_redacted_api.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using System.Data; using FluentMigrator; using Newtonsoft.Json.Linq; @@ -21,6 +22,8 @@ namespace NzbDrone.Core.Datastore.Migration cmd.Transaction = tran; cmd.CommandText = "SELECT \"Id\", \"Settings\" FROM \"Indexers\" WHERE \"Implementation\" = 'Redacted'"; + var updatedIndexers = new List(); + using (var reader = cmd.ExecuteReader()) { while (reader.Read()) @@ -45,19 +48,26 @@ namespace NzbDrone.Core.Datastore.Migration // write new json back to db, switch to new ConfigContract, and disable the indexer settings = jsonObject.ToJson(); - using (var updateCmd = conn.CreateCommand()) + + updatedIndexers.Add(new Indexer008 { - updateCmd.Transaction = tran; - updateCmd.CommandText = "UPDATE \"Indexers\" SET \"Settings\" = ?, \"ConfigContract\" = ?, \"Enable\" = 0 WHERE \"Id\" = ?"; - updateCmd.AddParameter(settings); - updateCmd.AddParameter("RedactedSettings"); - updateCmd.AddParameter(id); - updateCmd.ExecuteNonQuery(); - } + Id = id, + Settings = settings, + ConfigContract = "RedactedSettings", + Enable = false + }); } } } } } + + public class Indexer008 + { + public int Id { get; set; } + public string Settings { get; set; } + public string ConfigContract { get; set; } + public bool Enable { get; set; } + } } } diff --git a/src/NzbDrone.Core/Datastore/Migration/022_orpheus_api.cs b/src/NzbDrone.Core/Datastore/Migration/022_orpheus_api.cs index 3907af367..a35a51d58 100644 --- a/src/NzbDrone.Core/Datastore/Migration/022_orpheus_api.cs +++ b/src/NzbDrone.Core/Datastore/Migration/022_orpheus_api.cs @@ -1,8 +1,12 @@ +using System; +using System.Collections.Generic; using System.Data; +using Dapper; using FluentMigrator; using Newtonsoft.Json.Linq; using NzbDrone.Common.Serializer; using NzbDrone.Core.Datastore.Migration.Framework; +using static NzbDrone.Core.Datastore.Migration.redacted_api; namespace NzbDrone.Core.Datastore.Migration { @@ -21,6 +25,8 @@ namespace NzbDrone.Core.Datastore.Migration cmd.Transaction = tran; cmd.CommandText = "SELECT \"Id\", \"Settings\" FROM \"Indexers\" WHERE \"Implementation\" = 'Orpheus'"; + var updatedIndexers = new List(); + using (var reader = cmd.ExecuteReader()) { while (reader.Read()) @@ -45,18 +51,20 @@ namespace NzbDrone.Core.Datastore.Migration // write new json back to db, switch to new ConfigContract, and disable the indexer settings = jsonObject.ToJson(); - using (var updateCmd = conn.CreateCommand()) + + updatedIndexers.Add(new Indexer008 { - updateCmd.Transaction = tran; - updateCmd.CommandText = "UPDATE \"Indexers\" SET \"Settings\" = ?, \"ConfigContract\" = ?, \"Enable\" = 0 WHERE \"Id\" = ?"; - updateCmd.AddParameter(settings); - updateCmd.AddParameter("OrpheusSettings"); - updateCmd.AddParameter(id); - updateCmd.ExecuteNonQuery(); - } + Id = id, + Settings = settings, + ConfigContract = "OrpheusSettings", + Enable = false + }); } } } + + var updateSql = "UPDATE \"Indexers\" SET \"Settings\" = @Settings, \"ConfigContract\" = @ConfigContract, \"Enable\" = @Enable WHERE \"Id\" = @Id"; + conn.Execute(updateSql, updatedIndexers, transaction: tran); } } }