| | 1 | | #pragma warning disable RS0030 // Do not use banned APIs |
| | 2 | |
|
| | 3 | | using System; |
| | 4 | | using System.IO; |
| | 5 | | using System.Threading; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | | using Jellyfin.Server.ServerSetupApp; |
| | 8 | | using MediaBrowser.Controller; |
| | 9 | | using Microsoft.Data.Sqlite; |
| | 10 | | using Microsoft.Extensions.Logging; |
| | 11 | |
|
| | 12 | | namespace Jellyfin.Server.Migrations.Routines; |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// The migration routine for checking if the current instance of Jellyfin is compatiable to be upgraded. |
| | 16 | | /// </summary> |
| | 17 | | [JellyfinMigration("2025-04-20T19:30:00", nameof(MigrateLibraryDbCompatibilityCheck))] |
| | 18 | | public class MigrateLibraryDbCompatibilityCheck : IAsyncMigrationRoutine |
| | 19 | | { |
| | 20 | | private const string DbFilename = "library.db"; |
| | 21 | | private readonly IStartupLogger _logger; |
| | 22 | | private readonly IServerApplicationPaths _paths; |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Initializes a new instance of the <see cref="MigrateLibraryDbCompatibilityCheck"/> class. |
| | 26 | | /// </summary> |
| | 27 | | /// <param name="startupLogger">The startup logger.</param> |
| | 28 | | /// <param name="paths">The Path service.</param> |
| | 29 | | public MigrateLibraryDbCompatibilityCheck(IStartupLogger startupLogger, IServerApplicationPaths paths) |
| | 30 | | { |
| 0 | 31 | | _logger = startupLogger; |
| 0 | 32 | | _paths = paths; |
| 0 | 33 | | } |
| | 34 | |
|
| | 35 | | /// <inheritdoc/> |
| | 36 | | public async Task PerformAsync(CancellationToken cancellationToken) |
| | 37 | | { |
| | 38 | | var dataPath = _paths.DataPath; |
| | 39 | | var libraryDbPath = Path.Combine(dataPath, DbFilename); |
| | 40 | | if (!File.Exists(libraryDbPath)) |
| | 41 | | { |
| | 42 | | _logger.LogError("Cannot migrate {LibraryDb} as it does not exist..", libraryDbPath); |
| | 43 | | return; |
| | 44 | | } |
| | 45 | |
|
| | 46 | | using var connection = new SqliteConnection($"Filename={libraryDbPath};Mode=ReadOnly"); |
| | 47 | | await connection.OpenAsync(cancellationToken).ConfigureAwait(false); |
| | 48 | | CheckMigratableVersion(connection); |
| | 49 | | await connection.CloseAsync().ConfigureAwait(false); |
| | 50 | | } |
| | 51 | |
|
| | 52 | | private static void CheckMigratableVersion(SqliteConnection connection) |
| | 53 | | { |
| 0 | 54 | | CheckColumnExistance(connection, "TypedBaseItems", "lufs"); |
| 0 | 55 | | CheckColumnExistance(connection, "TypedBaseItems", "normalizationgain"); |
| 0 | 56 | | CheckColumnExistance(connection, "mediastreams", "dvversionmajor"); |
| | 57 | |
|
| | 58 | | static void CheckColumnExistance(SqliteConnection connection, string table, string column) |
| | 59 | | { |
| | 60 | | using (var cmd = connection.CreateCommand()) |
| | 61 | | { |
| | 62 | | #pragma warning disable CA2100 // Review SQL queries for security vulnerabilities |
| | 63 | | cmd.CommandText = $"Select COUNT(1) FROM pragma_table_xinfo('{table}') WHERE lower(name) = '{column}';"; |
| | 64 | | #pragma warning restore CA2100 // Review SQL queries for security vulnerabilities |
| | 65 | | var result = cmd.ExecuteScalar()!; |
| | 66 | | if (!result.Equals(1L)) |
| | 67 | | { |
| | 68 | | throw new InvalidOperationException("Your database does not meet the required standard. Only upgrade |
| | 69 | | } |
| | 70 | | } |
| | 71 | | } |
| 0 | 72 | | } |
| | 73 | | } |