mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-06-27 17:01:16 -04:00
Add migration to migrate disconnected UserData too (#14339)
This commit is contained in:
parent
d561cef81f
commit
ba0eb87371
2 changed files with 111 additions and 6 deletions
|
@ -183,12 +183,12 @@ internal class MigrateLibraryDb : IDatabaseMigrationRoutine
|
|||
|
||||
using (new TrackedMigrationStep("Loading UserData", _logger))
|
||||
{
|
||||
var users = operation.JellyfinDbContext.Users.AsNoTracking().ToImmutableArray();
|
||||
var users = operation.JellyfinDbContext.Users.AsNoTracking().ToArray();
|
||||
var userIdBlacklist = new HashSet<int>();
|
||||
|
||||
foreach (var entity in queryResult)
|
||||
{
|
||||
var userData = GetUserData(users, entity, userIdBlacklist);
|
||||
var userData = GetUserData(users, entity, userIdBlacklist, _logger);
|
||||
if (userData is null)
|
||||
{
|
||||
var userDataId = entity.GetString(0);
|
||||
|
@ -212,8 +212,6 @@ internal class MigrateLibraryDb : IDatabaseMigrationRoutine
|
|||
userData.ItemId = refItem.Id;
|
||||
operation.JellyfinDbContext.UserData.Add(userData);
|
||||
}
|
||||
|
||||
users.Clear();
|
||||
}
|
||||
|
||||
legacyBaseItemWithUserKeys.Clear();
|
||||
|
@ -404,7 +402,7 @@ internal class MigrateLibraryDb : IDatabaseMigrationRoutine
|
|||
return new DatabaseMigrationStep(dbContext, operationName, _logger);
|
||||
}
|
||||
|
||||
private UserData? GetUserData(ImmutableArray<User> users, SqliteDataReader dto, HashSet<int> userIdBlacklist)
|
||||
internal static UserData? GetUserData(User[] users, SqliteDataReader dto, HashSet<int> userIdBlacklist, ILogger logger)
|
||||
{
|
||||
var internalUserId = dto.GetInt32(1);
|
||||
if (userIdBlacklist.Contains(internalUserId))
|
||||
|
@ -415,9 +413,9 @@ internal class MigrateLibraryDb : IDatabaseMigrationRoutine
|
|||
var user = users.FirstOrDefault(e => e.InternalId == internalUserId);
|
||||
if (user is null)
|
||||
{
|
||||
_logger.LogError("Tried to find user with index '{Idx}' but was not found, skipping user data import.", internalUserId);
|
||||
userIdBlacklist.Add(internalUserId);
|
||||
|
||||
logger.LogError("Tried to find user with index '{Idx}' but there are only '{MaxIdx}' users.", internalUserId, users.Length);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
107
Jellyfin.Server/Migrations/Routines/MigrateLibraryUserData.cs
Normal file
107
Jellyfin.Server/Migrations/Routines/MigrateLibraryUserData.cs
Normal file
|
@ -0,0 +1,107 @@
|
|||
#pragma warning disable RS0030 // Do not use banned APIs
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Emby.Server.Implementations.Data;
|
||||
using Jellyfin.Database.Implementations;
|
||||
using Jellyfin.Server.Implementations.Item;
|
||||
using Jellyfin.Server.ServerSetupApp;
|
||||
using MediaBrowser.Controller;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Server.Migrations.Routines;
|
||||
|
||||
[JellyfinMigration("2025-06-18T01:00:00", nameof(MigrateLibraryUserData))]
|
||||
[JellyfinMigrationBackup(JellyfinDb = true)]
|
||||
internal class MigrateLibraryUserData : IAsyncMigrationRoutine
|
||||
{
|
||||
private const string DbFilename = "library.db.old";
|
||||
|
||||
private readonly IStartupLogger _logger;
|
||||
private readonly IServerApplicationPaths _paths;
|
||||
private readonly IDbContextFactory<JellyfinDbContext> _provider;
|
||||
|
||||
public MigrateLibraryUserData(
|
||||
IStartupLogger<MigrateLibraryDb> startupLogger,
|
||||
IDbContextFactory<JellyfinDbContext> provider,
|
||||
IServerApplicationPaths paths)
|
||||
{
|
||||
_logger = startupLogger;
|
||||
_provider = provider;
|
||||
_paths = paths;
|
||||
}
|
||||
|
||||
public async Task PerformAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Migrating the userdata from library.db.old may take a while, do not stop Jellyfin.");
|
||||
|
||||
var dataPath = _paths.DataPath;
|
||||
var libraryDbPath = Path.Combine(dataPath, DbFilename);
|
||||
if (!File.Exists(libraryDbPath))
|
||||
{
|
||||
_logger.LogError("Cannot migrate userdata from {LibraryDb} as it does not exist. This migration expects the MigrateLibraryDb to run first.", libraryDbPath);
|
||||
return;
|
||||
}
|
||||
|
||||
var dbContext = await _provider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
|
||||
await using (dbContext.ConfigureAwait(false))
|
||||
{
|
||||
if (!await dbContext.BaseItems.AnyAsync(e => e.Id == BaseItemRepository.PlaceholderId, cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
// the placeholder baseitem has been deleted by the librarydb migration so we need to readd it.
|
||||
await dbContext.BaseItems.AddAsync(
|
||||
new Database.Implementations.Entities.BaseItemEntity()
|
||||
{
|
||||
Id = BaseItemRepository.PlaceholderId,
|
||||
Type = "PLACEHOLDER",
|
||||
Name = "This is a placeholder item for UserData that has been detacted from its original item"
|
||||
},
|
||||
cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var users = dbContext.Users.AsNoTracking().ToArray();
|
||||
var userIdBlacklist = new HashSet<int>();
|
||||
using var connection = new SqliteConnection($"Filename={libraryDbPath};Mode=ReadOnly");
|
||||
var retentionDate = DateTime.UtcNow;
|
||||
|
||||
var queryResult = connection.Query(
|
||||
"""
|
||||
SELECT key, userId, rating, played, playCount, isFavorite, playbackPositionTicks, lastPlayedDate, AudioStreamIndex, SubtitleStreamIndex FROM UserDatas
|
||||
|
||||
WHERE NOT EXISTS(SELECT 1 FROM TypedBaseItems WHERE TypedBaseItems.UserDataKey = UserDatas.key)
|
||||
""");
|
||||
foreach (var entity in queryResult)
|
||||
{
|
||||
var userData = MigrateLibraryDb.GetUserData(users, entity, userIdBlacklist, _logger);
|
||||
if (userData is null)
|
||||
{
|
||||
var userDataId = entity.GetString(0);
|
||||
var internalUserId = entity.GetInt32(1);
|
||||
|
||||
if (!userIdBlacklist.Contains(internalUserId))
|
||||
{
|
||||
_logger.LogError("Was not able to migrate user data with key {0} because its id {InternalId} does not match any existing user.", userDataId, internalUserId);
|
||||
userIdBlacklist.Add(internalUserId);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
userData.ItemId = BaseItemRepository.PlaceholderId;
|
||||
userData.RetentionDate = retentionDate;
|
||||
dbContext.UserData.Add(userData);
|
||||
}
|
||||
|
||||
_logger.LogInformation("Try saving {NewSaved} UserData entries.", dbContext.UserData.Local.Count);
|
||||
await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue