From 999de06d6bc8deac4c440167ca59b22835b6bf60 Mon Sep 17 00:00:00 2001 From: JPVenson Date: Fri, 22 May 2026 17:31:45 +0000 Subject: [PATCH] Add 3 part migration for normalized Username --- ...20260522092304_UpdateNormalizedUsername.cs | 44 +++++++++++++++++++ .../20260522092303_AddNormalizedUsername.cs | 10 +---- ...20260522092305_UpdateNormalizedUsername.cs | 31 +++++++++++++ 3 files changed, 76 insertions(+), 9 deletions(-) create mode 100644 Jellyfin.Server/Migrations/Routines/20260522092304_UpdateNormalizedUsername.cs create mode 100644 src/Jellyfin.Database/Jellyfin.Database.Providers.Sqlite/Migrations/20260522092305_UpdateNormalizedUsername.cs diff --git a/Jellyfin.Server/Migrations/Routines/20260522092304_UpdateNormalizedUsername.cs b/Jellyfin.Server/Migrations/Routines/20260522092304_UpdateNormalizedUsername.cs new file mode 100644 index 0000000000..8100d4759e --- /dev/null +++ b/Jellyfin.Server/Migrations/Routines/20260522092304_UpdateNormalizedUsername.cs @@ -0,0 +1,44 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using Jellyfin.Database.Implementations; +using MediaBrowser.Controller.Configuration; +using Microsoft.EntityFrameworkCore; + +namespace Jellyfin.Server.Migrations.Routines; + +/// +/// Part 2 Migration for NormalisedUsername. +/// +[JellyfinMigration("2026-05-22T09:23:04", nameof(UpdateNormalizedUsername), Stage = Stages.JellyfinMigrationStageTypes.CoreInitialisation)] +#pragma warning disable SA1649 // File name should match first type name +public class UpdateNormalizedUsername : IAsyncMigrationRoutine +#pragma warning restore SA1649 // File name should match first type name +{ + private readonly IDbContextFactory _contextFactory; + + /// + /// Initializes a new instance of the class. + /// + /// Db Context factory. + public UpdateNormalizedUsername(IDbContextFactory contextFactory) + { + _contextFactory = contextFactory; + } + + /// + public async Task PerformAsync(CancellationToken cancellationToken) + { + var dbContext = await _contextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); + await using (dbContext.ConfigureAwait(false)) + { + var users = await dbContext.Users.ToListAsync(cancellationToken).ConfigureAwait(false); + foreach (var user in users) + { + user.NormalizedUsername = user.Username.ToUpperInvariant(); + } + + await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); + } + } +} diff --git a/src/Jellyfin.Database/Jellyfin.Database.Providers.Sqlite/Migrations/20260522092303_AddNormalizedUsername.cs b/src/Jellyfin.Database/Jellyfin.Database.Providers.Sqlite/Migrations/20260522092303_AddNormalizedUsername.cs index 2b791199d2..4bee8c15b9 100644 --- a/src/Jellyfin.Database/Jellyfin.Database.Providers.Sqlite/Migrations/20260522092303_AddNormalizedUsername.cs +++ b/src/Jellyfin.Database/Jellyfin.Database.Providers.Sqlite/Migrations/20260522092303_AddNormalizedUsername.cs @@ -10,21 +10,13 @@ namespace Jellyfin.Server.Implementations.Migrations /// protected override void Up(MigrationBuilder migrationBuilder) { + // this is the first part of the migration. Add the column. migrationBuilder.AddColumn( name: "NormalizedUsername", table: "Users", type: "TEXT", maxLength: 255, nullable: true); - migrationBuilder.Sql(""" - UPDATE "Users" SET "NormalizedUsername" = UPPER("Username") - """); - migrationBuilder.AlterColumn( - name: "NormalizedUsername", - table: "Users", - type: "TEXT", - maxLength: 255, - nullable: false); } /// diff --git a/src/Jellyfin.Database/Jellyfin.Database.Providers.Sqlite/Migrations/20260522092305_UpdateNormalizedUsername.cs b/src/Jellyfin.Database/Jellyfin.Database.Providers.Sqlite/Migrations/20260522092305_UpdateNormalizedUsername.cs new file mode 100644 index 0000000000..4747ea4b38 --- /dev/null +++ b/src/Jellyfin.Database/Jellyfin.Database.Providers.Sqlite/Migrations/20260522092305_UpdateNormalizedUsername.cs @@ -0,0 +1,31 @@ +using Jellyfin.Database.Implementations; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Jellyfin.Server.Implementations.Migrations +{ + /// + [DbContext(typeof(JellyfinDbContext))] + [Migration("20260522092305_UpdateNormalizedUsername")] + public partial class UpdateNormalizedUsername : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + // this is the 3rd part of the NormalizedUsername migration. + migrationBuilder.AlterColumn( + name: "NormalizedUsername", + table: "Users", + type: "TEXT", + maxLength: 255, + nullable: false); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + } + } +}