@@ -3,6 +3,7 @@ namespace ServiceControl.Persistence.EFCore.PostgreSql;
33using Microsoft . EntityFrameworkCore ;
44using Microsoft . Extensions . DependencyInjection ;
55using ServiceControl . Persistence . EFCore . Abstractions ;
6+ using ServiceControl . Persistence . EFCore . DbContexts ;
67
78class PostgreSqlPersistence ( PostgreSqlPersisterSettings settings ) : BasePersistence , IPersistence
89{
@@ -17,6 +18,8 @@ public void AddInstaller(IServiceCollection services)
1718 {
1819 RegisterSettings ( services ) ;
1920 ConfigureDbContext ( services ) ;
21+
22+ services . AddScoped < IDatabaseMigrator , PostgreSqlDatabaseMigrator > ( ) ;
2023 }
2124
2225 void RegisterSettings ( IServiceCollection services )
@@ -26,7 +29,28 @@ void RegisterSettings(IServiceCollection services)
2629 services . AddSingleton ( settings ) ;
2730 }
2831
29- void ConfigureDbContext ( IServiceCollection services ) =>
30- services . AddPooledDbContextFactory < PostgreSqlServiceControlDbContext > ( options =>
31- options . UseNpgsql ( settings . ConnectionString , npgsql => npgsql . CommandTimeout ( settings . CommandTimeout ) ) ) ;
32+ void ConfigureDbContext ( IServiceCollection services )
33+ {
34+ services . AddDbContext < PostgreSqlServiceControlDbContext > ( ( serviceProvider , options ) =>
35+ {
36+ options . UseNpgsql ( settings . ConnectionString , npgsqlOptions =>
37+ {
38+ npgsqlOptions . CommandTimeout ( settings . CommandTimeout ) ;
39+ if ( settings . EnableRetryOnFailure )
40+ {
41+ npgsqlOptions . EnableRetryOnFailure (
42+ maxRetryCount : settings . MaxRetryCount ,
43+ maxRetryDelay : TimeSpan . FromSeconds ( settings . MaxRetryDelayInSeconds ) ,
44+ errorCodesToAdd : null ) ;
45+ }
46+ } ) ;
47+
48+ if ( settings . EnableSensitiveDataLogging )
49+ {
50+ options . EnableSensitiveDataLogging ( ) ;
51+ }
52+ } , ServiceLifetime . Scoped ) ;
53+
54+ services . AddScoped < ServiceControlDbContext > ( provider => provider . GetRequiredService < PostgreSqlServiceControlDbContext > ( ) ) ;
55+ }
3256}
0 commit comments