diff --git a/Emby.Server.Implementations/ApplicationHost.cs b/Emby.Server.Implementations/ApplicationHost.cs
index 6687be2e91..eb01ed0fe5 100644
--- a/Emby.Server.Implementations/ApplicationHost.cs
+++ b/Emby.Server.Implementations/ApplicationHost.cs
@@ -572,8 +572,9 @@ namespace Emby.Server.Implementations
///
/// Create services registered with the service container that need to be initialized at application startup.
///
+ /// The configuration used to initialise the application.
/// A task representing the service initialization operation.
- public async Task InitializeServices()
+ public async Task InitializeServices(IConfiguration startupConfig)
{
var factory = Resolve>();
var provider = Resolve();
diff --git a/Jellyfin.Database/Jellyfin.Database.Providers.SqLite/Migrations/DesignTimeJellyfinDbFactory.cs b/Jellyfin.Database/Jellyfin.Database.Providers.SqLite/Migrations/DesignTimeJellyfinDbFactory.cs
index ff0ce3403c..fdd9a51361 100644
--- a/Jellyfin.Database/Jellyfin.Database.Providers.SqLite/Migrations/DesignTimeJellyfinDbFactory.cs
+++ b/Jellyfin.Database/Jellyfin.Database.Providers.SqLite/Migrations/DesignTimeJellyfinDbFactory.cs
@@ -14,7 +14,7 @@ namespace Jellyfin.Server.Implementations.Migrations
public JellyfinDbContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder();
- optionsBuilder.UseSqlite("Data Source=jellyfin.db");
+ optionsBuilder.UseSqlite("Data Source=jellyfin.db", f => f.MigrationsAssembly(GetType().Assembly));
return new JellyfinDbContext(
optionsBuilder.Options,
diff --git a/Jellyfin.Database/readme.md b/Jellyfin.Database/readme.md
new file mode 100644
index 0000000000..883aff2d75
--- /dev/null
+++ b/Jellyfin.Database/readme.md
@@ -0,0 +1,26 @@
+# How to run EFCore migrations
+
+This shall provide context on how to work with entity frameworks multi provider migration feature.
+
+Jellyfin supports multiple database providers, namely SqLite as its default and the experimental postgresSQL.
+
+Each provider has its own set of migrations, as they contain provider specific instructions to migrate the specific changes to their respective systems.
+
+When creating a new migration, you always have to create migrations for all providers. This is supported via the following syntax:
+
+```cmd
+dotnet ef migrations add MIGRATION_NAME --project "PATH_TO_PROJECT" -- --provider PROVIDER_KEY
+```
+
+with both sqlite and pgsql currently beeing supported and both need migrations, you need to run the efcore tool with the correct project to tell EfCore where to store the migrations and the correct provider key to tell jellyfin to load that provider.
+
+The example is made from the root folder of the project e.g for codespaces `/workspaces/jellyfin`
+
+```cmd
+dotnet ef migrations add {MIGRATION_NAME} --project "Jellyfin.Database/Jellyfin.Database.Providers.SqLite" -- --migration-provider Jellyfin-SqLite
+dotnet ef migrations add {MIGRATION_NAME} --project "Jellyfin.Database/Jellyfin.Database.Providers.PgSql" -- --migration-provider Jellyfin-PgSql
+```
+
+If you get the error: `Run "dotnet tool restore" to make the "dotnet-ef" command available.` Run `dotnet restore`.
+
+in the event that you get the error: `System.UnauthorizedAccessException: Access to the path '/Jellyfin.Database' is denied.` you have to restore as sudo and then run `ef migrations` as sudo too.
diff --git a/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs b/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs
index 1b0dbbe108..091ecee987 100644
--- a/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs
+++ b/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs
@@ -8,6 +8,7 @@ using Jellyfin.Server.Implementations.DatabaseConfiguration;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller.Configuration;
using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using JellyfinDbProviderFactory = System.Func;
@@ -46,8 +47,12 @@ public static class ServiceCollectionExtensions
///
/// An instance of the interface.
/// The server configuration manager.
+ /// The startup Configuration.
/// The updated service collection.
- public static IServiceCollection AddJellyfinDbContext(this IServiceCollection serviceCollection, IServerConfigurationManager configurationManager)
+ public static IServiceCollection AddJellyfinDbContext(
+ this IServiceCollection serviceCollection,
+ IServerConfigurationManager configurationManager,
+ IConfiguration configuration)
{
var efCoreConfiguration = configurationManager.GetConfiguration("database");
var providers = GetSupportedDbProviders();
@@ -55,11 +60,22 @@ public static class ServiceCollectionExtensions
if (efCoreConfiguration?.DatabaseType is null)
{
- // when nothing is setup via new Database configuration, fallback to SqLite with default settings.
- efCoreConfiguration = new DatabaseConfigurationOptions()
+ var cmdMigrationArgument = configuration.GetValue("migration-provider");
+ if (!string.IsNullOrWhiteSpace(cmdMigrationArgument))
{
- DatabaseType = "Jellyfin-SqLite",
- };
+ efCoreConfiguration = new DatabaseConfigurationOptions()
+ {
+ DatabaseType = cmdMigrationArgument,
+ };
+ }
+ else
+ {
+ // when nothing is setup via new Database configuration, fallback to SqLite with default settings.
+ efCoreConfiguration = new DatabaseConfigurationOptions()
+ {
+ DatabaseType = "Jellyfin-SqLite",
+ };
+ }
}
if (!providers.TryGetValue(efCoreConfiguration.DatabaseType, out providerFactory!))
diff --git a/Jellyfin.Server/Extensions/WebHostBuilderExtensions.cs b/Jellyfin.Server/Extensions/WebHostBuilderExtensions.cs
index 6b95770ed5..7695c0d9ee 100644
--- a/Jellyfin.Server/Extensions/WebHostBuilderExtensions.cs
+++ b/Jellyfin.Server/Extensions/WebHostBuilderExtensions.cs
@@ -85,6 +85,6 @@ public static class WebHostBuilderExtensions
logger.LogInformation("Kestrel listening to unix socket {SocketPath}", socketPath);
}
})
- .UseStartup(_ => new Startup(appHost));
+ .UseStartup(context => new Startup(appHost, context.Configuration));
}
}
diff --git a/Jellyfin.Server/Program.cs b/Jellyfin.Server/Program.cs
index a6270aa93d..fd23b7e25c 100644
--- a/Jellyfin.Server/Program.cs
+++ b/Jellyfin.Server/Program.cs
@@ -157,7 +157,7 @@ namespace Jellyfin.Server
// Re-use the host service provider in the app host since ASP.NET doesn't allow a custom service collection.
appHost.ServiceProvider = host.Services;
- await appHost.InitializeServices().ConfigureAwait(false);
+ await appHost.InitializeServices(startupConfig).ConfigureAwait(false);
Migrations.MigrationRunner.Run(appHost, _loggerFactory);
try
diff --git a/Jellyfin.Server/Startup.cs b/Jellyfin.Server/Startup.cs
index 850b653e1c..fa21d25664 100644
--- a/Jellyfin.Server/Startup.cs
+++ b/Jellyfin.Server/Startup.cs
@@ -39,15 +39,18 @@ namespace Jellyfin.Server
public class Startup
{
private readonly CoreAppHost _serverApplicationHost;
+ private readonly IConfiguration _configuration;
private readonly IServerConfigurationManager _serverConfigurationManager;
///
/// Initializes a new instance of the class.
///
/// The server application host.
- public Startup(CoreAppHost appHost)
+ /// The used Configuration.
+ public Startup(CoreAppHost appHost, IConfiguration configuration)
{
_serverApplicationHost = appHost;
+ _configuration = configuration;
_serverConfigurationManager = appHost.ConfigurationManager;
}
@@ -67,7 +70,7 @@ namespace Jellyfin.Server
// TODO remove once this is fixed upstream https://github.com/dotnet/aspnetcore/issues/34371
services.AddSingleton, SymlinkFollowingPhysicalFileResultExecutor>();
services.AddJellyfinApi(_serverApplicationHost.GetApiPluginAssemblies(), _serverConfigurationManager.GetNetworkConfiguration());
- services.AddJellyfinDbContext(_serverApplicationHost.ConfigurationManager);
+ services.AddJellyfinDbContext(_serverApplicationHost.ConfigurationManager, _configuration);
services.AddJellyfinApiSwagger();
// configure custom legacy authentication
diff --git a/tests/Jellyfin.Server.Integration.Tests/JellyfinApplicationFactory.cs b/tests/Jellyfin.Server.Integration.Tests/JellyfinApplicationFactory.cs
index 78b32d2785..a7fec2960c 100644
--- a/tests/Jellyfin.Server.Integration.Tests/JellyfinApplicationFactory.cs
+++ b/tests/Jellyfin.Server.Integration.Tests/JellyfinApplicationFactory.cs
@@ -13,6 +13,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
+using Moq;
using Serilog;
using Serilog.Extensions.Logging;
@@ -102,7 +103,7 @@ namespace Jellyfin.Server.Integration.Tests
var host = builder.Build();
var appHost = (TestAppHost)host.Services.GetRequiredService();
appHost.ServiceProvider = host.Services;
- appHost.InitializeServices().GetAwaiter().GetResult();
+ appHost.InitializeServices(Mock.Of()).GetAwaiter().GetResult();
host.Start();
appHost.RunStartupTasksAsync().GetAwaiter().GetResult();