2025-01-26 20:45:28 +00:00
|
|
|
using System;
|
2025-03-27 03:23:36 +01:00
|
|
|
using System.Globalization;
|
2025-02-20 09:55:02 +00:00
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2025-03-25 15:30:22 +00:00
|
|
|
using Jellyfin.Database.Implementations;
|
2025-01-26 20:45:28 +00:00
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
|
using Microsoft.Data.Sqlite;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
2025-03-24 10:14:16 +00:00
|
|
|
namespace Jellyfin.Database.Providers.Sqlite;
|
2025-01-26 20:45:28 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
2025-02-03 20:15:36 +00:00
|
|
|
/// Configures jellyfin to use an SQLite database.
|
2025-01-26 20:45:28 +00:00
|
|
|
/// </summary>
|
2025-02-03 20:15:36 +00:00
|
|
|
[JellyfinDatabaseProviderKey("Jellyfin-SQLite")]
|
2025-01-26 20:45:28 +00:00
|
|
|
public sealed class SqliteDatabaseProvider : IJellyfinDatabaseProvider
|
|
|
|
|
{
|
2025-03-27 03:23:36 +01:00
|
|
|
private const string BackupFolderName = "SQLiteBackups";
|
2025-01-26 20:45:28 +00:00
|
|
|
private readonly IApplicationPaths _applicationPaths;
|
|
|
|
|
private readonly ILogger<SqliteDatabaseProvider> _logger;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="SqliteDatabaseProvider"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="applicationPaths">Service to construct the fallback when the old data path configuration is used.</param>
|
|
|
|
|
/// <param name="logger">A logger.</param>
|
2025-01-27 16:35:46 +00:00
|
|
|
public SqliteDatabaseProvider(IApplicationPaths applicationPaths, ILogger<SqliteDatabaseProvider> logger)
|
2025-01-26 20:45:28 +00:00
|
|
|
{
|
|
|
|
|
_applicationPaths = applicationPaths;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-27 16:35:46 +00:00
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public IDbContextFactory<JellyfinDbContext>? DbContextFactory { get; set; }
|
2025-01-26 20:45:28 +00:00
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public void Initialise(DbContextOptionsBuilder options)
|
|
|
|
|
{
|
|
|
|
|
options.UseSqlite(
|
|
|
|
|
$"Filename={Path.Combine(_applicationPaths.DataPath, "jellyfin.db")};Pooling=false",
|
|
|
|
|
sqLiteOptions => sqLiteOptions.MigrationsAssembly(GetType().Assembly));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public async Task RunScheduledOptimisation(CancellationToken cancellationToken)
|
|
|
|
|
{
|
2025-01-27 16:35:46 +00:00
|
|
|
var context = await DbContextFactory!.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
|
2025-01-26 20:45:28 +00:00
|
|
|
await using (context.ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
if (context.Database.IsSqlite())
|
|
|
|
|
{
|
|
|
|
|
await context.Database.ExecuteSqlRawAsync("PRAGMA optimize", cancellationToken).ConfigureAwait(false);
|
|
|
|
|
await context.Database.ExecuteSqlRawAsync("VACUUM", cancellationToken).ConfigureAwait(false);
|
|
|
|
|
_logger.LogInformation("jellyfin.db optimized successfully!");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("This database doesn't support optimization");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public void OnModelCreating(ModelBuilder modelBuilder)
|
|
|
|
|
{
|
|
|
|
|
modelBuilder.SetDefaultDateTimeKind(DateTimeKind.Utc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2025-02-02 02:32:28 +00:00
|
|
|
public async Task RunShutdownTask(CancellationToken cancellationToken)
|
2025-01-26 20:45:28 +00:00
|
|
|
{
|
|
|
|
|
// Run before disposing the application
|
2025-02-02 02:32:28 +00:00
|
|
|
var context = await DbContextFactory!.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
|
2025-01-26 20:45:28 +00:00
|
|
|
await using (context.ConfigureAwait(false))
|
|
|
|
|
{
|
2025-02-02 02:32:28 +00:00
|
|
|
await context.Database.ExecuteSqlRawAsync("PRAGMA optimize", cancellationToken).ConfigureAwait(false);
|
2025-01-26 20:45:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SqliteConnection.ClearAllPools();
|
|
|
|
|
}
|
2025-03-01 14:16:02 +00:00
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
|
|
|
|
|
{
|
|
|
|
|
configurationBuilder.Conventions.Add(_ => new DoNotUseReturningClauseConvention());
|
|
|
|
|
}
|
2025-03-27 03:23:36 +01:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public Task<string> MigrationBackupFast(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var key = DateTime.UtcNow.ToString("yyyyMMddhhmmss", CultureInfo.InvariantCulture);
|
|
|
|
|
var path = Path.Combine(_applicationPaths.DataPath, "jellyfin.db");
|
|
|
|
|
var backupFile = Path.Combine(_applicationPaths.DataPath, BackupFolderName);
|
|
|
|
|
if (!Directory.Exists(backupFile))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(backupFile);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-07 16:59:00 +02:00
|
|
|
backupFile = Path.Combine(backupFile, $"{key}_jellyfin.db");
|
2025-03-27 03:23:36 +01:00
|
|
|
File.Copy(path, backupFile);
|
|
|
|
|
return Task.FromResult(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public Task RestoreBackupFast(string key, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2025-04-07 16:59:00 +02:00
|
|
|
// ensure there are absolutly no dangling Sqlite connections.
|
|
|
|
|
SqliteConnection.ClearAllPools();
|
2025-03-27 03:23:36 +01:00
|
|
|
var path = Path.Combine(_applicationPaths.DataPath, "jellyfin.db");
|
|
|
|
|
var backupFile = Path.Combine(_applicationPaths.DataPath, BackupFolderName, $"{key}_jellyfin.db");
|
|
|
|
|
|
|
|
|
|
if (!File.Exists(backupFile))
|
|
|
|
|
{
|
|
|
|
|
_logger.LogCritical("Tried to restore a backup that does not exist.");
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File.Copy(backupFile, path, true);
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2025-01-26 20:45:28 +00:00
|
|
|
}
|