| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.IO; |
| | 4 | | using Emby.Server.Implementations.Data; |
| | 5 | | using Jellyfin.Database.Implementations; |
| | 6 | | using Jellyfin.Database.Implementations.Entities; |
| | 7 | | using MediaBrowser.Controller; |
| | 8 | | using Microsoft.Data.Sqlite; |
| | 9 | | using Microsoft.EntityFrameworkCore; |
| | 10 | | using Microsoft.Extensions.Logging; |
| | 11 | |
|
| | 12 | | namespace 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 | | { |
| 0 | 36 | | _logger = logger; |
| 0 | 37 | | _provider = provider; |
| 0 | 38 | | _paths = paths; |
| 0 | 39 | | } |
| | 40 | |
|
| | 41 | | /// <inheritdoc/> |
| | 42 | | public void Perform() |
| | 43 | | { |
| 0 | 44 | | var logLevelDictionary = new Dictionary<string, LogLevel>(StringComparer.OrdinalIgnoreCase) |
| 0 | 45 | | { |
| 0 | 46 | | { "None", LogLevel.None }, |
| 0 | 47 | | { "Trace", LogLevel.Trace }, |
| 0 | 48 | | { "Debug", LogLevel.Debug }, |
| 0 | 49 | | { "Information", LogLevel.Information }, |
| 0 | 50 | | { "Info", LogLevel.Information }, |
| 0 | 51 | | { "Warn", LogLevel.Warning }, |
| 0 | 52 | | { "Warning", LogLevel.Warning }, |
| 0 | 53 | | { "Error", LogLevel.Error }, |
| 0 | 54 | | { "Critical", LogLevel.Critical } |
| 0 | 55 | | }; |
| | 56 | |
|
| 0 | 57 | | var dataPath = _paths.DataPath; |
| 0 | 58 | | using (var connection = new SqliteConnection($"Filename={Path.Combine(dataPath, DbFilename)}")) |
| | 59 | | { |
| 0 | 60 | | connection.Open(); |
| | 61 | |
|
| 0 | 62 | | using var userDbConnection = new SqliteConnection($"Filename={Path.Combine(dataPath, "users.db")}"); |
| 0 | 63 | | userDbConnection.Open(); |
| 0 | 64 | | _logger.LogWarning("Migrating the activity database may take a while, do not stop Jellyfin."); |
| 0 | 65 | | using var dbContext = _provider.CreateDbContext(); |
| | 66 | |
|
| | 67 | | // Make sure that the database is empty in case of failed migration due to power outages, etc. |
| 0 | 68 | | dbContext.ActivityLogs.RemoveRange(dbContext.ActivityLogs); |
| 0 | 69 | | dbContext.SaveChanges(); |
| | 70 | | // Reset the autoincrement counter |
| 0 | 71 | | dbContext.Database.ExecuteSqlRaw("UPDATE sqlite_sequence SET seq = 0 WHERE name = 'ActivityLog';"); |
| 0 | 72 | | dbContext.SaveChanges(); |
| | 73 | |
|
| 0 | 74 | | var newEntries = new List<ActivityLog>(); |
| | 75 | |
|
| 0 | 76 | | var queryResult = connection.Query("SELECT * FROM ActivityLog ORDER BY Id"); |
| | 77 | |
|
| 0 | 78 | | foreach (var entry in queryResult) |
| | 79 | | { |
| 0 | 80 | | if (!logLevelDictionary.TryGetValue(entry.GetString(8), out var severity)) |
| | 81 | | { |
| 0 | 82 | | severity = LogLevel.Trace; |
| | 83 | | } |
| | 84 | |
|
| 0 | 85 | | var guid = Guid.Empty; |
| 0 | 86 | | if (!entry.IsDBNull(6) && !entry.TryGetGuid(6, out guid)) |
| | 87 | | { |
| 0 | 88 | | var id = entry.GetString(6); |
| | 89 | | // This is not a valid Guid, see if it is an internal ID from an old Emby schema |
| 0 | 90 | | _logger.LogWarning("Invalid Guid in UserId column: {Guid}", id); |
| | 91 | |
|
| 0 | 92 | | using var statement = userDbConnection.PrepareStatement("SELECT guid FROM LocalUsersv2 WHERE Id= |
| 0 | 93 | | statement.TryBind("@Id", id); |
| | 94 | |
|
| 0 | 95 | | using var reader = statement.ExecuteReader(); |
| 0 | 96 | | if (reader.HasRows && reader.Read() && reader.TryGetGuid(0, out guid)) |
| | 97 | | { |
| | 98 | | // Successfully parsed a Guid from the user table. |
| 0 | 99 | | break; |
| | 100 | | } |
| | 101 | | } |
| | 102 | |
|
| 0 | 103 | | var newEntry = new ActivityLog(entry.GetString(1), entry.GetString(4), guid) |
| 0 | 104 | | { |
| 0 | 105 | | DateCreated = entry.GetDateTime(7), |
| 0 | 106 | | LogSeverity = severity |
| 0 | 107 | | }; |
| | 108 | |
|
| 0 | 109 | | if (entry.TryGetString(2, out var result)) |
| | 110 | | { |
| 0 | 111 | | newEntry.Overview = result; |
| | 112 | | } |
| | 113 | |
|
| 0 | 114 | | if (entry.TryGetString(3, out result)) |
| | 115 | | { |
| 0 | 116 | | newEntry.ShortOverview = result; |
| | 117 | | } |
| | 118 | |
|
| 0 | 119 | | if (entry.TryGetString(5, out result)) |
| | 120 | | { |
| 0 | 121 | | newEntry.ItemId = result; |
| | 122 | | } |
| | 123 | |
|
| 0 | 124 | | newEntries.Add(newEntry); |
| | 125 | | } |
| | 126 | |
|
| 0 | 127 | | dbContext.ActivityLogs.AddRange(newEntries); |
| 0 | 128 | | dbContext.SaveChanges(); |
| | 129 | | } |
| | 130 | |
|
| | 131 | | try |
| | 132 | | { |
| 0 | 133 | | File.Move(Path.Combine(dataPath, DbFilename), Path.Combine(dataPath, DbFilename + ".old")); |
| | 134 | |
|
| 0 | 135 | | var journalPath = Path.Combine(dataPath, DbFilename + "-journal"); |
| 0 | 136 | | if (File.Exists(journalPath)) |
| | 137 | | { |
| 0 | 138 | | File.Move(journalPath, Path.Combine(dataPath, DbFilename + ".old-journal")); |
| | 139 | | } |
| 0 | 140 | | } |
| 0 | 141 | | catch (IOException e) |
| | 142 | | { |
| 0 | 143 | | _logger.LogError(e, "Error renaming legacy activity log database to 'activitylog.db.old'"); |
| 0 | 144 | | } |
| 0 | 145 | | } |
| | 146 | | } |
| | 147 | | } |