Skip to content

Commit 1f95905

Browse files
committed
Merge branch 'release/6.0.0' into targetNet8
2 parents 00b2fa0 + 49956ad commit 1f95905

File tree

5 files changed

+153
-163
lines changed

5 files changed

+153
-163
lines changed

src/dbup-postgresql/PostgresqlConnectionManager.cs

Lines changed: 62 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,79 +4,78 @@
44
using DbUp.Engine.Transactions;
55
using Npgsql;
66

7-
namespace DbUp.Postgresql
7+
namespace DbUp.Postgresql;
8+
9+
/// <summary>
10+
/// Manages PostgreSQL database connections.
11+
/// </summary>
12+
public class PostgresqlConnectionManager : DatabaseConnectionManager
813
{
914
/// <summary>
10-
/// Manages PostgreSQL database connections.
15+
/// Disallow single quotes to be escaped with a backslash (\')
16+
/// </summary>
17+
public bool StandardConformingStrings { get; set; } = true;
18+
19+
/// <summary>
20+
/// Creates a new PostgreSQL database connection.
1121
/// </summary>
12-
public class PostgresqlConnectionManager : DatabaseConnectionManager
22+
/// <param name="connectionString">The PostgreSQL connection string.</param>
23+
public PostgresqlConnectionManager(string connectionString)
24+
: base(new DelegateConnectionFactory(l => new NpgsqlConnection(connectionString)))
1325
{
14-
/// <summary>
15-
/// Disallow single quotes to be escaped with a backslash (\')
16-
/// </summary>
17-
public bool StandardConformingStrings { get; set; } = true;
26+
}
1827

19-
/// <summary>
20-
/// Creates a new PostgreSQL database connection.
21-
/// </summary>
22-
/// <param name="connectionString">The PostgreSQL connection string.</param>
23-
public PostgresqlConnectionManager(string connectionString)
24-
: base(new DelegateConnectionFactory(l => new NpgsqlConnection(connectionString)))
28+
/// <summary>
29+
/// Creates a new PostgreSQL database connection with a certificate.
30+
/// </summary>
31+
/// <param name="connectionString">The PostgreSQL connection string.</param>
32+
/// <param name="certificate">Certificate for securing connection.</param>
33+
public PostgresqlConnectionManager(string connectionString, X509Certificate2 certificate)
34+
: this(connectionString, new PostgresqlConnectionOptions
2535
{
26-
}
36+
ClientCertificate = certificate
37+
})
38+
{
39+
}
2740

28-
/// <summary>
29-
/// Creates a new PostgreSQL database connection with a certificate.
30-
/// </summary>
31-
/// <param name="connectionString">The PostgreSQL connection string.</param>
32-
/// <param name="certificate">Certificate for securing connection.</param>
33-
public PostgresqlConnectionManager(string connectionString, X509Certificate2 certificate)
34-
: this(connectionString, new PostgresqlConnectionOptions
41+
/// <summary>
42+
/// Create a new PostgreSQL database connection
43+
/// </summary>
44+
/// <param name="connectionString">The PostgreSQL connection string.</param>
45+
/// <param name="connectionOptions">Custom options to apply on the created connection</param>
46+
public PostgresqlConnectionManager(string connectionString, PostgresqlConnectionOptions connectionOptions)
47+
: base(new DelegateConnectionFactory(l =>
3548
{
36-
ClientCertificate = certificate
37-
})
38-
{
39-
}
40-
41-
/// <summary>
42-
/// Create a new PostgreSQL database connection
43-
/// </summary>
44-
/// <param name="connectionString">The PostgreSQL connection string.</param>
45-
/// <param name="connectionOptions">Custom options to apply on the created connection</param>
46-
public PostgresqlConnectionManager(string connectionString, PostgresqlConnectionOptions connectionOptions)
47-
: base(new DelegateConnectionFactory(l =>
48-
{
49-
NpgsqlConnection databaseConnection = new NpgsqlConnection(connectionString);
50-
databaseConnection.ApplyConnectionOptions(connectionOptions);
49+
NpgsqlConnection databaseConnection = new NpgsqlConnection(connectionString);
50+
databaseConnection.ApplyConnectionOptions(connectionOptions);
5151

52-
return databaseConnection;
53-
}
54-
))
55-
{
56-
}
52+
return databaseConnection;
53+
}
54+
))
55+
{
56+
}
5757

