diff options
| author | JPVenson <github@jpb.email> | 2025-01-27 17:20:14 +0000 |
|---|---|---|
| committer | JPVenson <github@jpb.email> | 2025-01-27 17:20:14 +0000 |
| commit | 844646e2fe6d726edc8e086cc465396303d24f29 (patch) | |
| tree | b5e21a53df12d963a93047f9b1d959248cf18a5d | |
| parent | 9d1c4ea169a15d580923aefb0ec43c2b6be5b3a6 (diff) | |
Fixed migration runner and added docs for adding migrations
8 files changed, 59 insertions, 12 deletions
diff --git a/Emby.Server.Implementations/ApplicationHost.cs b/Emby.Server.Implementations/ApplicationHost.cs index 6687be2e9..eb01ed0fe 100644 --- a/Emby.Server.Implementations/ApplicationHost.cs +++ b/Emby.Server.Implementations/ApplicationHost.cs @@ -572,8 +572,9 @@ namespace Emby.Server.Implementations /// <summary> /// Create services registered with the service container that need to be initialized at application startup. /// </summary> + /// <param name="startupConfig">The configuration used to initialise the application.</param> /// <returns>A task representing the service initialization operation.</returns> - public async Task InitializeServices() + public async Task InitializeServices(IConfiguration startupConfig) { var factory = Resolve<IDbContextFactory<JellyfinDbContext>>(); var provider = Resolve<IJellyfinDatabaseProvider>(); diff --git a/Jellyfin.Database/Jellyfin.Database.Providers.SqLite/Migrations/DesignTimeJellyfinDbFactory.cs b/Jellyfin.Database/Jellyfin.Database.Providers.SqLite/Migrations/DesignTimeJellyfinDbFactory.cs index ff0ce3403..fdd9a5136 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<JellyfinDbContext>(); - 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 000000000..883aff2d7 --- /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 1b0dbbe10..091ecee98 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<System.IServiceProvider, Jellyfin.Server.Implementations.IJellyfinDatabaseProvider>; @@ -46,8 +47,12 @@ public static class ServiceCollectionExtensions /// </summary> /// <param name="serviceCollection">An instance of the <see cref="IServiceCollection"/> interface.</param> /// <param name="configurationManager">The server configuration manager.</param> + /// <param name="configuration">The startup Configuration.</param> /// <returns>The updated service collection.</returns> - public static IServiceCollection AddJellyfinDbContext(this IServiceCollection serviceCollection, IServerConfigurationManager configurationManager) + public static IServiceCollection AddJellyfinDbContext( + this IServiceCollection serviceCollection, + IServerConfigurationManager configurationManager, + IConfiguration configuration) { var efCoreConfiguration = configurationManager.GetConfiguration<DatabaseConfigurationOptions>("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<string>("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 6b95770ed..7695c0d9e 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 a6270aa93..fd23b7e25 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 850b653e1..fa21d2566 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; /// <summary> /// Initializes a new instance of the <see cref="Startup" /> class. /// </summary> /// <param name="appHost">The server application host.</param> - public Startup(CoreAppHost appHost) + /// <param name="configuration">The used Configuration.</param> + 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<IActionResultExecutor<PhysicalFileResult>, 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 78b32d278..a7fec2960 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<IApplicationHost>(); appHost.ServiceProvider = host.Services; - appHost.InitializeServices().GetAwaiter().GetResult(); + appHost.InitializeServices(Mock.Of<IConfiguration>()).GetAwaiter().GetResult(); host.Start(); appHost.RunStartupTasksAsync().GetAwaiter().GetResult(); |
