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