| | | 1 | | using System.Threading; |
| | | 2 | | using System.Threading.Tasks; |
| | | 3 | | using Jellyfin.Database.Implementations; |
| | | 4 | | using Microsoft.EntityFrameworkCore; |
| | | 5 | | using Microsoft.Extensions.Logging; |
| | | 6 | | |
| | | 7 | | namespace Jellyfin.Server.Migrations.Routines; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Drops keys that no current property reads or writes from the serialized <c>BaseItems.Data</c> blob. |
| | | 11 | | /// </summary> |
| | | 12 | | [JellyfinMigration("2026-09-11T12:00:00", nameof(StripEmbeddedLinkedChildren))] |
| | | 13 | | internal class StripEmbeddedLinkedChildren : IDatabaseMigrationRoutine |
| | | 14 | | { |
| | | 15 | | private readonly ILogger<StripEmbeddedLinkedChildren> _logger; |
| | | 16 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | | 17 | | |
| | | 18 | | public StripEmbeddedLinkedChildren( |
| | | 19 | | ILoggerFactory loggerFactory, |
| | | 20 | | IDbContextFactory<JellyfinDbContext> dbProvider) |
| | | 21 | | { |
| | 0 | 22 | | _logger = loggerFactory.CreateLogger<StripEmbeddedLinkedChildren>(); |
| | 0 | 23 | | _dbProvider = dbProvider; |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc/> |
| | | 27 | | public async Task PerformAsync(CancellationToken cancellationToken) |
| | | 28 | | { |
| | 0 | 29 | | var context = await _dbProvider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 30 | | await using (context.ConfigureAwait(false)) |
| | | 31 | | { |
| | | 32 | | // json_valid guards the rare malformed blob: json_remove would abort the statement on it, |
| | | 33 | | // and one bad row must not cost every other row the fix. |
| | 0 | 34 | | var updated = await context.Database.ExecuteSqlRawAsync( |
| | 0 | 35 | | """ |
| | 0 | 36 | | UPDATE "BaseItems" |
| | 0 | 37 | | SET "Data" = json_remove("Data", '$.LinkedChildren', '$.ExtraIds', '$.SupportsExternalTransfer') |
| | 0 | 38 | | WHERE "Data" IS NOT NULL |
| | 0 | 39 | | AND json_valid("Data") = 1 |
| | 0 | 40 | | AND ("Data" LIKE '%"LinkedChildren"%' |
| | 0 | 41 | | OR "Data" LIKE '%"ExtraIds"%' |
| | 0 | 42 | | OR "Data" LIKE '%"SupportsExternalTransfer"%') |
| | 0 | 43 | | """, |
| | 0 | 44 | | cancellationToken).ConfigureAwait(false); |
| | | 45 | | |
| | 0 | 46 | | _logger.LogInformation("Dropped dead keys from the serialized data of {Count} items", updated); |
| | | 47 | | } |
| | 0 | 48 | | } |
| | | 49 | | } |