From 434b0952dc6038c17644a5687155b416947a592f Mon Sep 17 00:00:00 2001 From: LD-Reborn Date: Sun, 12 Oct 2025 17:55:21 +0200 Subject: [PATCH] Added asset id counter to migrations, fixed migrations async issue --- src/Models/AssetsModel.cs | 5 +++++ src/Program.cs | 5 +---- src/Services/LdapService.cs | 5 +++++ src/Services/MigrationService.cs | 34 +++++++++++++++++++++----------- 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/Models/AssetsModel.cs b/src/Models/AssetsModel.cs index 80bf3cd..6d93c40 100644 --- a/src/Models/AssetsModel.cs +++ b/src/Models/AssetsModel.cs @@ -70,4 +70,9 @@ public class AssetsTableViewModel public required string AssetCn { get; set; } public string? AssetName { get; set; } public string? LocationName { get; set; } +} + +public class AssetsMetadataModel +{ + public int CnCounter { get; set; } } \ No newline at end of file diff --git a/src/Program.cs b/src/Program.cs index 9c35a95..7d4a7dd 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -32,7 +32,7 @@ builder.Services.AddElmah(Options => }); builder.Services.AddSingleton(); -builder.Services.AddSingleton(); +builder.Services.AddHostedService(); builder.Services.AddHealthChecks() .AddCheck("DatabaseHealthCheck"); @@ -123,8 +123,5 @@ app.MapControllerRoute( app.MapHealthChecks("/healthz") .RequireAuthorization(); -// Run migrations -using var scope = app.Services.CreateScope(); -var migrationService = scope.ServiceProvider.GetRequiredService(); app.Run(); diff --git a/src/Services/LdapService.cs b/src/Services/LdapService.cs index 2ff006d..d9dfaa5 100644 --- a/src/Services/LdapService.cs +++ b/src/Services/LdapService.cs @@ -396,6 +396,11 @@ public async Task CreateAsset(LdapAttributeSet attributeSet) await _conn.AddAsync(ldapEntry); } + public async Task ModifyAsync(string dn, LdapModification ldapModification) + { + await _conn.ModifyAsync(dn, ldapModification); + } + public void Dispose() { if (_conn != null && _conn.Connected) diff --git a/src/Services/MigrationService.cs b/src/Services/MigrationService.cs index b5717cd..8136c38 100644 --- a/src/Services/MigrationService.cs +++ b/src/Services/MigrationService.cs @@ -2,9 +2,10 @@ using Berufsschule_HAM.Models; using Microsoft.Extensions.Options; using Novell.Directory.Ldap; using System.Reflection; +using System.Text.Json; namespace Berufsschule_HAM.Services; -public class MigrationService +public class MigrationService : IHostedService { private readonly LdapService _ldapService; private readonly ILogger _logger; @@ -15,8 +16,15 @@ public class MigrationService _ldapService = ldapService; _logger = logger; _ldapConfig = ldapConfig.Value; - MigrateAsync().ConfigureAwait(false); } + + public async Task StartAsync(CancellationToken cancellationToken) + { + await MigrateAsync(); + } + + public async Task StopAsync(CancellationToken cancellationToken) { } + public async Task MigrateAsync() { MigrationModel migrationModel = await _ldapService.GetMigrationVersionAsync(); @@ -37,18 +45,13 @@ public class MigrationService { _logger.LogInformation("Migrating LDAP database from version {version} to {methodVersion}", [version, methodVersion + 1]); if (method is null) { continue; } -#pragma warning disable CS8605 // Unboxing a possibly null value. - version = (int)method.Invoke(null, [_ldapService]); -#pragma warning restore CS8605 // Unboxing a possibly null value. + Task? invocation = (Task?)method.Invoke(null, [_ldapService]) ?? throw new Exception("Invocation is null"); + version = await invocation; + migrationModel.Version = version; + await _ldapService.UpdateMigrationVersionAsync(migrationModel); } } - if (version != migrationModel.Version) - { - migrationModel.Version = version; - await _ldapService.UpdateMigrationVersionAsync(migrationModel); - } - return migrationModel; } @@ -92,6 +95,15 @@ public class MigrationService await TryCreateObjectIgnoreAlreadyExists(ldapService, ldapService.MigrationsBaseDn, migrationsAttributes); return 1; } + public static async Task UpdateFrom1Async(LdapService ldapService) + { + // Add the description attribute to ou=assets + AssetsMetadataModel assetsMetadataModel = new() { CnCounter = 1 }; + LdapAttribute ldapAttribute = new("description", JsonSerializer.Serialize(assetsMetadataModel)); + LdapModification ldapModification = new(LdapModification.Add, ldapAttribute); + await ldapService.ModifyAsync(ldapService.AssetsBaseDn, ldapModification); + return 2; + } private static async Task TryCreateObjectIgnoreAlreadyExists(LdapService ldapService, string dn, LdapAttributeSet ldapAttributes) {