Added asset id counter to migrations, fixed migrations async issue

This commit is contained in:
2025-10-12 17:55:21 +02:00
parent 5ff75e162d
commit 434b0952dc
4 changed files with 34 additions and 15 deletions

View File

@@ -71,3 +71,8 @@ public class AssetsTableViewModel
public string? AssetName { get; set; } public string? AssetName { get; set; }
public string? LocationName { get; set; } public string? LocationName { get; set; }
} }
public class AssetsMetadataModel
{
public int CnCounter { get; set; }
}

View File

@@ -32,7 +32,7 @@ builder.Services.AddElmah<XmlFileErrorLog>(Options =>
}); });
builder.Services.AddSingleton<LdapService>(); builder.Services.AddSingleton<LdapService>();
builder.Services.AddSingleton<MigrationService>(); builder.Services.AddHostedService<MigrationService>();
builder.Services.AddHealthChecks() builder.Services.AddHealthChecks()
.AddCheck<DatabaseHealthCheck>("DatabaseHealthCheck"); .AddCheck<DatabaseHealthCheck>("DatabaseHealthCheck");
@@ -123,8 +123,5 @@ app.MapControllerRoute(
app.MapHealthChecks("/healthz") app.MapHealthChecks("/healthz")
.RequireAuthorization(); .RequireAuthorization();
// Run migrations
using var scope = app.Services.CreateScope();
var migrationService = scope.ServiceProvider.GetRequiredService<MigrationService>();
app.Run(); app.Run();

View File

@@ -396,6 +396,11 @@ public async Task CreateAsset(LdapAttributeSet attributeSet)
await _conn.AddAsync(ldapEntry); await _conn.AddAsync(ldapEntry);
} }
public async Task ModifyAsync(string dn, LdapModification ldapModification)
{
await _conn.ModifyAsync(dn, ldapModification);
}
public void Dispose() public void Dispose()
{ {
if (_conn != null && _conn.Connected) if (_conn != null && _conn.Connected)

View File

@@ -2,9 +2,10 @@ using Berufsschule_HAM.Models;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Novell.Directory.Ldap; using Novell.Directory.Ldap;
using System.Reflection; using System.Reflection;
using System.Text.Json;
namespace Berufsschule_HAM.Services; namespace Berufsschule_HAM.Services;
public class MigrationService public class MigrationService : IHostedService
{ {
private readonly LdapService _ldapService; private readonly LdapService _ldapService;
private readonly ILogger<MigrationService> _logger; private readonly ILogger<MigrationService> _logger;
@@ -15,8 +16,15 @@ public class MigrationService
_ldapService = ldapService; _ldapService = ldapService;
_logger = logger; _logger = logger;
_ldapConfig = ldapConfig.Value; _ldapConfig = ldapConfig.Value;
MigrateAsync().ConfigureAwait(false);
} }
public async Task StartAsync(CancellationToken cancellationToken)
{
await MigrateAsync();
}
public async Task StopAsync(CancellationToken cancellationToken) { }
public async Task<MigrationModel> MigrateAsync() public async Task<MigrationModel> MigrateAsync()
{ {
MigrationModel migrationModel = await _ldapService.GetMigrationVersionAsync(); MigrationModel migrationModel = await _ldapService.GetMigrationVersionAsync();
@@ -37,17 +45,12 @@ public class MigrationService
{ {
_logger.LogInformation("Migrating LDAP database from version {version} to {methodVersion}", [version, methodVersion + 1]); _logger.LogInformation("Migrating LDAP database from version {version} to {methodVersion}", [version, methodVersion + 1]);
if (method is null) { continue; } if (method is null) { continue; }
#pragma warning disable CS8605 // Unboxing a possibly null value. Task<int>? invocation = (Task<int>?)method.Invoke(null, [_ldapService]) ?? throw new Exception("Invocation is null");
version = (int)method.Invoke(null, [_ldapService]); version = await invocation;
#pragma warning restore CS8605 // Unboxing a possibly null value.
}
}
if (version != migrationModel.Version)
{
migrationModel.Version = version; migrationModel.Version = version;
await _ldapService.UpdateMigrationVersionAsync(migrationModel); await _ldapService.UpdateMigrationVersionAsync(migrationModel);
} }
}
return migrationModel; return migrationModel;
} }
@@ -92,6 +95,15 @@ public class MigrationService
await TryCreateObjectIgnoreAlreadyExists(ldapService, ldapService.MigrationsBaseDn, migrationsAttributes); await TryCreateObjectIgnoreAlreadyExists(ldapService, ldapService.MigrationsBaseDn, migrationsAttributes);
return 1; return 1;
} }
public static async Task<int> 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) private static async Task TryCreateObjectIgnoreAlreadyExists(LdapService ldapService, string dn, LdapAttributeSet ldapAttributes)
{ {