From cbfec3df3c875db12a2505efbb5c1d805336940d Mon Sep 17 00:00:00 2001 From: Robin Dadswell <19610103+RobinDadswell@users.noreply.github.com> Date: Mon, 12 May 2025 17:51:06 +0100 Subject: [PATCH] New: Updated all newznab indexers to be redirect --- ...newznab_indexers_enable_redirectFixture.cs | 192 ++++++++++++++++++ .../043_newznab_indexers_enable_redirect.cs | 22 ++ 2 files changed, 214 insertions(+) create mode 100644 src/NzbDrone.Core.Test/Datastore/Migration/043_newznab_indexers_enable_redirectFixture.cs create mode 100644 src/NzbDrone.Core/Datastore/Migration/043_newznab_indexers_enable_redirect.cs diff --git a/src/NzbDrone.Core.Test/Datastore/Migration/043_newznab_indexers_enable_redirectFixture.cs b/src/NzbDrone.Core.Test/Datastore/Migration/043_newznab_indexers_enable_redirectFixture.cs new file mode 100644 index 000000000..8681f82fb --- /dev/null +++ b/src/NzbDrone.Core.Test/Datastore/Migration/043_newznab_indexers_enable_redirectFixture.cs @@ -0,0 +1,192 @@ +using System; +using System.Linq; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Core.Datastore.Migration; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.Datastore.Migration +{ + [TestFixture] + public class newznab_indexers_enable_redirectFixture : MigrationTest + { + [Test] + public void should_update_redirect_setting_to_true_if_false() + { + var db = WithMigrationTestDb(c => + { + c.Insert.IntoTable("Indexers").Row(new + { + Id = 1, + Name = "Test", + Implementation = "Newznab", + Settings = "{\"baseUrl\":\"https://example.com\",\"apiKey\":\"testapikey\"}", + ConfigContract = "NewznabSettings", + Enable = true, + Priority = 1, + Added = System.DateTime.UtcNow, + Redirect = false, + AppProfileId = 1, + Tags = "[]", + DownloadClientId = 0 + }); + }); + + var items = db.Query("SELECT * FROM \"Indexers\""); + + items.Should().HaveCount(1); + items.First().Implementation.Should().Be("Newznab"); + items.First().Redirect.Should().BeTrue(); // Validate Redirect is updated + } + + [Test] + public void should_not_change_redirect_setting_if_already_true() + { + var db = WithMigrationTestDb(c => + { + c.Insert.IntoTable("Indexers").Row(new + { + Id = 1, + Name = "Test", + Implementation = "Newznab", + Settings = "{\"baseUrl\":\"https://example.com\",\"apiKey\":\"testapikey\"}", + ConfigContract = "NewznabSettings", + Enable = true, + Priority = 2, + Added = System.DateTime.UtcNow, + Redirect = true, + AppProfileId = 1, + Tags = "[]", + DownloadClientId = 0 + }); + }); + + var items = db.Query("SELECT * FROM \"Indexers\""); + + items.Should().HaveCount(1); + items.First().Implementation.Should().Be("Newznab"); + items.First().Redirect.Should().BeTrue(); // Validate Redirect remains true + } + + [Test] + public void should_not_affect_non_newznab_indexers() + { + var db = WithMigrationTestDb(c => + { + c.Insert.IntoTable("Indexers").Row(new + { + Id = 1, + Name = "Test", + Implementation = "OtherIndexer", + Settings = "{\"baseUrl\":\"https://otherindexer.com\"}", + ConfigContract = "OtherIndexerSettings", + Enable = true, + Priority = 3, + Added = System.DateTime.UtcNow, + Redirect = false, + AppProfileId = 1, + Tags = "[]", + DownloadClientId = 0 + }); + }); + + var items = db.Query("SELECT * FROM \"Indexers\""); + + items.Should().HaveCount(1); + items.First().Implementation.Should().Be("OtherIndexer"); + items.First().Redirect.Should().BeFalse(); // Validate Redirect is not changed + } + + [Test] + public void should_handle_multiple_indexers() + { + var db = WithMigrationTestDb(c => + { + c.Insert.IntoTable("Indexers").Row(new + { + Id = 1, + Name = "Test 1", + Implementation = "Newznab", + Settings = "{\"baseUrl\":\"https://example1.com\",\"apiKey\":\"testapikey1\"}", + ConfigContract = "NewznabSettings", + Enable = true, + Priority = 4, + Added = System.DateTime.UtcNow, + Redirect = false, + AppProfileId = 1, + Tags = "[]", + DownloadClientId = 0 + }); + c.Insert.IntoTable("Indexers").Row(new + { + Id = 2, + Name = "Test 2", + Implementation = "Newznab", + Settings = "{\"baseUrl\":\"https://example2.com\",\"apiKey\":\"testapikey2\"}", + ConfigContract = "NewznabSettings", + Enable = true, + Priority = 5, + Added = System.DateTime.UtcNow, + Redirect = false, + AppProfileId = 1, + Tags = "[]", + DownloadClientId = 0 + }); + c.Insert.IntoTable("Indexers").Row(new + { + Id = 3, + Name = "Test 3", + Implementation = "Newznab", + Settings = "{\"baseUrl\":\"https://example3.com\",\"apiKey\":\"testapikey3\"}", + ConfigContract = "NewznabSettings", + Enable = true, + Priority = 6, + Added = System.DateTime.UtcNow, + Redirect = true, + AppProfileId = 1, + Tags = "[]", + DownloadClientId = 0 + }); + c.Insert.IntoTable("Indexers").Row(new + { + Id = 4, + Name = "Test 4", + Implementation = "OtherIndexer", + Settings = "{\"baseUrl\":\"https://otherindexer.com\"}", + ConfigContract = "OtherIndexerSettings", + Enable = true, + Priority = 7, + Added = System.DateTime.UtcNow, + Redirect = false, + AppProfileId = 1, + Tags = "[]", + DownloadClientId = 0 + }); + }); + + var items = db.Query("SELECT * FROM \"Indexers\""); + + items.Should().HaveCount(4); + items.First(i => i.Id == 1).Redirect.Should().BeTrue(); // Validate Redirect is updated + items.First(i => i.Id == 2).Redirect.Should().BeTrue(); // Validate Redirect is updated + items.First(i => i.Id == 3).Redirect.Should().BeTrue(); // Validate Redirect remains true + items.First(i => i.Id == 4).Redirect.Should().BeFalse(); // Validate Redirect is not changed + } + } + + public class IndexerDefinition043 + { + public int Id { get; set; } + public string Name { get; set; } + public string Implementation { get; set; } + public string Settings { get; set; } + public string ConfigContract { get; set; } + public bool Enable { get; set; } + public int Priority { get; set; } + public DateTime Added { get; set; } + public bool Redirect { get; set; } + public int AppProfileId { get; set; } + public string Tags { get; set; } + public int DownloadClientId { get; set; } + } +} diff --git a/src/NzbDrone.Core/Datastore/Migration/043_newznab_indexers_enable_redirect.cs b/src/NzbDrone.Core/Datastore/Migration/043_newznab_indexers_enable_redirect.cs new file mode 100644 index 000000000..4411e226e --- /dev/null +++ b/src/NzbDrone.Core/Datastore/Migration/043_newznab_indexers_enable_redirect.cs @@ -0,0 +1,22 @@ +using System.Data; +using Dapper; +using FluentMigrator; +using NzbDrone.Core.Datastore.Migration.Framework; + +namespace NzbDrone.Core.Datastore.Migration +{ + [Migration(043)] + public class newznab_indexers_enable_redirect : NzbDroneMigrationBase + { + protected override void MainDbUpgrade() + { + Execute.WithConnection(UpdateNewznabRedirectSetting); + } + + private void UpdateNewznabRedirectSetting(IDbConnection conn, IDbTransaction tran) + { + var updateSql = "UPDATE \"Indexers\" SET \"Redirect\" = @Redirect WHERE \"Implementation\" = 'Newznab' AND \"Redirect\" = false"; + conn.Execute(updateSql, new { Redirect = true }, transaction: tran); + } + } +}