2020-05-02 15:32:22 -07:00
|
|
|
using System;
|
2020-07-31 01:13:54 -07:00
|
|
|
using System.IO;
|
2021-09-06 11:41:37 -07:00
|
|
|
using System.Linq;
|
2020-07-31 01:13:54 -07:00
|
|
|
using MediaBrowser.Common.Configuration;
|
2020-05-02 15:32:22 -07:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2021-09-06 11:41:37 -07:00
|
|
|
using Microsoft.Extensions.Logging;
|
2020-05-02 15:32:22 -07:00
|
|
|
|
|
|
|
namespace Jellyfin.Server.Implementations
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Factory class for generating new <see cref="JellyfinDb"/> instances.
|
|
|
|
/// </summary>
|
|
|
|
public class JellyfinDbProvider
|
|
|
|
{
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
2020-07-31 01:13:54 -07:00
|
|
|
private readonly IApplicationPaths _appPaths;
|
2021-09-06 11:41:37 -07:00
|
|
|
private readonly ILogger<JellyfinDbProvider> _logger;
|
2020-05-02 15:32:22 -07:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="JellyfinDbProvider"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="serviceProvider">The application's service provider.</param>
|
2020-07-31 01:13:54 -07:00
|
|
|
/// <param name="appPaths">The application paths.</param>
|
2021-09-06 11:41:37 -07:00
|
|
|
/// <param name="logger">The logger.</param>
|
|
|
|
public JellyfinDbProvider(IServiceProvider serviceProvider, IApplicationPaths appPaths, ILogger<JellyfinDbProvider> logger)
|
2020-05-02 15:32:22 -07:00
|
|
|
{
|
|
|
|
_serviceProvider = serviceProvider;
|
2020-07-31 01:13:54 -07:00
|
|
|
_appPaths = appPaths;
|
2021-09-06 11:41:37 -07:00
|
|
|
_logger = logger;
|
2020-07-31 01:13:54 -07:00
|
|
|
|
|
|
|
using var jellyfinDb = CreateContext();
|
2021-09-06 11:41:37 -07:00
|
|
|
if (jellyfinDb.Database.GetPendingMigrations().Any())
|
|
|
|
{
|
|
|
|
_logger.LogInformation("There are pending EFCore migrations in the database. Applying... (This may take a while, do not stop Jellyfin)");
|
|
|
|
jellyfinDb.Database.Migrate();
|
|
|
|
_logger.LogInformation("EFCore migrations applied successfully");
|
|
|
|
}
|
2020-05-02 15:32:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates a new <see cref="JellyfinDb"/> context.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>The newly created context.</returns>
|
|
|
|
public JellyfinDb CreateContext()
|
|
|
|
{
|
2020-07-31 01:13:54 -07:00
|
|
|
var contextOptions = new DbContextOptionsBuilder<JellyfinDb>().UseSqlite($"Filename={Path.Combine(_appPaths.DataPath, "jellyfin.db")}");
|
|
|
|
return ActivatorUtilities.CreateInstance<JellyfinDb>(_serviceProvider, contextOptions.Options);
|
2020-05-02 15:32:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|