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