58-
/// <summary>
59-
/// Creates a new PostgreSQL database connection with a NpgsqlDatasource
60-
/// </summary>
61-
/// <param name="datasource">The PostgreSQL NpgsqlDataSource.</param>
62-
public PostgresqlConnectionManager(NpgsqlDataSource datasource)
63-
: base(new DelegateConnectionFactory(l => datasource.CreateConnection()))
64-
{
65-
}
58+
/// <summary>
59+
/// Creates a new PostgreSQL database connection with a NpgsqlDatasource
60+
/// </summary>
61+
/// <param name="datasource">The PostgreSQL NpgsqlDataSource.</param>
62+
public PostgresqlConnectionManager(NpgsqlDataSource datasource)
63+
: base(new DelegateConnectionFactory(l => datasource.CreateConnection()))
64+
{
65+
}
6666

67-
/// <summary>
68-
/// Splits the statements in the script using the ";" character.
69-
/// </summary>
70-
/// <param name="scriptContents">The contents of the script to split.</param>
71-
public override IEnumerable<string> SplitScriptIntoCommands(string scriptContents)
72-
{
73-
var scriptStatements =
74-
PostgresqlQueryParser.ParseRawQuery(scriptContents, StandardConformingStrings)
75-
.Select(x => x.Trim())
76-
.Where(x => x.Length > 0)
77-
.ToArray();
67+
/// <summary>
68+
/// Splits the statements in the script using the ";" character.
69+
/// </summary>
70+
/// <param name="scriptContents">The contents of the script to split.</param>
71+
public override IEnumerable<string> SplitScriptIntoCommands(string scriptContents)
72+
{
73+
var scriptStatements =
74+
PostgresqlQueryParser.ParseRawQuery(scriptContents, StandardConformingStrings)
75+
.Select(x => x.Trim())
76+
.Where(x => x.Length > 0)
77+
.ToArray();
7878

79-
return scriptStatements;
80-
}
79+
return scriptStatements;
8180
}
82-
}
81+
}
Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
using DbUp.Support;
22

3-
namespace DbUp.Postgresql
4-
{
5-
/// <summary>
6-
/// Parses Sql Objects and performs quoting functions.
7-
/// </summary>
8-
public class PostgresqlObjectParser : SqlObjectParser
9-
{
10-
public PostgresqlObjectParser() : base("\"", "\"")
11-
{
12-
}
13-
}
14-
}
3+
namespace DbUp.Postgresql;
4+
5+
/// <summary>
6+
/// Parses Sql Objects and performs quoting functions.
7+
/// </summary>
8+
public class PostgresqlObjectParser() : SqlObjectParser("\"", "\"");
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
using DbUp.Engine;
22

3-
namespace DbUp.Postgresql
3+
namespace DbUp.Postgresql;
4+
5+
/// <summary>
6+
/// This preprocessor makes adjustments to your sql to make it compatible with PostgreSQL.
7+
/// </summary>
8+
public class PostgresqlPreprocessor : IScriptPreprocessor
49
{
510
/// <summary>
6-
/// This preprocessor makes adjustments to your sql to make it compatible with PostgreSQL.
11+
/// Performs some preprocessing step on a PostgreSQL script.
712
/// </summary>
8-
public class PostgresqlPreprocessor : IScriptPreprocessor
9-
{
10-
/// <summary>
11-
/// Performs some preprocessing step on a PostgreSQL script.
12-
/// </summary>
13-
public string Process(string contents) => contents;
14-
}
15-
}
13+
public string Process(string contents) => contents;
14+
}

src/dbup-postgresql/PostgresqlScriptExecutor.cs

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,42 @@
66
using DbUp.Support;
77
using Npgsql;
88

