@@ -51,6 +51,19 @@ public static UpgradeEngineBuilder PostgresqlDatabase(this SupportedDatabases su
5151 public static UpgradeEngineBuilder PostgresqlDatabase ( this SupportedDatabases supported , string connectionString , string schema , X509Certificate2 certificate )
5252 => PostgresqlDatabase ( new PostgresqlConnectionManager ( connectionString , certificate ) , schema ) ;
5353
54+ /// <summary>
55+ /// Creates an upgrader for PostgreSQL databases that use SSL.
56+ /// </summary>
57+ /// <param name="supported">Fluent helper type.</param>
58+ /// <param name="connectionString">PostgreSQL database connection string.</param>
59+ /// <param name="schema">The schema in which to check for changes</param>
60+ /// <param name="connectionOptions">Connection options to set SSL parameters</param>
61+ /// <returns>
62+ /// A builder for a database upgrader designed for PostgreSQL databases.
63+ /// </returns>
64+ public static UpgradeEngineBuilder PostgresqlDatabase ( this SupportedDatabases supported , string connectionString , string schema , PostgresqlConnectionOptions connectionOptions )
65+ => PostgresqlDatabase ( new PostgresqlConnectionManager ( connectionString , connectionOptions ) , schema ) ;
66+
5467 /// <summary>
5568 /// Creates an upgrader for PostgreSQL databases.
5669 /// </summary>
@@ -113,6 +126,18 @@ public static void PostgresqlDatabase(this SupportedDatabasesForEnsureDatabase s
113126 PostgresqlDatabase ( supported , connectionString , new ConsoleUpgradeLog ( ) , certificate ) ;
114127 }
115128
129+ /// <summary>
130+ /// Ensures that the database specified in the connection string exists using SSL for the connection.
131+ /// </summary>
132+ /// <param name="supported">Fluent helper type.</param>
133+ /// <param name="connectionString">The connection string.</param>
134+ /// <param name="connectionOptions">Connection SSL to customize SSL behaviour</param>
135+ /// <returns></returns>
136+ public static void PostgresqlDatabase ( this SupportedDatabasesForEnsureDatabase supported , string connectionString , PostgresqlConnectionOptions connectionOptions )
137+ {
138+ PostgresqlDatabase ( supported , connectionString , new ConsoleUpgradeLog ( ) , connectionOptions ) ;
139+ }
140+
116141 /// <summary>
117142 /// Ensures that the database specified in the connection string exists.
118143 /// </summary>
@@ -122,10 +147,19 @@ public static void PostgresqlDatabase(this SupportedDatabasesForEnsureDatabase s
122147 /// <returns></returns>
123148 public static void PostgresqlDatabase ( this SupportedDatabasesForEnsureDatabase supported , string connectionString , IUpgradeLog logger )
124149 {
125- PostgresqlDatabase ( supported , connectionString , logger , null ) ;
150+ PostgresqlDatabase ( supported , connectionString , logger , ( PostgresqlConnectionOptions ) null ) ;
126151 }
127-
152+
128153 private static void PostgresqlDatabase ( this SupportedDatabasesForEnsureDatabase supported , string connectionString , IUpgradeLog logger , X509Certificate2 certificate )
154+ {
155+ var options = new PostgresqlConnectionOptions
156+ {
157+ ClientCertificate = certificate
158+ } ;
159+ PostgresqlDatabase ( supported , connectionString , logger , options ) ;
160+ }
161+
162+ private static void PostgresqlDatabase ( this SupportedDatabasesForEnsureDatabase supported , string connectionString , IUpgradeLog logger , PostgresqlConnectionOptions connectionOptions )
129163 {
130164 if ( supported == null ) throw new ArgumentNullException ( "supported" ) ;
131165
@@ -137,7 +171,7 @@ private static void PostgresqlDatabase(this SupportedDatabasesForEnsureDatabase
137171 if ( logger == null ) throw new ArgumentNullException ( "logger" ) ;
138172
139173 var masterConnectionStringBuilder = new NpgsqlConnectionStringBuilder ( connectionString ) ;
140-
174+
141175 var databaseName = masterConnectionStringBuilder . Database ;
142176
143177 if ( string . IsNullOrEmpty ( databaseName ) || databaseName . Trim ( ) == string . Empty )
@@ -157,11 +191,7 @@ private static void PostgresqlDatabase(this SupportedDatabasesForEnsureDatabase
157191
158192 using ( var connection = new NpgsqlConnection ( masterConnectionStringBuilder . ConnectionString ) )
159193 {
160- if ( certificate != null )
161- {
162- connection . ProvideClientCertificatesCallback +=
163- certs => certs . Add ( certificate ) ;
164- }
194+ connection . ApplyConnectionOptions ( connectionOptions ) ;
165195 connection . Open ( ) ;
166196
167197 var sqlCommandText = string . Format
@@ -216,4 +246,17 @@ public static UpgradeEngineBuilder JournalToPostgresqlTable(this UpgradeEngineBu
216246 builder . Configure ( c => c . Journal = new PostgresqlTableJournal ( ( ) => c . ConnectionManager , ( ) => c . Log , schema , table ) ) ;
217247 return builder ;
218248 }
249+
250+ internal static void ApplyConnectionOptions ( this NpgsqlConnection connection , PostgresqlConnectionOptions connectionOptions )
251+ {
252+ if ( connectionOptions ? . ClientCertificate != null )
253+ {
254+ connection . ProvideClientCertificatesCallback +=
255+ certs => certs . Add ( connectionOptions . ClientCertificate ) ;
256+ }
257+ if ( connectionOptions ? . UserCertificateValidationCallback != null )
258+ {
259+ connection . UserCertificateValidationCallback = connectionOptions . UserCertificateValidationCallback ;
260+ }
261+ }
219262}
0 commit comments