| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Globalization; |
| | 4 | | using System.IO; |
| | 5 | | using System.Threading; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | | using Jellyfin.Database.Implementations; |
| | 8 | | using MediaBrowser.Common.Configuration; |
| | 9 | | using Microsoft.Data.Sqlite; |
| | 10 | | using Microsoft.EntityFrameworkCore; |
| | 11 | | using Microsoft.EntityFrameworkCore.Diagnostics; |
| | 12 | | using Microsoft.Extensions.Logging; |
| | 13 | |
|
| | 14 | | namespace Jellyfin.Database.Providers.Sqlite; |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Configures jellyfin to use an SQLite database. |
| | 18 | | /// </summary> |
| | 19 | | [JellyfinDatabaseProviderKey("Jellyfin-SQLite")] |
| | 20 | | public sealed class SqliteDatabaseProvider : IJellyfinDatabaseProvider |
| | 21 | | { |
| | 22 | | private const string BackupFolderName = "SQLiteBackups"; |
| | 23 | | private readonly IApplicationPaths _applicationPaths; |
| | 24 | | private readonly ILogger<SqliteDatabaseProvider> _logger; |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Initializes a new instance of the <see cref="SqliteDatabaseProvider"/> class. |
| | 28 | | /// </summary> |
| | 29 | | /// <param name="applicationPaths">Service to construct the fallback when the old data path configuration is used.</ |
| | 30 | | /// <param name="logger">A logger.</param> |
| | 31 | | public SqliteDatabaseProvider(IApplicationPaths applicationPaths, ILogger<SqliteDatabaseProvider> logger) |
| | 32 | | { |
| 43 | 33 | | _applicationPaths = applicationPaths; |
| 43 | 34 | | _logger = logger; |
| 43 | 35 | | } |
| | 36 | |
|
| | 37 | | /// <inheritdoc/> |
| | 38 | | public IDbContextFactory<JellyfinDbContext>? DbContextFactory { get; set; } |
| | 39 | |
|
| | 40 | | /// <inheritdoc/> |
| | 41 | | public void Initialise(DbContextOptionsBuilder options) |
| | 42 | | { |
| 42 | 43 | | options |
| 42 | 44 | | .UseSqlite( |
| 42 | 45 | | $"Filename={Path.Combine(_applicationPaths.DataPath, "jellyfin.db")};Pooling=false", |
| 42 | 46 | | sqLiteOptions => sqLiteOptions.MigrationsAssembly(GetType().Assembly)) |
| 42 | 47 | | // TODO: Remove when https://github.com/dotnet/efcore/pull/35873 is merged & released |
| 42 | 48 | | .ConfigureWarnings(warnings => |
| 42 | 49 | | warnings.Ignore(RelationalEventId.NonTransactionalMigrationOperationWarning)); |
| 42 | 50 | | } |
| | 51 | |
|
| | 52 | | /// <inheritdoc/> |
| | 53 | | public async Task RunScheduledOptimisation(CancellationToken cancellationToken) |
| | 54 | | { |
| | 55 | | var context = await DbContextFactory!.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); |
| | 56 | | await using (context.ConfigureAwait(false)) |
| | 57 | | { |
| | 58 | | if (context.Database.IsSqlite()) |
| | 59 | | { |
| | 60 | | await context.Database.ExecuteSqlRawAsync("PRAGMA optimize", cancellationToken).ConfigureAwait(false); |
| | 61 | | await context.Database.ExecuteSqlRawAsync("VACUUM", cancellationToken).ConfigureAwait(false); |
| | 62 | | _logger.LogInformation("jellyfin.db optimized successfully!"); |
| | 63 | | } |
| | 64 | | else |
| | 65 | | { |
| | 66 | | _logger.LogInformation("This database doesn't support optimization"); |
| | 67 | | } |
| | 68 | | } |
| | 69 | | } |
| | 70 | |
|
| | 71 | | /// <inheritdoc/> |
| | 72 | | public void OnModelCreating(ModelBuilder modelBuilder) |
| | 73 | | { |
| 2 | 74 | | modelBuilder.SetDefaultDateTimeKind(DateTimeKind.Utc); |
| 2 | 75 | | } |
| | 76 | |
|
| | 77 | | /// <inheritdoc/> |
| | 78 | | public async Task RunShutdownTask(CancellationToken cancellationToken) |
| | 79 | | { |
| | 80 | | if (DbContextFactory is null) |
| | 81 | | { |
| | 82 | | return; |
| | 83 | | } |
| | 84 | |
|
| | 85 | | // Run before disposing the application |
| | 86 | | var context = await DbContextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); |
| | 87 | | await using (context.ConfigureAwait(false)) |
| | 88 | | { |
| | 89 | | await context.Database.ExecuteSqlRawAsync("PRAGMA optimize", cancellationToken).ConfigureAwait(false); |
| | 90 | | } |
| | 91 | |
|
| | 92 | | SqliteConnection.ClearAllPools(); |
| | 93 | | } |
| | 94 | |
|
| | 95 | | /// <inheritdoc/> |
| | 96 | | public void ConfigureConventions(ModelConfigurationBuilder configurationBuilder) |
| | 97 | | { |
| 2 | 98 | | configurationBuilder.Conventions.Add(_ => new DoNotUseReturningClauseConvention()); |
| 2 | 99 | | } |
| | 100 | |
|
| | 101 | | /// <inheritdoc /> |
| | 102 | | public Task<string> MigrationBackupFast(CancellationToken cancellationToken) |
| | 103 | | { |
| 0 | 104 | | var key = DateTime.UtcNow.ToString("yyyyMMddhhmmss", CultureInfo.InvariantCulture); |
| 0 | 105 | | var path = Path.Combine(_applicationPaths.DataPath, "jellyfin.db"); |
| 0 | 106 | | var backupFile = Path.Combine(_applicationPaths.DataPath, BackupFolderName); |
| 0 | 107 | | Directory.CreateDirectory(backupFile); |
| | 108 | |
|
| 0 | 109 | | backupFile = Path.Combine(backupFile, $"{key}_jellyfin.db"); |
| 0 | 110 | | File.Copy(path, backupFile); |
| 0 | 111 | | return Task.FromResult(key); |
| | 112 | | } |
| | 113 | |
|
| | 114 | | /// <inheritdoc /> |
| | 115 | | public Task RestoreBackupFast(string key, CancellationToken cancellationToken) |
| | 116 | | { |
| | 117 | | // ensure there are absolutly no dangling Sqlite connections. |
| 0 | 118 | | SqliteConnection.ClearAllPools(); |
| 0 | 119 | | var path = Path.Combine(_applicationPaths.DataPath, "jellyfin.db"); |
| 0 | 120 | | var backupFile = Path.Combine(_applicationPaths.DataPath, BackupFolderName, $"{key}_jellyfin.db"); |
| | 121 | |
|
| 0 | 122 | | if (!File.Exists(backupFile)) |
| | 123 | | { |
| 0 | 124 | | _logger.LogCritical("Tried to restore a backup that does not exist: {Key}", key); |
| 0 | 125 | | return Task.CompletedTask; |
| | 126 | | } |
| | 127 | |
|
| 0 | 128 | | File.Copy(backupFile, path, true); |
| 0 | 129 | | return Task.CompletedTask; |
| | 130 | | } |
| | 131 | |
|
| | 132 | | /// <inheritdoc /> |
| | 133 | | public Task DeleteBackup(string key) |
| | 134 | | { |
| 0 | 135 | | var backupFile = Path.Combine(_applicationPaths.DataPath, BackupFolderName, $"{key}_jellyfin.db"); |
| | 136 | |
|
| 0 | 137 | | if (!File.Exists(backupFile)) |
| | 138 | | { |
| 0 | 139 | | _logger.LogCritical("Tried to delete a backup that does not exist: {Key}", key); |
| 0 | 140 | | return Task.CompletedTask; |
| | 141 | | } |
| | 142 | |
|
| 0 | 143 | | File.Delete(backupFile); |
| 0 | 144 | | return Task.CompletedTask; |
| | 145 | | } |
| | 146 | |
|
| | 147 | | /// <inheritdoc/> |
| | 148 | | public async Task PurgeDatabase(JellyfinDbContext dbContext, IEnumerable<string>? tableNames) |
| | 149 | | { |
| | 150 | | ArgumentNullException.ThrowIfNull(tableNames); |
| | 151 | |
|
| | 152 | | var deleteQueries = new List<string>(); |
| | 153 | | foreach (var tableName in tableNames) |
| | 154 | | { |
| | 155 | | deleteQueries.Add($"DELETE FROM \"{tableName}\";"); |
| | 156 | | } |
| | 157 | |
|
| | 158 | | var deleteAllQuery = |
| | 159 | | $""" |
| | 160 | | PRAGMA foreign_keys = OFF; |
| | 161 | | {string.Join('\n', deleteQueries)} |
| | 162 | | PRAGMA foreign_keys = ON; |
| | 163 | | """; |
| | 164 | |
|
| | 165 | | await dbContext.Database.ExecuteSqlRawAsync(deleteAllQuery).ConfigureAwait(false); |
| | 166 | | } |
| | 167 | | } |