Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed: Prevent two Artists pointing to same ArtistMetadata
- Loading branch information
Showing
5 changed files
with
180 additions
and
4 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
src/NzbDrone.Core.Test/Datastore/Migration/031_add_artistmetadataid_constraintFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
using NUnit.Framework; | ||
using NzbDrone.Core.Test.Framework; | ||
using NzbDrone.Core.Datastore.Migration; | ||
using NzbDrone.Test.Common; | ||
using System.Linq; | ||
using FluentAssertions; | ||
using System.Collections.Generic; | ||
|
||
namespace NzbDrone.Core.Test.Datastore.Migration | ||
{ | ||
[TestFixture] | ||
public class add_artistmetadataid_constraintFixture : MigrationTest<add_artistmetadataid_constraint> | ||
{ | ||
private string _artistPath = null; | ||
|
||
private void GivenArtistMetadata(add_artistmetadataid_constraint c, int id, string name) | ||
{ | ||
c.Insert.IntoTable("ArtistMetadata").Row(new | ||
{ | ||
Id = id, | ||
ForeignArtistId = id, | ||
Name = name, | ||
Status = 1, | ||
Images = "images" | ||
}); | ||
} | ||
|
||
private void GivenArtist(add_artistmetadataid_constraint c, int id, int artistMetadataId, string name) | ||
{ | ||
_artistPath = $"/mnt/data/path/{name}".AsOsAgnostic(); | ||
c.Insert.IntoTable("Artists").Row(new | ||
{ | ||
Id = id, | ||
ArtistMetadataId = artistMetadataId, | ||
CleanName = name, | ||
Path = _artistPath, | ||
Monitored = 1, | ||
AlbumFolder = 1, | ||
LanguageProfileId = 1, | ||
MetadataProfileId = 1, | ||
}); | ||
} | ||
|
||
private void VerifyArtists(IDirectDataMapper db, List<int> ids) | ||
{ | ||
var artists = db.Query("SELECT Artists.* from Artists"); | ||
|
||
artists.Select(x => x["Id"]).ShouldBeEquivalentTo(ids); | ||
|
||
var duplicates = artists.GroupBy(x => x["ArtistMetadataId"]) | ||
.Where(x => x.Count() > 1); | ||
|
||
duplicates.Should().BeEmpty(); | ||
} | ||
|
||
[Test] | ||
public void migration_031_should_not_remove_unique_artist() | ||
{ | ||
var db = WithMigrationTestDb(c => { | ||
GivenArtistMetadata(c, 1, "test"); | ||
GivenArtist(c, 1, 1, "test"); | ||
}); | ||
|
||
VerifyArtists(db, new List<int> { 1 }); | ||
} | ||
|
||
[Test] | ||
public void migration_031_should_not_remove_either_unique_artist() | ||
{ | ||
var db = WithMigrationTestDb(c => { | ||
GivenArtistMetadata(c, 1, "test"); | ||
GivenArtist(c, 1, 1, "test"); | ||
GivenArtistMetadata(c, 2, "test2"); | ||
GivenArtist(c, 2, 2, "test2"); | ||
}); | ||
|
||
VerifyArtists(db, new List<int> { 1, 2 }); | ||
} | ||
|
||
[Test] | ||
public void migration_031_should_remove_duplicate_artist() | ||
{ | ||
var db = WithMigrationTestDb(c => { | ||
GivenArtistMetadata(c, 1, "test"); | ||
GivenArtist(c, 1, 1, "test"); | ||
GivenArtist(c, 2, 1, "test2"); | ||
}); | ||
|
||
VerifyArtists(db, new List<int> { 1 }); | ||
} | ||
|
||
[Test] | ||
public void migration_031_should_remove_all_duplicate_artists() | ||
{ | ||
var db = WithMigrationTestDb(c => { | ||
GivenArtistMetadata(c, 1, "test"); | ||
GivenArtist(c, 1, 1, "test"); | ||
GivenArtist(c, 2, 1, "test"); | ||
GivenArtist(c, 3, 1, "test"); | ||
GivenArtist(c, 4, 1, "test"); | ||
GivenArtistMetadata(c, 2, "test2"); | ||
GivenArtist(c, 5, 2, "test2"); | ||
GivenArtist(c, 6, 2, "test2"); | ||
}); | ||
|
||
VerifyArtists(db, new List<int> { 1, 5 }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/NzbDrone.Core/Datastore/Migration/031_add_artistmetadataid_constraint.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using FluentMigrator; | ||
using NzbDrone.Core.Datastore.Migration.Framework; | ||
|
||
namespace NzbDrone.Core.Datastore.Migration | ||
{ | ||
[Migration(31)] | ||
public class add_artistmetadataid_constraint : NzbDroneMigrationBase | ||
{ | ||
protected override void MainDbUpgrade() | ||
{ | ||
// Remove any duplicate artists | ||
Execute.Sql(@"DELETE FROM Artists | ||
WHERE Id NOT IN ( | ||
SELECT MIN(Artists.id) from Artists | ||
JOIN ArtistMetadata ON Artists.ArtistMetadataId = ArtistMetadata.Id | ||
GROUP BY ArtistMetadata.Id)"); | ||
|
||
// The index exists but will be recreated as part of unique constraint | ||
Delete.Index().OnTable("Artists").OnColumn("ArtistMetadataId"); | ||
|
||
// Add a constraint to prevent any more duplicates | ||
Alter.Column("ArtistMetadataId").OnTable("Artists").AsInt32().Unique(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters