diff --git a/src/NzbDrone.Core.Test/Datastore/Migration/214_add_bluray576p_in_profileFixture.cs b/src/NzbDrone.Core.Test/Datastore/Migration/214_add_bluray576p_in_profileFixture.cs deleted file mode 100644 index 8fd403b91..000000000 --- a/src/NzbDrone.Core.Test/Datastore/Migration/214_add_bluray576p_in_profileFixture.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System.Linq; -using FluentAssertions; -using NUnit.Framework; -using NzbDrone.Core.Datastore.Migration; -using NzbDrone.Core.Qualities; -using NzbDrone.Core.Test.Framework; - -namespace NzbDrone.Core.Test.Datastore.Migration -{ - [TestFixture] - public class add_bluray576p_in_profileFixture : MigrationTest - { - private string GenerateQualityJson(int quality, bool allowed) - { - return $"{{ \"quality\": {quality}, \"allowed\": {allowed.ToString().ToLowerInvariant()} }}"; - } - - [Test] - public void should_add_bluray576p_to_old_profile() - { - var db = WithMigrationTestDb(c => - { - c.Insert.IntoTable("QualityProfiles").Row(new - { - Id = 0, - Name = "Bluray", - Cutoff = 7, - Items = $"[{GenerateQualityJson((int)Quality.DVD, true)}, {GenerateQualityJson((int)Quality.Bluray480p, true)}, {GenerateQualityJson((int)Quality.Bluray720p, false)}]" - }); - }); - - var profiles = db.Query("SELECT \"Items\" FROM \"QualityProfiles\" LIMIT 1"); - - var items = profiles.First().Items; - items.Should().HaveCount(4); - items.Select(v => v.Quality).Should().Equal((int)Quality.DVD, (int)Quality.Bluray480p, (int)Quality.Bluray576p, (int)Quality.Bluray720p); - items.Select(v => v.Allowed).Should().Equal(true, true, true, false); - items.Select(v => v.Name).Should().Equal(null, null, null, null); - } - } -} diff --git a/src/NzbDrone.Core.Test/Datastore/Migration/215_add_blurary576p_quality_in_profiles_with_grouped_blurary480pFixture.cs b/src/NzbDrone.Core.Test/Datastore/Migration/215_add_blurary576p_quality_in_profiles_with_grouped_blurary480pFixture.cs new file mode 100644 index 000000000..1a72d8379 --- /dev/null +++ b/src/NzbDrone.Core.Test/Datastore/Migration/215_add_blurary576p_quality_in_profiles_with_grouped_blurary480pFixture.cs @@ -0,0 +1,141 @@ +using System.Linq; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Core.Datastore.Migration; +using NzbDrone.Core.Qualities; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.Datastore.Migration +{ + [TestFixture] + public class add_blurary576p_quality_in_profiles_with_grouped_blurary480pFixture : MigrationTest + { + private string GenerateQualityJson(int quality, bool allowed) + { + return $"{{ \"quality\": {quality}, \"allowed\": {allowed.ToString().ToLowerInvariant()} }}"; + } + + private string GenerateQualityGroupJson(int id, string name, int[] qualities, bool allowed) + { + return $"{{ \"id\": {id}, \"name\": \"{name}\", \"items\": [{string.Join(", ", qualities.Select(q => $"{{ \"quality\": {q} }}"))}], \"allowed\": {allowed.ToString().ToLowerInvariant()} }}"; + } + + [Test] + public void should_add_bluray576p_to_old_profile() + { + var db = WithMigrationTestDb(c => + { + c.Insert.IntoTable("QualityProfiles").Row(new + { + Id = 0, + Name = "Bluray", + Cutoff = 7, + Items = $"[{GenerateQualityJson((int)Quality.DVD, true)}, {GenerateQualityJson((int)Quality.Bluray480p, true)}, {GenerateQualityJson((int)Quality.Bluray720p, false)}]" + }); + }); + + var profiles = db.Query("SELECT \"Items\" FROM \"QualityProfiles\" LIMIT 1"); + + var items = profiles.First().Items; + items.Should().HaveCount(4); + items.Select(v => v.Quality).Should().Equal((int)Quality.DVD, (int)Quality.Bluray480p, (int)Quality.Bluray576p, (int)Quality.Bluray720p); + items.Select(v => v.Allowed).Should().Equal(true, true, true, false); + items.Select(v => v.Name).Should().Equal(null, null, null, null); + } + + [Test] + public void should_not_allow_bluray576p_if_blurary480p_not_allowed() + { + var db = WithMigrationTestDb(c => + { + c.Insert.IntoTable("QualityProfiles").Row(new + { + Id = 0, + Name = "Bluray", + Cutoff = 7, + Items = $"[{GenerateQualityJson((int)Quality.DVD, true)}, {GenerateQualityJson((int)Quality.Bluray480p, false)}, {GenerateQualityJson((int)Quality.Bluray720p, false)}]" + }); + }); + + var profiles = db.Query("SELECT \"Items\" FROM \"QualityProfiles\" LIMIT 1"); + + var items = profiles.First().Items; + items.Should().HaveCount(4); + items.Select(v => v.Quality).Should().Equal((int)Quality.DVD, (int)Quality.Bluray480p, (int)Quality.Bluray576p, (int)Quality.Bluray720p); + items.Select(v => v.Allowed).Should().Equal(true, false, false, false); + items.Select(v => v.Name).Should().Equal(null, null, null, null); + } + + [Test] + public void should_add_bluray576p_to_old_profile_with_grouped_bluray_480p() + { + var db = WithMigrationTestDb(c => + { + c.Insert.IntoTable("QualityProfiles").Row(new + { + Id = 0, + Name = "Bluray", + Cutoff = 7, + Items = $"[{GenerateQualityGroupJson(1000, "DVD", new[] { (int)Quality.DVD, (int)Quality.Bluray480p }, true)}, {GenerateQualityJson((int)Quality.Bluray720p, false)}]" + }); + }); + + var profiles = db.Query("SELECT \"Items\" FROM \"QualityProfiles\" LIMIT 1"); + + var items = profiles.First().Items; + items.Should().HaveCount(3); + items.Select(v => v.Quality).Should().Equal(null, (int)Quality.Bluray576p, (int)Quality.Bluray720p); + items.Select(v => v.Id).Should().Equal(1000, 0, 0); + items.Select(v => v.Allowed).Should().Equal(true, true, false); + items.Select(v => v.Name).Should().Equal("DVD", null, null); + } + + [Test] + public void should_not_add_bluray576p_to_profile_with_bluray_576p() + { + var db = WithMigrationTestDb(c => + { + c.Insert.IntoTable("QualityProfiles").Row(new + { + Id = 0, + Name = "Bluray", + Cutoff = 7, + Items = $"[{GenerateQualityJson((int)Quality.DVD, true)}, {GenerateQualityJson((int)Quality.Bluray480p, false)}, {GenerateQualityJson((int)Quality.Bluray576p, false)}, {GenerateQualityJson((int)Quality.Bluray720p, false)}]" + }); + }); + + var profiles = db.Query("SELECT \"Items\" FROM \"QualityProfiles\" LIMIT 1"); + + var items = profiles.First().Items; + items.Should().HaveCount(4); + items.Select(v => v.Quality).Should().Equal((int)Quality.DVD, (int)Quality.Bluray480p, (int)Quality.Bluray576p, (int)Quality.Bluray720p); + items.Select(v => v.Allowed).Should().Equal(true, false, false, false); + items.Select(v => v.Name).Should().Equal(null, null, null, null); + } + + [Test] + public void should_not_add_bluray576p_to_profile_with_grouped_bluray_576p() + { + var db = WithMigrationTestDb(c => + { + c.Insert.IntoTable("QualityProfiles").Row(new + { + Id = 0, + Name = "Bluray", + Cutoff = 7, + Items = $"[{GenerateQualityGroupJson(1000, "DVD", new[] { (int)Quality.DVD, (int)Quality.Bluray480p, (int)Quality.Bluray576p }, true)}, {GenerateQualityJson((int)Quality.Bluray720p, false)}]" + }); + }); + + var profiles = db.Query("SELECT \"Items\" FROM \"QualityProfiles\" LIMIT 1"); + + var items = profiles.First().Items; + items.Should().HaveCount(2); + items.Select(v => v.Quality).Should().Equal(null, (int)Quality.Bluray720p); + items.Select(v => v.Id).Should().Equal(1000, 0); + items.Select(v => v.Allowed).Should().Equal(true, false); + items.Select(v => v.Name).Should().Equal("DVD", null); + items.First().Items.Select(v => v.Quality).Should().Equal((int)Quality.DVD, (int)Quality.Bluray480p, (int)Quality.Bluray576p); + } + } +} diff --git a/src/NzbDrone.Core/Datastore/Migration/214_fake_bluray576p.cs b/src/NzbDrone.Core/Datastore/Migration/214_fake_bluray576p.cs new file mode 100644 index 000000000..b5551ebce --- /dev/null +++ b/src/NzbDrone.Core/Datastore/Migration/214_fake_bluray576p.cs @@ -0,0 +1,14 @@ +using FluentMigrator; +using NzbDrone.Core.Datastore.Migration.Framework; + +namespace NzbDrone.Core.Datastore.Migration +{ + [Migration(214)] + public class add_blurary576p_quality_in_profiles : NzbDroneMigrationBase + { + protected override void MainDbUpgrade() + { + // Replaced with 215 + } + } +} diff --git a/src/NzbDrone.Core/Datastore/Migration/214_add_blurary576p_quality_in_profiles.cs b/src/NzbDrone.Core/Datastore/Migration/215_add_blurary576p_quality_in_profiles_with_grouped_blurary480p.cs similarity index 73% rename from src/NzbDrone.Core/Datastore/Migration/214_add_blurary576p_quality_in_profiles.cs rename to src/NzbDrone.Core/Datastore/Migration/215_add_blurary576p_quality_in_profiles_with_grouped_blurary480p.cs index 8024313cd..3a8bd6019 100644 --- a/src/NzbDrone.Core/Datastore/Migration/214_add_blurary576p_quality_in_profiles.cs +++ b/src/NzbDrone.Core/Datastore/Migration/215_add_blurary576p_quality_in_profiles_with_grouped_blurary480p.cs @@ -9,8 +9,8 @@ using NzbDrone.Core.Datastore.Migration.Framework; namespace NzbDrone.Core.Datastore.Migration { - [Migration(214)] - public class add_blurary576p_quality_in_profiles : NzbDroneMigrationBase + [Migration(215)] + public class add_blurary576p_quality_in_profiles_with_grouped_blurary480p : NzbDroneMigrationBase { protected override void MainDbUpgrade() { @@ -19,46 +19,46 @@ namespace NzbDrone.Core.Datastore.Migration private void ConvertProfile(IDbConnection conn, IDbTransaction tran) { - var updater = new ProfileUpdater214(conn, tran); + var updater = new ProfileUpdater215(conn, tran); updater.InsertQualityAfter(13, 22); // Group Bluray576p with Bluray480p updater.Commit(); } } - public class Profile214 + public class Profile215 { public int Id { get; set; } public string Name { get; set; } public int Cutoff { get; set; } - public List Items { get; set; } + public List Items { get; set; } } - public class ProfileItem214 + public class ProfileItem215 { [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)] public int Id { get; set; } public string Name { get; set; } public int? Quality { get; set; } - public List Items { get; set; } + public List Items { get; set; } public bool Allowed { get; set; } - public ProfileItem214() + public ProfileItem215() { - Items = new List(); + Items = new List(); } } - public class ProfileUpdater214 + public class ProfileUpdater215 { private readonly IDbConnection _connection; private readonly IDbTransaction _transaction; - private List _profiles; - private HashSet _changedProfiles = new HashSet(); + private List _profiles; + private HashSet _changedProfiles = new HashSet(); - public ProfileUpdater214(IDbConnection conn, IDbTransaction tran) + public ProfileUpdater215(IDbConnection conn, IDbTransaction tran) { _connection = conn; _transaction = tran; @@ -86,11 +86,17 @@ namespace NzbDrone.Core.Datastore.Migration { foreach (var profile in _profiles) { - var findIndex = profile.Items.FindIndex(v => v.Quality == find); + // Don't update if Bluray 576p was already added to the profile in 214 + if (profile.Items.FindIndex(v => v.Quality == quality || v.Items.Any(i => i.Quality == quality)) > -1) + { + continue; + } + + var findIndex = profile.Items.FindIndex(v => v.Quality == find || v.Items.Any(i => i.Quality == find)); if (findIndex > -1) { - profile.Items.Insert(findIndex + 1, new ProfileItem214 + profile.Items.Insert(findIndex + 1, new ProfileItem215 { Quality = quality, Allowed = profile.Items[findIndex].Allowed @@ -101,9 +107,9 @@ namespace NzbDrone.Core.Datastore.Migration } } - private List GetProfiles() + private List GetProfiles() { - var profiles = new List(); + var profiles = new List(); using (var getProfilesCmd = _connection.CreateCommand()) { @@ -114,12 +120,12 @@ namespace NzbDrone.Core.Datastore.Migration { while (profileReader.Read()) { - profiles.Add(new Profile214 + profiles.Add(new Profile215 { Id = profileReader.GetInt32(0), Name = profileReader.GetString(1), Cutoff = profileReader.GetInt32(2), - Items = Json.Deserialize>(profileReader.GetString(3)) + Items = Json.Deserialize>(profileReader.GetString(3)) }); } }