11using Microsoft . EntityFrameworkCore ;
2+ using Microsoft . Extensions . Configuration ;
23using Microsoft . Extensions . DependencyInjection ;
34using Microsoft . Extensions . Hosting ;
45using Microsoft . Extensions . Logging ;
@@ -14,25 +15,39 @@ public class MigrationService : IHostedService
1415 {
1516 private readonly IServiceProvider _serviceProvider ;
1617 private readonly ILogger < MigrationService > _logger ;
18+ private readonly IConfiguration _configuration ;
1719
18- public MigrationService ( IServiceProvider serviceProvider , ILogger < MigrationService > logger )
20+ public MigrationService ( IServiceProvider serviceProvider , ILogger < MigrationService > logger , IConfiguration configuration )
1921 {
2022 _serviceProvider = serviceProvider ;
2123 _logger = logger ;
24+ _configuration = configuration ;
2225 }
2326
2427 public async Task StartAsync ( CancellationToken cancellationToken )
2528 {
2629 _logger . LogInformation ( "Starting database migration check..." ) ;
2730
2831 using var scope = _serviceProvider . CreateScope ( ) ;
29- var context = scope . ServiceProvider . GetRequiredService < PathfinderContext > ( ) ;
32+
33+ var migrationConnectionString = _configuration . GetConnectionString ( "PathfinderMigrationCS" ) ;
34+ var optionsBuilder = new DbContextOptionsBuilder < PathfinderContext > ( ) ;
35+ optionsBuilder . UseNpgsql ( migrationConnectionString ,
36+ npgsqlOptions =>
37+ {
38+ npgsqlOptions . CommandTimeout ( 60 ) ;
39+ npgsqlOptions . EnableRetryOnFailure (
40+ maxRetryCount : 3 ,
41+ maxRetryDelay : TimeSpan . FromSeconds ( 10 ) ,
42+ errorCodesToAdd : null ) ;
43+ } ) ;
44+
45+ var context = new PathfinderContext ( optionsBuilder . Options ) ;
3046
3147 try
3248 {
33- var connectionString = context . Database . GetConnectionString ( ) ;
34- var builder = new Npgsql . NpgsqlConnectionStringBuilder ( connectionString ) ;
35- _logger . LogDebug ( "Connecting to database: Host={Host}, Database={Database}, Username={Username}" ,
49+ var builder = new Npgsql . NpgsqlConnectionStringBuilder ( migrationConnectionString ) ;
50+ _logger . LogDebug ( "Connecting to database for migrations: Host={Host}, Database={Database}, Username={Username}" ,
3651 builder . Host , builder . Database , builder . Username ) ;
3752
3853 var canConnect = await context . Database . CanConnectAsync ( cancellationToken ) ;
0 commit comments