From 3cbdd6bfd3a8a7314457de9e598c021b2de16847 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Thu, 5 Dec 2019 23:59:36 +0100 Subject: [PATCH] Fixed: Rare scenario where early Radarr version messes up Sonarr database --- ...ity_profiles_add_upgrade_allowedFixture.cs | 28 +++++++++++++ ...me_quality_profiles_add_upgrade_allowed.cs | 40 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/NzbDrone.Core.Test/Datastore/Migration/128_rename_quality_profiles_add_upgrade_allowedFixture.cs diff --git a/src/NzbDrone.Core.Test/Datastore/Migration/128_rename_quality_profiles_add_upgrade_allowedFixture.cs b/src/NzbDrone.Core.Test/Datastore/Migration/128_rename_quality_profiles_add_upgrade_allowedFixture.cs new file mode 100644 index 000000000..87dcfb614 --- /dev/null +++ b/src/NzbDrone.Core.Test/Datastore/Migration/128_rename_quality_profiles_add_upgrade_allowedFixture.cs @@ -0,0 +1,28 @@ +using System.Linq; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Core.Datastore.Migration; +using NzbDrone.Core.Datastore.Migration.Framework; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.Datastore.Migration +{ + [TestFixture] + public class rename_quality_profiles_add_upgrade_allowedFixture : MigrationTest + { + [Test] + public void should_handle_injected_radarr_migration() + { + var dbBefore = WithTestDb(new MigrationContext(MigrationType, 110)); + + // Ensure 111 isn't applied + dbBefore.GetDirectDataMapper().Query("INSERT INTO VersionInfo (Version, AppliedOn, Description) VALUES (111, '2018-12-24T18:21:07', 'remove_bitmetv')"); + + var dbAfter = WithMigrationTestDb(); + + var result = dbAfter.QueryScalar("SELECT COUNT(*) FROM VersionInfo WHERE Description = 'remove_bitmetv'"); + + result.Should().Be(0); + } + } +} diff --git a/src/NzbDrone.Core/Datastore/Migration/128_rename_quality_profiles_add_upgrade_allowed.cs b/src/NzbDrone.Core/Datastore/Migration/128_rename_quality_profiles_add_upgrade_allowed.cs index 91739863b..c56c59e8b 100644 --- a/src/NzbDrone.Core/Datastore/Migration/128_rename_quality_profiles_add_upgrade_allowed.cs +++ b/src/NzbDrone.Core/Datastore/Migration/128_rename_quality_profiles_add_upgrade_allowed.cs @@ -1,13 +1,20 @@ using FluentMigrator; +using FluentMigrator.Infrastructure; using NzbDrone.Core.Datastore.Migration.Framework; +using System.Data; +using System.Data.SQLite; namespace NzbDrone.Core.Datastore.Migration { [Migration(128)] public class rename_quality_profiles_add_upgrade_allowed : NzbDroneMigrationBase { + private IMigrationContext _capturedContext; + protected override void MainDbUpgrade() { + FixupMigration111(); + Rename.Table("Profiles").To("QualityProfiles"); Alter.Table("QualityProfiles").AddColumn("UpgradeAllowed").AsInt32().Nullable(); @@ -19,5 +26,38 @@ namespace NzbDrone.Core.Datastore.Migration Rename.Column("ProfileId").OnTable("Series").To("QualityProfileId"); } + + public override void GetUpExpressions(IMigrationContext context) + { + _capturedContext = context; + + base.GetUpExpressions(context); + + _capturedContext = null; + } + + // Early Radarr versions can mess up Sonarr's database if they point to the same config. Fixup the damage. + private void FixupMigration111() + { + // In order to get the expressions we need to check the database directly + using (var connection = new SQLiteConnection(ConnectionString)) + using (var command = connection.CreateCommand()) + { + connection.Open(); + command.CommandText = "SELECT Description FROM VersionInfo WHERE Version = 111"; + + var description = command.ExecuteScalar() as string; + connection.Close(); + + if (description == "remove_bitmetv") + { + // Get the migration expressions from the 111 migration + var migration111 = new create_language_profiles(); + migration111.GetUpExpressions(_capturedContext); + + Execute.Sql("UPDATE VersionInfo SET Description = 'create_language_profiles fixup' WHERE Version = 111"); + } + } + } } }