mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-05-24 02:02:29 -04:00
Fixed migration runner and added docs for adding migrations
This commit is contained in:
parent
9d1c4ea169
commit
844646e2fe
@ -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>();
|
||||
|
@ -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,
|
||||
|
26
Jellyfin.Database/readme.md
Normal file
26
Jellyfin.Database/readme.md
Normal file
@ -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.
|
@ -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!))
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user