< Summary - Jellyfin

Information
Class: Jellyfin.Server.Migrations.Routines.MigrateActivityLogDb
Assembly: jellyfin
File(s): /srv/git/jellyfin/Jellyfin.Server/Migrations/Routines/MigrateActivityLogDb.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 73
Coverable lines: 73
Total lines: 163
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 28
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 8/14/2025 - 12:11:05 AM Line coverage: 0% (0/64) Branch coverage: 0% (0/22) Total lines: 14710/28/2025 - 12:11:27 AM Line coverage: 0% (0/73) Branch coverage: 0% (0/28) Total lines: 163 8/14/2025 - 12:11:05 AM Line coverage: 0% (0/64) Branch coverage: 0% (0/22) Total lines: 14710/28/2025 - 12:11:27 AM Line coverage: 0% (0/73) Branch coverage: 0% (0/28) Total lines: 163

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
Perform()0%812280%

File(s)

/srv/git/jellyfin/Jellyfin.Server/Migrations/Routines/MigrateActivityLogDb.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.IO;
 4using Emby.Server.Implementations.Data;
 5using Jellyfin.Database.Implementations;
 6using Jellyfin.Database.Implementations.Entities;
 7using MediaBrowser.Controller;
 8using Microsoft.Data.Sqlite;
 9using Microsoft.EntityFrameworkCore;
 10using Microsoft.Extensions.Logging;
 11
 12namespace Jellyfin.Server.Migrations.Routines
 13{
 14    /// <summary>
 15    /// The migration routine for migrating the activity log database to EF Core.
 16    /// </summary>
 17#pragma warning disable CS0618 // Type or member is obsolete
 18    [JellyfinMigration("2025-04-20T07:00:00", nameof(MigrateActivityLogDb), "3793eb59-bc8c-456c-8b9f-bd5a62a42978")]
 19    public class MigrateActivityLogDb : IMigrationRoutine
 20#pragma warning restore CS0618 // Type or member is obsolete
 21    {
 22        private const string DbFilename = "activitylog.db";
 23
 24        private readonly ILogger<MigrateActivityLogDb> _logger;
 25        private readonly IDbContextFactory<JellyfinDbContext> _provider;
 26        private readonly IServerApplicationPaths _paths;
 27
 28        /// <summary>
 29        /// Initializes a new instance of the <see cref="MigrateActivityLogDb"/> class.
 30        /// </summary>
 31        /// <param name="logger">The logger.</param>
 32        /// <param name="paths">The server application paths.</param>
 33        /// <param name="provider">The database provider.</param>
 34        public MigrateActivityLogDb(ILogger<MigrateActivityLogDb> logger, IServerApplicationPaths paths, IDbContextFacto
 35        {
 036            _logger = logger;
 037            _provider = provider;
 038            _paths = paths;
 039        }
 40
 41        /// <inheritdoc/>
 42        public void Perform()
 43        {
 044            var logLevelDictionary = new Dictionary<string, LogLevel>(StringComparer.OrdinalIgnoreCase)
 045            {
 046                { "None", LogLevel.None },
 047                { "Trace", LogLevel.Trace },
 048                { "Debug", LogLevel.Debug },
 049                { "Information", LogLevel.Information },
 050                { "Info", LogLevel.Information },
 051                { "Warn", LogLevel.Warning },
 052                { "Warning", LogLevel.Warning },
 053                { "Error", LogLevel.Error },
 054                { "Critical", LogLevel.Critical }
 055            };
 56
 057            var dataPath = _paths.DataPath;
 058            var activityLogPath = Path.Combine(dataPath, DbFilename);
 059            if (!File.Exists(activityLogPath))
 60            {
 061                _logger.LogWarning("{ActivityLogDb} doesn't exist, nothing to migrate", activityLogPath);
 062                return;
 63            }
 64
 065            using (var connection = new SqliteConnection($"Filename={activityLogPath}"))
 66            {
 067                connection.Open();
 068                var tableQuery = connection.Query("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='Activ
 069                foreach (var row in tableQuery)
 70                {
 071                    if (row.GetInt32(0) == 0)
 72                    {
 073                        _logger.LogWarning("Table 'ActivityLog' doesn't exist in {ActivityLogPath}, nothing to migrate",
 074                        return;
 75                    }
 76                }
 77
 078                using var userDbConnection = new SqliteConnection($"Filename={Path.Combine(dataPath, "users.db")}");
 079                userDbConnection.Open();
 080                _logger.LogWarning("Migrating the activity database may take a while, do not stop Jellyfin.");
 081                using var dbContext = _provider.CreateDbContext();
 82
 83                // Make sure that the database is empty in case of failed migration due to power outages, etc.
 084                dbContext.ActivityLogs.RemoveRange(dbContext.ActivityLogs);
 085                dbContext.SaveChanges();
 86                // Reset the autoincrement counter
 087                dbContext.Database.ExecuteSqlRaw("UPDATE sqlite_sequence SET seq = 0 WHERE name = 'ActivityLog';");
 088                dbContext.SaveChanges();
 89
 090                var newEntries = new List<ActivityLog>();
 91
 092                var queryResult = connection.Query("SELECT * FROM ActivityLog ORDER BY Id");
 93
 094                foreach (var entry in queryResult)
 95                {
 096                    if (!logLevelDictionary.TryGetValue(entry.GetString(8), out var severity))
 97                    {
 098                        severity = LogLevel.Trace;
 99                    }
 100
 0101                    var guid = Guid.Empty;
 0102                    if (!entry.IsDBNull(6) && !entry.TryGetGuid(6, out guid))
 103                    {
 0104                        var id = entry.GetString(6);
 105                        // This is not a valid Guid, see if it is an internal ID from an old Emby schema
 0106                        _logger.LogWarning("Invalid Guid in UserId column: {Guid}", id);
 107
 0108                        using var statement = userDbConnection.PrepareStatement("SELECT guid FROM LocalUsersv2 WHERE Id=
 0109                        statement.TryBind("@Id", id);
 110
 0111                        using var reader = statement.ExecuteReader();
 0112                        if (reader.HasRows && reader.Read() && reader.TryGetGuid(0, out guid))
 113                        {
 114                            // Successfully parsed a Guid from the user table.
 0115                            break;
 116                        }
 117                    }
 118
 0119                    var newEntry = new ActivityLog(entry.GetString(1), entry.GetString(4), guid)
 0120                    {
 0121                        DateCreated = entry.GetDateTime(7),
 0122                        LogSeverity = severity
 0123                    };
 124
 0125                    if (entry.TryGetString(2, out var result))
 126                    {
 0127                        newEntry.Overview = result;
 128                    }
 129
 0130                    if (entry.TryGetString(3, out result))
 131                    {
 0132                        newEntry.ShortOverview = result;
 133                    }
 134
 0135                    if (entry.TryGetString(5, out result))
 136                    {
 0137                        newEntry.ItemId = result;
 138                    }
 139
 0140                    newEntries.Add(newEntry);
 141                }
 142
 0143                dbContext.ActivityLogs.AddRange(newEntries);
 0144                dbContext.SaveChanges();
 145            }
 146
 147            try
 148            {
 0149                File.Move(Path.Combine(dataPath, DbFilename), Path.Combine(dataPath, DbFilename + ".old"));
 150
 0151                var journalPath = Path.Combine(dataPath, DbFilename + "-journal");
 0152                if (File.Exists(journalPath))
 153                {
 0154                    File.Move(journalPath, Path.Combine(dataPath, DbFilename + ".old-journal"));
 155                }
 0156            }
 0157            catch (IOException e)
 158            {
 0159                _logger.LogError(e, "Error renaming legacy activity log database to 'activitylog.db.old'");
 0160            }
 0161        }
 162    }
 163}