| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.IO; |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using Emby.Server.Implementations.Data; |
| | | 7 | | using Jellyfin.Database.Implementations; |
| | | 8 | | using Jellyfin.Database.Implementations.Entities; |
| | | 9 | | using MediaBrowser.Controller; |
| | | 10 | | using Microsoft.Data.Sqlite; |
| | | 11 | | using Microsoft.EntityFrameworkCore; |
| | | 12 | | using Microsoft.Extensions.Logging; |
| | | 13 | | |
| | | 14 | | namespace Jellyfin.Server.Migrations.Routines |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// The migration routine for migrating the activity log database to EF Core. |
| | | 18 | | /// </summary> |
| | | 19 | | [JellyfinMigration("2025-04-20T07:00:00", nameof(MigrateActivityLogDb), "3793eb59-bc8c-456c-8b9f-bd5a62a42978")] |
| | | 20 | | public class MigrateActivityLogDb : IAsyncMigrationRoutine |
| | | 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 Task PerformAsync(CancellationToken cancellationToken) |
| | | 43 | | { |
| | | 44 | | // This routine predates the async interface and has not been ported to async database access yet. |
| | 0 | 45 | | PerformCore(); |
| | 0 | 46 | | return Task.CompletedTask; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | private void PerformCore() |
| | | 50 | | { |
| | 0 | 51 | | var logLevelDictionary = new Dictionary<string, LogLevel>(StringComparer.OrdinalIgnoreCase) |
| | 0 | 52 | | { |
| | 0 | 53 | | { "None", LogLevel.None }, |
| | 0 | 54 | | { "Trace", LogLevel.Trace }, |
| | 0 | 55 | | { "Debug", LogLevel.Debug }, |
| | 0 | 56 | | { "Information", LogLevel.Information }, |
| | 0 | 57 | | { "Info", LogLevel.Information }, |
| | 0 | 58 | | { "Warn", LogLevel.Warning }, |
| | 0 | 59 | | { "Warning", LogLevel.Warning }, |
| | 0 | 60 | | { "Error", LogLevel.Error }, |
| | 0 | 61 | | { "Critical", LogLevel.Critical } |
| | 0 | 62 | | }; |
| | | 63 | | |
| | 0 | 64 | | var dataPath = _paths.DataPath; |
| | 0 | 65 | | var activityLogPath = Path.Combine(dataPath, DbFilename); |
| | 0 | 66 | | if (!File.Exists(activityLogPath)) |
| | | 67 | | { |
| | 0 | 68 | | _logger.LogWarning("{ActivityLogDb} doesn't exist, nothing to migrate", activityLogPath); |
| | 0 | 69 | | return; |
| | | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | using (var connection = new SqliteConnection($"Filename={activityLogPath}")) |
| | | 73 | | { |
| | 0 | 74 | | connection.Open(); |
| | 0 | 75 | | var tableQuery = connection.Query("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='Activ |
| | 0 | 76 | | foreach (var row in tableQuery) |
| | | 77 | | { |
| | 0 | 78 | | if (row.GetInt32(0) == 0) |
| | | 79 | | { |
| | 0 | 80 | | _logger.LogWarning("Table 'ActivityLog' doesn't exist in {ActivityLogPath}, nothing to migrate", |
| | 0 | 81 | | return; |
| | | 82 | | } |
| | | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | using var userDbConnection = new SqliteConnection($"Filename={Path.Combine(dataPath, "users.db")}"); |
| | 0 | 86 | | userDbConnection.Open(); |
| | 0 | 87 | | _logger.LogWarning("Migrating the activity database may take a while, do not stop Jellyfin."); |
| | 0 | 88 | | using var dbContext = _provider.CreateDbContext(); |
| | | 89 | | |
| | | 90 | | // Make sure that the database is empty in case of failed migration due to power outages, etc. |
| | 0 | 91 | | dbContext.ActivityLogs.RemoveRange(dbContext.ActivityLogs); |
| | 0 | 92 | | dbContext.SaveChanges(); |
| | | 93 | | // Reset the autoincrement counter |
| | 0 | 94 | | dbContext.Database.ExecuteSqlRaw("UPDATE sqlite_sequence SET seq = 0 WHERE name = 'ActivityLog';"); |
| | 0 | 95 | | dbContext.SaveChanges(); |
| | | 96 | | |
| | 0 | 97 | | var newEntries = new List<ActivityLog>(); |
| | | 98 | | |
| | 0 | 99 | | var queryResult = connection.Query("SELECT * FROM ActivityLog ORDER BY Id"); |
| | | 100 | | |
| | 0 | 101 | | foreach (var entry in queryResult) |
| | | 102 | | { |
| | 0 | 103 | | if (!logLevelDictionary.TryGetValue(entry.GetString(8), out var severity)) |
| | | 104 | | { |
| | 0 | 105 | | severity = LogLevel.Trace; |
| | | 106 | | } |
| | | 107 | | |
| | 0 | 108 | | var guid = Guid.Empty; |
| | 0 | 109 | | if (!entry.IsDBNull(6) && !entry.TryGetGuid(6, out guid)) |
| | | 110 | | { |
| | 0 | 111 | | var id = entry.GetString(6); |
| | | 112 | | // This is not a valid Guid, see if it is an internal ID from an old Emby schema |
| | 0 | 113 | | _logger.LogWarning("Invalid Guid in UserId column: {Guid}", id); |
| | | 114 | | |
| | 0 | 115 | | using var statement = userDbConnection.PrepareStatement("SELECT guid FROM LocalUsersv2 WHERE Id= |
| | 0 | 116 | | statement.TryBind("@Id", id); |
| | | 117 | | |
| | 0 | 118 | | using var reader = statement.ExecuteReader(); |
| | 0 | 119 | | if (reader.HasRows && reader.Read() && reader.TryGetGuid(0, out guid)) |
| | | 120 | | { |
| | | 121 | | // Successfully parsed a Guid from the user table. |
| | 0 | 122 | | break; |
| | | 123 | | } |
| | | 124 | | } |
| | | 125 | | |
| | 0 | 126 | | var newEntry = new ActivityLog(entry.GetString(1), entry.GetString(4), guid) |
| | 0 | 127 | | { |
| | 0 | 128 | | DateCreated = entry.GetDateTime(7), |
| | 0 | 129 | | LogSeverity = severity |
| | 0 | 130 | | }; |
| | | 131 | | |
| | 0 | 132 | | if (entry.TryGetString(2, out var result)) |
| | | 133 | | { |
| | 0 | 134 | | newEntry.Overview = result; |
| | | 135 | | } |
| | | 136 | | |
| | 0 | 137 | | if (entry.TryGetString(3, out result)) |
| | | 138 | | { |
| | 0 | 139 | | newEntry.ShortOverview = result; |
| | | 140 | | } |
| | | 141 | | |
| | 0 | 142 | | if (entry.TryGetString(5, out result)) |
| | | 143 | | { |
| | 0 | 144 | | newEntry.ItemId = result; |
| | | 145 | | } |
| | | 146 | | |
| | 0 | 147 | | newEntries.Add(newEntry); |
| | | 148 | | } |
| | | 149 | | |
| | 0 | 150 | | dbContext.ActivityLogs.AddRange(newEntries); |
| | 0 | 151 | | dbContext.SaveChanges(); |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | try |
| | | 155 | | { |
| | 0 | 156 | | File.Move(Path.Combine(dataPath, DbFilename), Path.Combine(dataPath, DbFilename + ".old")); |
| | | 157 | | |
| | 0 | 158 | | var journalPath = Path.Combine(dataPath, DbFilename + "-journal"); |
| | 0 | 159 | | if (File.Exists(journalPath)) |
| | | 160 | | { |
| | 0 | 161 | | File.Move(journalPath, Path.Combine(dataPath, DbFilename + ".old-journal")); |
| | | 162 | | } |
| | 0 | 163 | | } |
| | 0 | 164 | | catch (IOException e) |
| | | 165 | | { |
| | 0 | 166 | | _logger.LogError(e, "Error renaming legacy activity log database to 'activitylog.db.old'"); |
| | 0 | 167 | | } |
| | 0 | 168 | | } |
| | | 169 | | } |
| | | 170 | | } |