Browse Source

remove orphan media items and prevent duplicate paths

pull/31/head
Jason Dove 6 years ago
parent
commit
510a13ffd2
  1. 3
      ErsatzTV.Infrastructure/Data/Configurations/MediaItem/MediaItemConfiguration.cs
  2. 1364
      ErsatzTV.Infrastructure/Migrations/20210227001213_Add_MediaItemPathIndex.Designer.cs
  3. 41
      ErsatzTV.Infrastructure/Migrations/20210227001213_Add_MediaItemPathIndex.cs
  4. 3
      ErsatzTV.Infrastructure/Migrations/TvContextModelSnapshot.cs

3
ErsatzTV.Infrastructure/Data/Configurations/MediaItem/MediaItemConfiguration.cs

@ -11,6 +11,9 @@ namespace ErsatzTV.Infrastructure.Data.Configurations @@ -11,6 +11,9 @@ namespace ErsatzTV.Infrastructure.Data.Configurations
builder.ToTable("MediaItem");
builder.OwnsOne(c => c.Statistics).WithOwner();
builder.HasIndex(i => i.Path)
.IsUnique();
}
}
}

1364
ErsatzTV.Infrastructure/Migrations/20210227001213_Add_MediaItemPathIndex.Designer.cs generated

File diff suppressed because it is too large Load Diff

41
ErsatzTV.Infrastructure/Migrations/20210227001213_Add_MediaItemPathIndex.cs

@ -0,0 +1,41 @@ @@ -0,0 +1,41 @@
using Microsoft.EntityFrameworkCore.Migrations;
namespace ErsatzTV.Infrastructure.Migrations
{
public partial class Add_MediaItemPathIndex : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
// delete orphan movies, if any exist
migrationBuilder.Sql(
@"DELETE FROM MediaItem WHERE Id IN
(SELECT mi.Id FROM Library l
INNER JOIN LibraryPath lp ON l.Id = lp.LibraryId
INNER JOIN MediaItem mi ON lp.Id = mi.LibraryPathId
LEFT OUTER JOIN Movie m ON mi.Id = m.Id
WHERE l.MediaKind = 1 AND m.Id IS NULL)");
// delete orphan episodes, if any exist
migrationBuilder.Sql(
@"DELETE FROM MediaItem WHERE Id IN
(SELECT mi.Id FROM Library l
INNER JOIN LibraryPath lp ON l.Id = lp.LibraryId
INNER JOIN MediaItem mi ON lp.Id = mi.LibraryPathId
LEFT OUTER JOIN Show s ON mi.Id = s.Id
LEFT OUTER JOIN Season ssn ON mi.Id = ssn.Id
LEFT OUTER JOIN Episode e ON mi.Id = e.Id
WHERE l.MediaKind = 2 AND s.Id IS NULL AND ssn.Id IS NULL AND e.Id IS NULL)");
migrationBuilder.CreateIndex(
"IX_MediaItem_Path",
"MediaItem",
"Path",
unique: true);
}
protected override void Down(MigrationBuilder migrationBuilder) =>
migrationBuilder.DropIndex(
"IX_MediaItem_Path",
"MediaItem");
}
}

3
ErsatzTV.Infrastructure/Migrations/TvContextModelSnapshot.cs

@ -326,6 +326,9 @@ namespace ErsatzTV.Infrastructure.Migrations @@ -326,6 +326,9 @@ namespace ErsatzTV.Infrastructure.Migrations
b.HasIndex("LibraryPathId");
b.HasIndex("Path")
.IsUnique();
b.ToTable("MediaItem");
});

Loading…
Cancel
Save