mirror of
https://github.com/LD-Reborn/Berufsschule_HAM.git
synced 2025-12-20 06:51:55 +00:00
Added asset id counter to migrations, fixed migrations async issue
This commit is contained in:
@@ -71,3 +71,8 @@ public class AssetsTableViewModel
|
||||
public string? AssetName { get; set; }
|
||||
public string? LocationName { get; set; }
|
||||
}
|
||||
|
||||
public class AssetsMetadataModel
|
||||
{
|
||||
public int CnCounter { get; set; }
|
||||
}
|
||||
@@ -32,7 +32,7 @@ builder.Services.AddElmah<XmlFileErrorLog>(Options =>
|
||||
});
|
||||
|
||||
builder.Services.AddSingleton<LdapService>();
|
||||
builder.Services.AddSingleton<MigrationService>();
|
||||
builder.Services.AddHostedService<MigrationService>();
|
||||
|
||||
builder.Services.AddHealthChecks()
|
||||
.AddCheck<DatabaseHealthCheck>("DatabaseHealthCheck");
|
||||
@@ -123,8 +123,5 @@ app.MapControllerRoute(
|
||||
app.MapHealthChecks("/healthz")
|
||||
.RequireAuthorization();
|
||||
|
||||
// Run migrations
|
||||
using var scope = app.Services.CreateScope();
|
||||
var migrationService = scope.ServiceProvider.GetRequiredService<MigrationService>();
|
||||
|
||||
app.Run();
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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,17 +45,12 @@ 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.
|
||||
}
|
||||
}
|
||||
|
||||
if (version != migrationModel.Version)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user