2025-01-26 20:45:28 +00:00
|
|
|
using System;
|
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
|
|
|
|
|
{
|
|
|
|
|
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-01-26 20:45:28 +00:00
|
|
|
}
|