@@ -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,24 @@ 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 (
163+ this SupportedDatabasesForEnsureDatabase supported ,
164+ string connectionString ,
165+ IUpgradeLog logger ,
166+ PostgresqlConnectionOptions connectionOptions
167+ )
129168 {
130169 if ( supported == null ) throw new ArgumentNullException ( "supported" ) ;
131170
@@ -137,15 +176,15 @@ private static void PostgresqlDatabase(this SupportedDatabasesForEnsureDatabase
137176 if ( logger == null ) throw new ArgumentNullException ( "logger" ) ;
138177
139178 var masterConnectionStringBuilder = new NpgsqlConnectionStringBuilder ( connectionString ) ;
140-
179+
141180 var databaseName = masterConnectionStringBuilder . Database ;
142181
143182 if ( string . IsNullOrEmpty ( databaseName ) || databaseName . Trim ( ) == string . Empty )
144183 {
145184 throw new InvalidOperationException ( "The connection string does not specify a database name." ) ;
146185 }
147186
148- masterConnectionStringBuilder . Database = "postgres" ;
187+ masterConnectionStringBuilder . Database = connectionOptions . MasterDatabaseName ;
149188
150189 var logMasterConnectionStringBuilder = new NpgsqlConnectionStringBuilder ( masterConnectionStringBuilder . ConnectionString ) ;
151190 if ( ! string . IsNullOrEmpty ( logMasterConnectionStringBuilder . Password ) )
@@ -157,11 +196,7 @@ private static void PostgresqlDatabase(this SupportedDatabasesForEnsureDatabase
157196
158197 using ( var connection = new NpgsqlConnection ( masterConnectionStringBuilder . ConnectionString ) )
159198 {
160- if ( certificate != null )
161- {
162- connection . ProvideClientCertificatesCallback +=
163- certs => certs . Add ( certificate ) ;
164- }
199+ connection . ApplyConnectionOptions ( connectionOptions ) ;
165200 connection . Open ( ) ;
166201
167202 var sqlCommandText =
@@ -209,4 +244,17 @@ public static UpgradeEngineBuilder JournalToPostgresqlTable(this UpgradeEngineBu
209244 builder . Configure ( c => c . Journal = new PostgresqlTableJournal ( ( ) => c . ConnectionManager , ( ) => c . Log , schema , table ) ) ;
210245 return builder ;
211246 }
247+
248+ internal static void ApplyConnectionOptions ( this NpgsqlConnection connection , PostgresqlConnectionOptions connectionOptions )
249+ {
250+ if ( connectionOptions ? . ClientCertificate != null )
251+ {
252+ connection . ProvideClientCertificatesCallback +=
253+ certs => certs . Add ( connectionOptions . ClientCertificate ) ;
254+ }
255+ if ( connectionOptions ? . UserCertificateValidationCallback != null )
256+ {
257+ connection . UserCertificateValidationCallback = connectionOptions . UserCertificateValidationCallback ;
258+ }
259+ }
212260}
0 commit comments