Skip to content

Commit 26e27f5

Browse files
committed
Update Other Dependencies
1 parent 5cb8e38 commit 26e27f5

File tree

4 files changed

+125
-5
lines changed

4 files changed

+125
-5
lines changed

src/Tests/ApprovalFiles/NoPublicApiChanges.Run.approved.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,20 @@ public PostgresqlConnectionManager(string connectionString) { }
3333
public PostgresqlConnectionManager(Npgsql.NpgsqlDataSource datasource) { }
3434
public PostgresqlConnectionManager(string connectionString, System.Security.Cryptography.X509Certificates.X509Certificate2 certificate) { }
3535
public PostgresqlConnectionManager(string connectionString, DbUp.Postgresql.PostgresqlConnectionOptions connectionOptions) { }
36+
<<<<<<< Updated upstream
3637
public bool StandardConformingStrings { get; set; }
38+
=======
39+
>>>>>>> Stashed changes
3740
public override System.Collections.Generic.IEnumerable<string> SplitScriptIntoCommands(string scriptContents) { }
3841
}
3942
public class PostgresqlConnectionOptions
4043
{
4144
public PostgresqlConnectionOptions() { }
4245
public System.Security.Cryptography.X509Certificates.X509Certificate2 ClientCertificate { get; set; }
46+
<<<<<<< Updated upstream
4347
public string MasterDatabaseName { get; set; }
48+
=======
49+
>>>>>>> Stashed changes
4450
public System.Net.Security.RemoteCertificateValidationCallback UserCertificateValidationCallback { get; set; }
4551
}
4652
public class PostgresqlObjectParser : DbUp.Support.SqlObjectParser, DbUp.Engine.ISqlObjectParser

src/dbup-postgresql/PostgresqlConnectionManager.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class PostgresqlConnectionManager : DatabaseConnectionManager
2424
public PostgresqlConnectionManager(string connectionString)
2525
: base(new DelegateConnectionFactory(l => new NpgsqlConnection(connectionString)))
2626
{
27+
<<<<<<< Updated upstream
2728
}
2829

