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

@@ -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)

View File

@@ -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<MigrationService> _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<MigrationModel> 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<int>? invocation = (Task<int>?)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<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)
{