aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server/Migrations/Routines/20260522092304_UpdateNormalizedUsername.cs
blob: 8100d4759e18bea3fa12a6530a52107cd81e3544 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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;

/// <summary>
/// Part 2 Migration for NormalisedUsername.
/// </summary>
[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<JellyfinDbContext> _contextFactory;

    /// <summary>
    /// Initializes a new instance of the <see cref="UpdateNormalizedUsername"/> class.
    /// </summary>
    /// <param name="contextFactory">Db Context factory.</param>
    public UpdateNormalizedUsername(IDbContextFactory<JellyfinDbContext> contextFactory)
    {
        _contextFactory = contextFactory;
    }

    /// <inheritdoc/>
    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);
        }
    }
}