2930
/// <summary>
@@ -48,6 +49,18 @@ public PostgresqlConnectionManager(string connectionString, PostgresqlConnection
4849
: base(new DataSourceConnectionFactory(connectionString, connectionOptions))
4950
{
5051
}
52+
=======
53+
/// <summary>
54+
/// Create a new PostgreSQL database connection
55+
/// </summary>
56+
/// <param name="connectionString">The PostgreSQL connection string.</param>
57+
/// <param name="connectionOptions">Custom options to apply on the created connection</param>
58+
public PostgresqlConnectionManager(PostgresqlConnectionOptions connectionOptions)
59+
: base(new DelegateConnectionFactory(l =>
60+
{
61+
NpgsqlConnection databaseConnection = new NpgsqlConnection(connectionOptions.ConnectionString);
62+
databaseConnection.ApplyConnectionOptions(connectionOptions);
63+
>>>>>>> Stashed changes
5164

5265
/// <summary>
5366
/// Creates a new PostgreSQL database connection with a NpgsqlDatasource

src/dbup-postgresql/PostgresqlConnectionOptions.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
<<<<<<< Updated upstream
12
using System.Net.Security;
23
using System.Security.Cryptography.X509Certificates;
4+
=======
5+
using System;
6+
using System.Net.Security;
7+
using System.Security.Cryptography.X509Certificates;
8+
using DbUp.Engine.Output;
9+
>>>>>>> Stashed changes
310

411
namespace DbUp.Postgresql
512
{
@@ -8,6 +15,20 @@ namespace DbUp.Postgresql
815
/// </summary>
916
public class PostgresqlConnectionOptions
1017
{
18+
<<<<<<< Updated upstream
19+
=======
20+
21+
public PostgresqlConnectionOptions(string connectionString)
22+
{
23+
if (string.IsNullOrWhiteSpace(connectionString))
24+
throw new ArgumentNullException(nameof(connectionString));
25+
26+
ConnectionString = connectionString;
27+
}
28+
29+
public string ConnectionString { get; }
30+
31+
>>>>>>> Stashed changes
1132
/// <summary>
1233
/// Certificate for securing connection.
1334
/// </summary>
@@ -23,5 +44,10 @@ public class PostgresqlConnectionOptions
2344
/// The database to connect to initially. Default is 'postgres'.
2445
/// </summary>
2546
public string MasterDatabaseName { get; set; } = "postgres";
47+
<<<<<<< Updated upstream
48+
=======
49+
50+
public IUpgradeLog Logger { get; set; } = new ConsoleUpgradeLog();
51+
>>>>>>> Stashed changes
2652
}
2753
}

src/dbup-postgresql/PostgresqlExtensions.cs

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public static class PostgresqlExtensions
2626
/// A builder for a database upgrader designed for PostgreSQL databases.
2727
/// </returns>
2828
public static UpgradeEngineBuilder PostgresqlDatabase(this SupportedDatabases supported, string connectionString)
29+
<<<<<<< Updated upstream
2930
=> PostgresqlDatabase(supported, connectionString, GetDefaultSchemaByConnectionString(connectionString));
3031
/// <summary>
3132
/// Get connection string use parameter SearchPath for defaultSchema
@@ -52,17 +53,21 @@ private static string GetDefaultSchemaByConnectionString(string connectionString
5253
/// </returns>
5354
public static UpgradeEngineBuilder PostgresqlDatabase(this SupportedDatabases supported, string connectionString, string schema)
5455
=> PostgresqlDatabase(new PostgresqlConnectionManager(connectionString), schema);
56+
=======
57+
=> PostgresqlDatabase(supported, null, new PostgresqlConnectionOptions(connectionString));
58+
>>>>>>> Stashed changes
5559

5660
/// <summary>
5761
/// Creates an upgrader for PostgreSQL databases that use SSL.
5862
/// </summary>
5963
/// <param name="supported">Fluent helper type.</param>
6064
/// <param name="connectionString">PostgreSQL database connection string.</param>
6165
/// <param name="schema">The schema in which to check for changes</param>
62-
/// <param name="certificate">Certificate for securing connection.</param>
66+
/// <param name="connectionOptions">Connection options to set SSL parameters</param>
6367
/// <returns>
6468
/// A builder for a database upgrader designed for PostgreSQL databases.
6569
/// </returns>
70+
<<<<<<< Updated upstream
6671
public static UpgradeEngineBuilder PostgresqlDatabase(this SupportedDatabases supported, string connectionString, string schema, X509Certificate2 certificate)
6772
=> PostgresqlDatabase(new PostgresqlConnectionManager(connectionString, certificate), schema);
6873

@@ -99,6 +104,10 @@ public static UpgradeEngineBuilder PostgresqlDatabase(this SupportedDatabases su
99104
/// </returns>
100105
public static UpgradeEngineBuilder PostgresqlDatabase(IConnectionManager connectionManager)
101106
=> PostgresqlDatabase(connectionManager, null);
107+
=======
108+
public static UpgradeEngineBuilder PostgresqlDatabase(this SupportedDatabases supported, string schema, PostgresqlConnectionOptions connectionOptions)
109+
=> PostgresqlDatabase(new PostgresqlConnectionManager(connectionOptions), schema);
110+
>>>>>>> Stashed changes
102111

103112
/// <summary>
104113
/// Creates an upgrader for PostgreSQL databases.
@@ -108,7 +117,7 @@ public static UpgradeEngineBuilder PostgresqlDatabase(IConnectionManager connect
108117
/// <returns>
109118
/// A builder for a database upgrader designed for PostgreSQL databases.
110119
/// </returns>
111-
public static UpgradeEngineBuilder PostgresqlDatabase(IConnectionManager connectionManager, string schema)
120+
public static UpgradeEngineBuilder PostgresqlDatabase(IConnectionManager connectionManager, string schema = null)
112121
{
113122
var builder = new UpgradeEngineBuilder();
114123
builder.Configure(c => c.ConnectionManager = connectionManager);
@@ -126,18 +135,22 @@ public static UpgradeEngineBuilder PostgresqlDatabase(IConnectionManager connect
126135
/// <returns></returns>
127136
public static void PostgresqlDatabase(this SupportedDatabasesForEnsureDatabase supported, string connectionString)
128137
{
129-
PostgresqlDatabase(supported, connectionString, new ConsoleUpgradeLog());
138+
supported.PostgresqlDatabase(new PostgresqlConnectionOptions(connectionString));
130139
}
131140

132141
/// <summary>
133142
/// Ensures that the database specified in the connection string exists using SSL for the connection.
134143
/// </summary>
135144
/// <param name="supported">Fluent helper type.</param>
136145
/// <param name="connectionString">The connection string.</param>
137-
/// <param name="certificate">Certificate for securing connection.</param>
146+
/// <param name="connectionOptions">Connection SSL to customize SSL behaviour</param>
138147
/// <returns></returns>
139-
public static void PostgresqlDatabase(this SupportedDatabasesForEnsureDatabase supported, string connectionString, X509Certificate2 certificate)
148+
public static void PostgresqlDatabase(
149+
this SupportedDatabasesForEnsureDatabase supported,
150+
PostgresqlConnectionOptions connectionOptions
151+
)
140152
{
153+
<<<<<<< Updated upstream
141154
PostgresqlDatabase(supported, connectionString, new ConsoleUpgradeLog(), certificate);
142155
}
143156

@@ -320,27 +333,40 @@ PostgresqlConnectionOptions connectionOptions
320333
var masterConnectionStringBuilder = new NpgsqlConnectionStringBuilder(connectionString);
321334

322335
var databaseName = masterConnectionStringBuilder.Database;
336+
=======
337+
var masterConnectionStringBuilder = new NpgsqlConnectionStringBuilder(connectionOptions.ConnectionString);
338+
339+
var databaseName = masterConnectionStringBuilder.Database;
340+
>>>>>>> Stashed changes
323341

324342
if (string.IsNullOrEmpty(databaseName) || databaseName.Trim() == string.Empty)
325343
{
326344
throw new InvalidOperationException("The connection string does not specify a database name.");
327345
}
328346

347+
<<<<<<< Updated upstream
329348
if (databaseName == connectionOptions.MasterDatabaseName)
330349
{
331350
throw new InvalidOperationException("Database in connection string needs to be different than the master database in PostgresqlConnectionOptions.");
332351
}
333352

334353
masterConnectionStringBuilder.Database = connectionOptions.MasterDatabaseName;
335354
masterConnectionStringBuilder.SearchPath = "public";
355+
=======
356+
masterConnectionStringBuilder.Database = connectionOptions.MasterDatabaseName;
357+
>>>>>>> Stashed changes
336358

337359
var logMasterConnectionStringBuilder = new NpgsqlConnectionStringBuilder(masterConnectionStringBuilder.ConnectionString);
338360
if (!string.IsNullOrEmpty(logMasterConnectionStringBuilder.Password))
339361
{
340362
logMasterConnectionStringBuilder.Password = "******";
341363
}
342364

365+
<<<<<<< Updated upstream
343366
logger.LogDebug("Master ConnectionString => {0}", logMasterConnectionStringBuilder.ConnectionString);
367+
=======
368+
connectionOptions.Logger.LogInformation("Master ConnectionString => {0}", logMasterConnectionStringBuilder.ConnectionString);
369+
>>>>>>> Stashed changes
344370

345371
var factory = new DataSourceConnectionFactory(masterConnectionStringBuilder.ConnectionString, connectionOptions);
346372
using var connection = factory.CreateConnection();
@@ -355,6 +381,7 @@ PostgresqlConnectionOptions connectionOptions
355381
CommandType = CommandType.Text
356382
})
357383
{
384+
<<<<<<< Updated upstream
358385
var results = Convert.ToInt32(command.ExecuteScalar());
359386

360387
// if the database does not exist, we're done here...
@@ -363,6 +390,41 @@ PostgresqlConnectionOptions connectionOptions
363390
logger.LogInformation(@"Database {0} does not exist. Skipping delete operation.", databaseName);
364391
return;
365392
}
393+
=======
394+
connection.ApplyConnectionOptions(connectionOptions);
395+
connection.Open();
396+
397+
var sqlCommandText =
398+
$@"SELECT case WHEN oid IS NOT NULL THEN 1 ELSE 0 end FROM pg_database WHERE datname = '{databaseName}' limit 1;";
399+
400+
// check to see if the database already exists..
401+
using (var command = new NpgsqlCommand(sqlCommandText, connection)
402+
{
403+
CommandType = CommandType.Text
404+
})
405+
{
406+
var results = Convert.ToInt32(command.ExecuteScalar());
407+
408+
// if the database exists, we're done here...
409+
if (results == 1)
410+
{
411+
return;
412+
}
413+
}
414+
415+
sqlCommandText = $"create database \"{databaseName}\";";
416+
417+
// Create the database...
418+
using (var command = new NpgsqlCommand(sqlCommandText, connection)
419+
{
420+
CommandType = CommandType.Text
421+
})
422+
{
423+
command.ExecuteNonQuery();
424+
}
425+
426+
connectionOptions.Logger.LogInformation(@"Created database {0}", databaseName);
427+
>>>>>>> Stashed changes
366428
}
367429

368430
// prevent new connections to the database
@@ -415,4 +477,17 @@ public static UpgradeEngineBuilder JournalToPostgresqlTable(this UpgradeEngineBu
415477
builder.Configure(c => c.Journal = new PostgresqlTableJournal(() => c.ConnectionManager, () => c.Log, schema, table));
416478
return builder;
417479
}
480+
481+
internal static void ApplyConnectionOptions(this NpgsqlConnection connection, PostgresqlConnectionOptions connectionOptions)
482+
{
483+
if (connectionOptions?.ClientCertificate != null)
484+
{
485+
connection.ProvideClientCertificatesCallback +=
486+
certs => certs.Add(connectionOptions.ClientCertificate);
487+
}
488+
if (connectionOptions?.UserCertificateValidationCallback != null)
489+
{
490+
connection.UserCertificateValidationCallback = connectionOptions.UserCertificateValidationCallback;
491+
}
492+
}
418493
}

0 commit comments

Comments
 (0)