9-
namespace DbUp.Postgresql
9+
namespace DbUp.Postgresql;
10+
11+
/// <summary>
12+
/// An implementation of <see cref="ScriptExecutor"/> that executes against a PostgreSQL database.
13+
/// </summary>
14+
public class PostgresqlScriptExecutor : ScriptExecutor
1015
{
1116
/// <summary>
12-
/// An implementation of <see cref="ScriptExecutor"/> that executes against a PostgreSQL database.
17+
/// Initializes an instance of the <see cref="PostgresqlScriptExecutor"/> class.
1318
/// </summary>
14-
public class PostgresqlScriptExecutor : ScriptExecutor
19+
/// <param name="connectionManagerFactory"></param>
20+
/// <param name="log">The logging mechanism.</param>
21+
/// <param name="schema">The schema that contains the table.</param>
22+
/// <param name="variablesEnabled">Function that returns <c>true</c> if variables should be replaced, <c>false</c> otherwise.</param>
23+
/// <param name="scriptPreprocessors">Script Preprocessors in addition to variable substitution</param>
24+
/// <param name="journalFactory">Database journal</param>
25+
public PostgresqlScriptExecutor(Func<IConnectionManager> connectionManagerFactory, Func<IUpgradeLog> log, string schema, Func<bool> variablesEnabled,
26+
IEnumerable<IScriptPreprocessor> scriptPreprocessors, Func<IJournal> journalFactory)
27+
: base(connectionManagerFactory, new PostgresqlObjectParser(), log, schema, variablesEnabled, scriptPreprocessors, journalFactory)
1528
{
16-
/// <summary>
17-
/// Initializes an instance of the <see cref="PostgresqlScriptExecutor"/> class.
18-
/// </summary>
19-
/// <param name="connectionManagerFactory"></param>
20-
/// <param name="log">The logging mechanism.</param>
21-
/// <param name="schema">The schema that contains the table.</param>
22-
/// <param name="variablesEnabled">Function that returns <c>true</c> if variables should be replaced, <c>false</c> otherwise.</param>
23-
/// <param name="scriptPreprocessors">Script Preprocessors in addition to variable substitution</param>
24-
/// <param name="journalFactory">Database journal</param>
25-
public PostgresqlScriptExecutor(Func<IConnectionManager> connectionManagerFactory, Func<IUpgradeLog> log, string schema, Func<bool> variablesEnabled,
26-
IEnumerable<IScriptPreprocessor> scriptPreprocessors, Func<IJournal> journalFactory)
27-
: base(connectionManagerFactory, new PostgresqlObjectParser(), log, schema, variablesEnabled, scriptPreprocessors, journalFactory)
28-
{
29-
}
29+
}
3030

31-
protected override string GetVerifySchemaSql(string schema) => $"CREATE SCHEMA IF NOT EXISTS {schema}";
31+
protected override string GetVerifySchemaSql(string schema) => $"CREATE SCHEMA IF NOT EXISTS {schema}";
3232

33-
protected override void ExecuteCommandsWithinExceptionHandler(int index, SqlScript script, Action executeCommand)
33+
protected override void ExecuteCommandsWithinExceptionHandler(int index, SqlScript script, Action executeCommand)
34+
{
35+
try
36+
{
37+
executeCommand();
38+
}
39+
catch (PostgresException exception)
3440
{
35-
try
36-
{
37-
executeCommand();
38-
}
39-
catch (PostgresException exception)
40-
{
41-
Log().LogInformation("Npgsql exception has occurred in script: '{0}'", script.Name);
42-
Log().LogError("Script block number: {0}; Block line {1}; Position: {2}; Message: {3}", index, exception.Line, exception.Position, exception.Message);
43-
Log().LogError(exception.ToString());
44-
throw;
45-
}
41+
Log().LogInformation("Npgsql exception has occurred in script: '{0}'", script.Name);
42+
Log().LogError("Script block number: {0}; Block line {1}; Position: {2}; Message: {3}", index, exception.Line, exception.Position, exception.Message);
43+
Log().LogError(exception.ToString());
44+
throw;
4645
}
4746
}
48-
}
47+
}

src/dbup-postgresql/PostgresqlTableJournal.cs

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,70 +5,69 @@
55
using DbUp.Engine.Transactions;
66
using DbUp.Support;
77

