| | | 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 | | var activityLogPath = Path.Combine(dataPath, DbFilename); |
| | 0 | 59 | | if (!File.Exists(activityLogPath)) |
| | | 60 | | { |
| | 0 | 61 | | _logger.LogWarning("{ActivityLogDb} doesn't exist, nothing to migrate", activityLogPath); |
| | 0 | 62 | | return; |
| | | 63 | | } |
| | | 64 | | |
| | 0 | 65 | | using (var connection = new SqliteConnection($"Filename={activityLogPath}")) |
| | | 66 | | { |
| | 0 | 67 | | connection.Open(); |
| | 0 | 68 | | var tableQuery = connection.Query("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='Activ |
| | 0 | 69 | | foreach (var row in tableQuery) |
| | | 70 | | { |
| | 0 | 71 | | if (row.GetInt32(0) == 0) |
| | | 72 | | { |
| | 0 | 73 | | _logger.LogWarning("Table 'ActivityLog' doesn't exist in {ActivityLogPath}, nothing to migrate", |
| | 0 | 74 | | return; |
| | | 75 | | } |
| | | 76 | | } |
| | | 77 | | |
| | 0 | 78 | | using var userDbConnection = new SqliteConnection($"Filename={Path.Combine(dataPath, "users.db")}"); |
| | 0 | 79 | | userDbConnection.Open(); |
| | 0 | 80 | | _logger.LogWarning("Migrating the activity database may take a while, do not stop Jellyfin."); |
| | 0 | 81 | | using var dbContext = _provider.CreateDbContext(); |
| | | 82 | | |
| | | 83 | | // Make sure that the database is empty in case of failed migration due to power outages, etc. |
| | 0 | 84 | | dbContext.ActivityLogs.RemoveRange(dbContext.ActivityLogs); |
| | 0 | 85 | | dbContext.SaveChanges(); |
| | | 86 | | // Reset the autoincrement counter |
| | 0 | 87 | | dbContext.Database.ExecuteSqlRaw("UPDATE sqlite_sequence SET seq = 0 WHERE name = 'ActivityLog';"); |
| | 0 | 88 | | dbContext.SaveChanges(); |
| | | 89 | | |
| | 0 | 90 | | var newEntries = new List<ActivityLog>(); |
| | | 91 | | |
| | 0 | 92 | | var queryResult = connection.Query("SELECT * FROM ActivityLog ORDER BY Id"); |
| | | 93 | | |
| | 0 | 94 | | foreach (var entry in queryResult) |
| | | 95 | | { |
| | 0 | 96 | | if (!logLevelDictionary.TryGetValue(entry.GetString(8), out var severity)) |
| | | 97 | | { |
| | 0 | 98 | | severity = LogLevel.Trace; |
| | | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | var guid = Guid.Empty; |
| | 0 | 102 | | if (!entry.IsDBNull(6) && !entry.TryGetGuid(6, out guid)) |
| | | 103 | | { |
| | 0 | 104 | | var id = entry.GetString(6); |
| | | 105 | | // This is not a valid Guid, see if it is an internal ID from an old Emby schema |
| | 0 | 106 | | _logger.LogWarning("Invalid Guid in UserId column: {Guid}", id); |
| | | 107 | | |
| | 0 | 108 | | using var statement = userDbConnection.PrepareStatement("SELECT guid FROM LocalUsersv2 WHERE Id= |
| | 0 | 109 | | statement.TryBind("@Id", id); |
| | | 110 | | |
| | 0 | 111 | | using var reader = statement.ExecuteReader(); |
| | 0 | 112 | | if (reader.HasRows && reader.Read() && reader.TryGetGuid(0, out guid)) |
| | | 113 | | { |
| | | 114 | | // Successfully parsed a Guid from the user table. |
| | 0 | 115 | | break; |
| | | 116 | | } |
| | | 117 | | } |
| | | 118 | | |
| | 0 | 119 | | var newEntry = new ActivityLog(entry.GetString(1), entry.GetString(4), guid) |
| | 0 | 120 | | { |
| | 0 | 121 | | DateCreated = entry.GetDateTime(7), |
| | 0 | 122 | | LogSeverity = severity |
| | 0 | 123 | | }; |
| | | 124 | | |
| | 0 | 125 | | if (entry.TryGetString(2, out var result)) |
| | | 126 | | { |
| | 0 | 127 | | newEntry.Overview = result; |
| | | 128 | | } |
| | | 129 | | |
| | 0 | 130 | | if (entry.TryGetString(3, out result)) |
| | | 131 | | { |
| | 0 | 132 | | newEntry.ShortOverview = result; |
| | | 133 | | } |
| | | 134 | | |
| | 0 | 135 | | if (entry.TryGetString(5, out result)) |
| | | 136 | | { |
| | 0 | 137 | | newEntry.ItemId = result; |
| | | 138 | | } |
| | | 139 | | |
| | 0 | 140 | | newEntries.Add(newEntry); |
| | | 141 | | } |
| | | 142 | | |
| | 0 | 143 | | dbContext.ActivityLogs.AddRange(newEntries); |
| | 0 | 144 | | dbContext.SaveChanges(); |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | try |
| | | 148 | | { |
| | 0 | 149 | | File.Move(Path.Combine(dataPath, DbFilename), Path.Combine(dataPath, DbFilename + ".old")); |
| | | 150 | | |
| | 0 | 151 | | var journalPath = Path.Combine(dataPath, DbFilename + "-journal"); |
| | 0 | 152 | | if (File.Exists(journalPath)) |
| | | 153 | | { |
| | 0 | 154 | | File.Move(journalPath, Path.Combine(dataPath, DbFilename + ".old-journal")); |
| | | 155 | | } |
| | 0 | 156 | | } |
| | 0 | 157 | | catch (IOException e) |
| | | 158 | | { |
| | 0 | 159 | | _logger.LogError(e, "Error renaming legacy activity log database to 'activitylog.db.old'"); |
| | 0 | 160 | | } |
| | 0 | 161 | | } |
| | | 162 | | } |
| | | 163 | | } |