aboutsummaryrefslogtreecommitdiff
path: root/src/Jellyfin.Database/Jellyfin.Database.Providers.Sqlite/SqliteDatabaseProvider.cs
diff options
context:
space:
mode:
authorCody Robibero <cody@robibe.ro>2025-03-25 21:34:26 -0600
committerGitHub <noreply@github.com>2025-03-25 21:34:26 -0600
commitd848faeb75f0109b5616f1f8405fa85cf734f860 (patch)
tree59671ffe495d438cbb9ce095f1c3f26247095291 /src/Jellyfin.Database/Jellyfin.Database.Providers.Sqlite/SqliteDatabaseProvider.cs
parent035ecbdde33ce5b71cd580ebb0c3e1df320f80c7 (diff)
parent1b388d729682435b92cb10eba67a1170ecbfcc6c (diff)
Merge pull request #13589 from JPVenson/feature/DatabaseRefactor
[Feature] Database code refactor
Diffstat (limited to 'src/Jellyfin.Database/Jellyfin.Database.Providers.Sqlite/SqliteDatabaseProvider.cs')
-rw-r--r--src/Jellyfin.Database/Jellyfin.Database.Providers.Sqlite/SqliteDatabaseProvider.cs87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/Jellyfin.Database/Jellyfin.Database.Providers.Sqlite/SqliteDatabaseProvider.cs b/src/Jellyfin.Database/Jellyfin.Database.Providers.Sqlite/SqliteDatabaseProvider.cs
new file mode 100644
index 000000000..d9eb0ae7a
--- /dev/null
+++ b/src/Jellyfin.Database/Jellyfin.Database.Providers.Sqlite/SqliteDatabaseProvider.cs
@@ -0,0 +1,87 @@
+using System;
+using System.IO;
+using System.Threading;
+using System.Threading.Tasks;
+using Jellyfin.Database.Implementations;
+using MediaBrowser.Common.Configuration;
+using Microsoft.Data.Sqlite;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.Logging;
+
+namespace Jellyfin.Database.Providers.Sqlite;
+
+/// <summary>
+/// Configures jellyfin to use an SQLite database.
+/// </summary>
+[JellyfinDatabaseProviderKey("Jellyfin-SQLite")]
+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>
+ public SqliteDatabaseProvider(IApplicationPaths applicationPaths, ILogger<SqliteDatabaseProvider> logger)
+ {
+ _applicationPaths = applicationPaths;
+ _logger = logger;
+ }
+
+ /// <inheritdoc/>
+ public IDbContextFactory<JellyfinDbContext>? DbContextFactory { get; set; }
+
+ /// <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)
+ {
+ var context = await DbContextFactory!.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
+ 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/>
+ public async Task RunShutdownTask(CancellationToken cancellationToken)
+ {
+ // Run before disposing the application
+ var context = await DbContextFactory!.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
+ await using (context.ConfigureAwait(false))
+ {
+ await context.Database.ExecuteSqlRawAsync("PRAGMA optimize", cancellationToken).ConfigureAwait(false);
+ }
+
+ SqliteConnection.ClearAllPools();
+ }
+
+ /// <inheritdoc/>
+ public void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
+ {
+ configurationBuilder.Conventions.Add(_ => new DoNotUseReturningClauseConvention());
+ }
+}