| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Globalization; |
| | | 4 | | using System.IO; |
| | | 5 | | using Emby.Server.Implementations.Data; |
| | | 6 | | using Jellyfin.Database.Implementations; |
| | | 7 | | using Jellyfin.Database.Implementations.Entities.Security; |
| | | 8 | | using MediaBrowser.Controller; |
| | | 9 | | using MediaBrowser.Controller.Library; |
| | | 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 | | /// A migration that moves data from the authentication database into the new schema. |
| | | 18 | | /// </summary> |
| | | 19 | | #pragma warning disable CS0618 // Type or member is obsolete |
| | | 20 | | [JellyfinMigration("2025-04-20T14:00:00", nameof(MigrateAuthenticationDb), "5BD72F41-E6F3-4F60-90AA-09869ABE0E22")] |
| | | 21 | | public class MigrateAuthenticationDb : IMigrationRoutine |
| | | 22 | | #pragma warning restore CS0618 // Type or member is obsolete |
| | | 23 | | { |
| | | 24 | | private const string DbFilename = "authentication.db"; |
| | | 25 | | |
| | | 26 | | private readonly ILogger<MigrateAuthenticationDb> _logger; |
| | | 27 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | | 28 | | private readonly IServerApplicationPaths _appPaths; |
| | | 29 | | private readonly IUserManager _userManager; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Initializes a new instance of the <see cref="MigrateAuthenticationDb"/> class. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="logger">The logger.</param> |
| | | 35 | | /// <param name="dbProvider">The database provider.</param> |
| | | 36 | | /// <param name="appPaths">The server application paths.</param> |
| | | 37 | | /// <param name="userManager">The user manager.</param> |
| | | 38 | | public MigrateAuthenticationDb( |
| | | 39 | | ILogger<MigrateAuthenticationDb> logger, |
| | | 40 | | IDbContextFactory<JellyfinDbContext> dbProvider, |
| | | 41 | | IServerApplicationPaths appPaths, |
| | | 42 | | IUserManager userManager) |
| | | 43 | | { |
| | 0 | 44 | | _logger = logger; |
| | 0 | 45 | | _dbProvider = dbProvider; |
| | 0 | 46 | | _appPaths = appPaths; |
| | 0 | 47 | | _userManager = userManager; |
| | 0 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <inheritdoc /> |
| | | 51 | | public void Perform() |
| | | 52 | | { |
| | 0 | 53 | | var dataPath = _appPaths.DataPath; |
| | 0 | 54 | | var dbFilePath = Path.Combine(dataPath, DbFilename); |
| | | 55 | | |
| | 0 | 56 | | if (!File.Exists(dbFilePath)) |
| | | 57 | | { |
| | 0 | 58 | | _logger.LogWarning("{Path} doesn't exist, nothing to migrate", dbFilePath); |
| | 0 | 59 | | return; |
| | | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | using (var connection = new SqliteConnection($"Filename={dbFilePath}")) |
| | | 63 | | { |
| | 0 | 64 | | connection.Open(); |
| | | 65 | | |
| | 0 | 66 | | var tableQuery = connection.Query("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='Token |
| | 0 | 67 | | foreach (var row in tableQuery) |
| | | 68 | | { |
| | 0 | 69 | | if (row.GetInt32(0) == 0) |
| | | 70 | | { |
| | 0 | 71 | | _logger.LogWarning("Table 'Tokens' doesn't exist in {Path}, nothing to migrate", dbFilePath); |
| | 0 | 72 | | return; |
| | | 73 | | } |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | using var dbContext = _dbProvider.CreateDbContext(); |
| | | 77 | | |
| | 0 | 78 | | var authenticatedDevices = connection.Query("SELECT * FROM Tokens"); |
| | | 79 | | |
| | 0 | 80 | | foreach (var row in authenticatedDevices) |
| | | 81 | | { |
| | 0 | 82 | | var dateCreatedStr = row.GetString(9); |
| | 0 | 83 | | _ = DateTime.TryParse(dateCreatedStr, CultureInfo.InvariantCulture, out var dateCreated); |
| | 0 | 84 | | var dateLastActivityStr = row.GetString(10); |
| | 0 | 85 | | _ = DateTime.TryParse(dateLastActivityStr, CultureInfo.InvariantCulture, out var dateLastActivity); |
| | | 86 | | |
| | 0 | 87 | | if (row.IsDBNull(6)) |
| | | 88 | | { |
| | 0 | 89 | | dbContext.ApiKeys.Add(new ApiKey(row.GetString(3)) |
| | 0 | 90 | | { |
| | 0 | 91 | | AccessToken = row.GetString(1), |
| | 0 | 92 | | DateCreated = dateCreated, |
| | 0 | 93 | | DateLastActivity = dateLastActivity |
| | 0 | 94 | | }); |
| | | 95 | | } |
| | | 96 | | else |
| | | 97 | | { |
| | 0 | 98 | | var userId = row.GetGuid(6); |
| | 0 | 99 | | var user = _userManager.GetUserById(userId); |
| | 0 | 100 | | if (user is null) |
| | | 101 | | { |
| | | 102 | | // User doesn't exist, don't bring over the device. |
| | | 103 | | continue; |
| | | 104 | | } |
| | | 105 | | |
| | 0 | 106 | | dbContext.Devices.Add(new Device( |
| | 0 | 107 | | userId, |
| | 0 | 108 | | row.GetString(3), |
| | 0 | 109 | | row.GetString(4), |
| | 0 | 110 | | row.GetString(5), |
| | 0 | 111 | | row.GetString(2)) |
| | 0 | 112 | | { |
| | 0 | 113 | | AccessToken = row.GetString(1), |
| | 0 | 114 | | IsActive = row.GetBoolean(8), |
| | 0 | 115 | | DateCreated = dateCreated, |
| | 0 | 116 | | DateLastActivity = dateLastActivity |
| | 0 | 117 | | }); |
| | | 118 | | } |
| | | 119 | | } |
| | | 120 | | |
| | 0 | 121 | | var deviceOptions = connection.Query("SELECT * FROM Devices"); |
| | 0 | 122 | | var deviceIds = new HashSet<string>(); |
| | 0 | 123 | | foreach (var row in deviceOptions) |
| | | 124 | | { |
| | 0 | 125 | | if (row.IsDBNull(2)) |
| | | 126 | | { |
| | | 127 | | continue; |
| | | 128 | | } |
| | | 129 | | |
| | 0 | 130 | | var deviceId = row.GetString(2); |
| | 0 | 131 | | if (deviceIds.Contains(deviceId)) |
| | | 132 | | { |
| | | 133 | | continue; |
| | | 134 | | } |
| | | 135 | | |
| | 0 | 136 | | deviceIds.Add(deviceId); |
| | | 137 | | |
| | 0 | 138 | | dbContext.DeviceOptions.Add(new DeviceOptions(deviceId) |
| | 0 | 139 | | { |
| | 0 | 140 | | CustomName = row.IsDBNull(1) ? null : row.GetString(1) |
| | 0 | 141 | | }); |
| | | 142 | | } |
| | | 143 | | |
| | 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 'authentication.db.old'"); |
| | 0 | 160 | | } |
| | 0 | 161 | | } |
| | | 162 | | } |
| | | 163 | | } |