< Summary - Jellyfin

Information
Class: Jellyfin.Server.Migrations.Routines.RemoveDuplicateExtras
Assembly: jellyfin
File(s): /srv/git/jellyfin/Jellyfin.Server/Migrations/Routines/20250420080000_RemoveDuplicateExtras.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 29
Coverable lines: 29
Total lines: 83
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 5/31/2026 - 12:15:44 AM Line coverage: 0% (0/27) Branch coverage: 0% (0/4) Total lines: 7611/13/2026 - 12:19:51 AM Line coverage: 0% (0/29) Branch coverage: 0% (0/4) Total lines: 83 5/31/2026 - 12:15:44 AM Line coverage: 0% (0/27) Branch coverage: 0% (0/4) Total lines: 7611/13/2026 - 12:19:51 AM Line coverage: 0% (0/29) Branch coverage: 0% (0/4) Total lines: 83

Coverage delta

Coverage delta 1 -1

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
PerformAsync(...)100%210%
PerformCore()0%2040%

File(s)

/srv/git/jellyfin/Jellyfin.Server/Migrations/Routines/20250420080000_RemoveDuplicateExtras.cs

#LineLine coverage
 1using System;
 2using System.Globalization;
 3using System.IO;
 4using System.Linq;
 5using System.Threading;
 6using System.Threading.Tasks;
 7using Emby.Server.Implementations.Data;
 8using MediaBrowser.Controller;
 9using Microsoft.Data.Sqlite;
 10using Microsoft.Extensions.Logging;
 11
 12namespace Jellyfin.Server.Migrations.Routines;
 13
 14/// <summary>
 15/// Remove duplicate entries which were caused by a bug where a file was considered to be an "Extra" to itself.
 16/// </summary>
 17[JellyfinMigration("2025-04-20T08:00:00", nameof(RemoveDuplicateExtras), "ACBE17B7-8435-4A83-8B64-6FCF162CB9BD")]
 18internal class RemoveDuplicateExtras : IAsyncMigrationRoutine
 19{
 20    private const string DbFilename = "library.db";
 21    private readonly ILogger<RemoveDuplicateExtras> _logger;
 22    private readonly IServerApplicationPaths _paths;
 23
 24    public RemoveDuplicateExtras(ILogger<RemoveDuplicateExtras> logger, IServerApplicationPaths paths)
 25    {
 026        _logger = logger;
 027        _paths = paths;
 028    }
 29
 30    /// <inheritdoc />
 31    public Task PerformAsync(CancellationToken cancellationToken)
 32    {
 33        // This routine predates the async interface and has not been ported to async database access yet.
 034        PerformCore();
 035        return Task.CompletedTask;
 36    }
 37
 38    private void PerformCore()
 39    {
 040        var dataPath = _paths.DataPath;
 041        var dbPath = Path.Combine(dataPath, DbFilename);
 042        using var connection = new SqliteConnection($"Filename={dbPath}");
 043        connection.Open();
 044        using (var transaction = connection.BeginTransaction())
 45        {
 46            // Query the database for the ids of duplicate extras
 047            var queryResult = connection.Query("SELECT t1.Path FROM TypedBaseItems AS t1, TypedBaseItems AS t2 WHERE t1.
 048            var bads = string.Join(", ", queryResult.Select(x => x.GetString(0)));
 49
 50            // Do nothing if no duplicate extras were detected
 051            if (bads.Length == 0)
 52            {
 053                _logger.LogInformation("No duplicate extras detected, skipping migration.");
 054                return;
 55            }
 56
 57            // Back up the database before deleting any entries
 058            for (int i = 1; ; i++)
 59            {
 060                var bakPath = string.Format(CultureInfo.InvariantCulture, "{0}.bak{1}", dbPath, i);
 061                if (!File.Exists(bakPath))
 62                {
 63                    try
 64                    {
 065                        File.Copy(dbPath, bakPath);
 066                        _logger.LogInformation("Library database backed up to {BackupPath}", bakPath);
 067                        break;
 68                    }
 069                    catch (Exception ex)
 70                    {
 071                        _logger.LogError(ex, "Cannot make a backup of {Library} at path {BackupPath}", DbFilename, bakPa
 072                        throw;
 73                    }
 74                }
 75            }
 76
 77            // Delete all duplicate extras
 078            _logger.LogInformation("Removing found duplicated extras for the following items: {DuplicateExtras}", bads);
 079            connection.Execute("DELETE FROM TypedBaseItems WHERE rowid IN (SELECT t1.rowid FROM TypedBaseItems AS t1, Ty
 080            transaction.Commit();
 081        }
 082    }
 83}