8-
namespace DbUp.Postgresql
8+
namespace DbUp.Postgresql;
9+
10+
/// <summary>
11+
/// An implementation of the <see cref="IJournal"/> interface which tracks version numbers for a
12+
/// PostgreSQL database using a table called SchemaVersions.
13+
/// </summary>
14+
public class PostgresqlTableJournal : TableJournal
915
{
1016
/// <summary>
11-
/// An implementation of the <see cref="IJournal"/> interface which tracks version numbers for a
12-
/// PostgreSQL database using a table called SchemaVersions.
17+
/// Creates a new PostgreSQL table journal.
1318
/// </summary>
14-
public class PostgresqlTableJournal : TableJournal
19+
/// <param name="connectionManager">The PostgreSQL connection manager.</param>
20+
/// <param name="logger">The upgrade logger.</param>
21+
/// <param name="schema">The name of the schema the journal is stored in.</param>
22+
/// <param name="tableName">The name of the journal table.</param>
23+
public PostgresqlTableJournal(Func<IConnectionManager> connectionManager, Func<IUpgradeLog> logger, string schema, string tableName)
24+
: base(connectionManager, logger, new PostgresqlObjectParser(), schema, tableName)
1525
{
16-
/// <summary>
17-
/// Creates a new PostgreSQL table journal.
18-
/// </summary>
19-
/// <param name="connectionManager">The PostgreSQL connection manager.</param>
20-
/// <param name="logger">The upgrade logger.</param>
21-
/// <param name="schema">The name of the schema the journal is stored in.</param>
22-
/// <param name="tableName">The name of the journal table.</param>
23-
public PostgresqlTableJournal(Func<IConnectionManager> connectionManager, Func<IUpgradeLog> logger, string schema, string tableName)
24-
: base(connectionManager, logger, new PostgresqlObjectParser(), schema, tableName)
25-
{
26-
}
26+
}
2727

28-
protected override IDbCommand GetInsertScriptCommand(Func<IDbCommand> dbCommandFactory, SqlScript script)
29-
{
30-
// EnableSqlRewriting is enabled by default, and needs to be explicitly disabled
31-
bool enableSqlRewriting = !AppContext.TryGetSwitch("Npgsql.EnableSqlRewriting", out bool enabled) || enabled;
28+
protected override IDbCommand GetInsertScriptCommand(Func<IDbCommand> dbCommandFactory, SqlScript script)
29+
{
30+
// EnableSqlRewriting is enabled by default, and needs to be explicitly disabled
31+
bool enableSqlRewriting = !AppContext.TryGetSwitch("Npgsql.EnableSqlRewriting", out bool enabled) || enabled;
3232

33-
if (enableSqlRewriting)
34-
return base.GetInsertScriptCommand(dbCommandFactory, script);
33+
if (enableSqlRewriting)
34+
return base.GetInsertScriptCommand(dbCommandFactory, script);
3535

36-
// Use positional parameters instead of named parameters
37-
var command = dbCommandFactory();
36+
// Use positional parameters instead of named parameters
37+
var command = dbCommandFactory();
3838

39-
var scriptNameParam = command.CreateParameter();
40-
scriptNameParam.Value = script.Name;
41-
command.Parameters.Add(scriptNameParam);
39+
var scriptNameParam = command.CreateParameter();
40+
scriptNameParam.Value = script.Name;
41+
command.Parameters.Add(scriptNameParam);
4242

43-
var appliedParam = command.CreateParameter();
44-
appliedParam.Value = DateTime.Now;
45-
command.Parameters.Add(appliedParam);
43+
var appliedParam = command.CreateParameter();
44+
appliedParam.Value = DateTime.Now;
45+
command.Parameters.Add(appliedParam);
4646

47-
command.CommandText = GetInsertJournalEntrySql("$1", "$2");
48-
command.CommandType = CommandType.Text;
49-
return command;
50-
}
47+
command.CommandText = GetInsertJournalEntrySql("$1", "$2");
48+
command.CommandType = CommandType.Text;
49+
return command;
50+
}
5151

52-
protected override string GetInsertJournalEntrySql(string scriptName, string applied)
53-
{
54-
return $"insert into {FqSchemaTableName} (ScriptName, Applied) values ({scriptName}, {applied})";
55-
}
52+
protected override string GetInsertJournalEntrySql(string scriptName, string applied)
53+
{
54+
return $"insert into {FqSchemaTableName} (ScriptName, Applied) values ({scriptName}, {applied})";
55+
}
5656

57-
protected override string GetJournalEntriesSql()
58-
{
59-
return $"select ScriptName from {FqSchemaTableName} order by ScriptName";
60-
}
57+
protected override string GetJournalEntriesSql()
58+
{
59+
return $"select ScriptName from {FqSchemaTableName} order by ScriptName";
60+
}
6161

62-
protected override string CreateSchemaTableSql(string quotedPrimaryKeyName)
63-
{
64-
return
65-
$@"CREATE TABLE {FqSchemaTableName}
62+
protected override string CreateSchemaTableSql(string quotedPrimaryKeyName)
63+
{
64+
return
65+
$@"CREATE TABLE {FqSchemaTableName}
6666
(
6767
schemaversionsid serial NOT NULL,
6868
scriptname character varying(255) NOT NULL,
6969
applied timestamp without time zone NOT NULL,
7070
CONSTRAINT {quotedPrimaryKeyName} PRIMARY KEY (schemaversionsid)
7171
)";
72-
}
7372
}
74-
}
73+
}

0 commit comments

Comments
 (0)