Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions TestHosts/TestHosts/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Shared.Logger;
using TestHosts.Common;
using TestHosts.Database.PataPawa;
using TestHosts.Database.TestBank;
Expand Down Expand Up @@ -61,7 +62,7 @@
protected override async Task ExecuteAsync(CancellationToken stoppingToken){
while (stoppingToken.IsCancellationRequested == false){
// TODO: may introduce a date filter
using ResolvedDbContext<PataPawaContext>? resolvedContext = this.Resolver.Resolve("PataPawaReadModel");

Check warning on line 65 in TestHosts/TestHosts/Startup.cs

View workflow job for this annotation

GitHub Actions / Build and Test Pull Requests

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

var pendingTransactions = await resolvedContext.Context.Transactions.Where(t => t.IsPending).OrderBy(t => t.Date).ToListAsync(stoppingToken);

Expand Down Expand Up @@ -111,34 +112,42 @@
public sealed class DatabaseInitializerHostedService : IHostedService
{
private readonly IServiceProvider _serviceProvider;
private readonly ILogger<DatabaseInitializerHostedService> _logger;

public DatabaseInitializerHostedService(IServiceProvider serviceProvider, ILogger<DatabaseInitializerHostedService> logger)
public DatabaseInitializerHostedService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
_logger = logger;
}

public async Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Starting database initialization...");
Logger.LogWarning("Starting database initialization...");

try
{
using var scope = _serviceProvider.CreateScope();
var pataPawaContext = scope.ServiceProvider.GetRequiredService<PataPawaContext>();
PataPawaContext pataPawaContext = scope.ServiceProvider.GetRequiredService<PataPawaContext>();

// Example: apply migrations or seed data
await pataPawaContext.Database.MigrateAsync(cancellationToken);
if (pataPawaContext.Database.IsRelational()) {
// Example: apply migrations or seed data
await pataPawaContext.Database.MigrateAsync(cancellationToken);
}
//else {
// await pataPawaContext.Database.EnsureCreatedAsync(cancellationToken);
//}

var bankContext = scope.ServiceProvider.GetRequiredService<TestBankContext>();
await bankContext.Database.MigrateAsync(cancellationToken);
TestBankContext bankContext = scope.ServiceProvider.GetRequiredService<TestBankContext>();
if (bankContext.Database.IsRelational()) {
await bankContext.Database.MigrateAsync(cancellationToken);
}
//else {
// await bankContext.Database.EnsureCreatedAsync(cancellationToken);
//}

_logger.LogInformation("Database initialization completed successfully.");
Logger.LogWarning("Database initialization completed successfully.");
}
catch (Exception ex)
{
_logger.LogError(ex, "Database initialization failed.");
Logger.LogError("Database initialization failed.", ex);
throw; // Let the host fail fast if initialization is critical
}
}
Expand Down
Loading