Skip to content
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/dbup-postgresql/PostgresqlExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Data;
using System.Security.Cryptography.X509Certificates;
using System.Text.RegularExpressions;
using DbUp;
using DbUp.Builder;
using DbUp.Engine.Output;
Expand All @@ -15,6 +16,7 @@
/// </summary>
public static class PostgresqlExtensions
{
private static readonly string pattern= @"(?i)SearchPath=([^;]+)";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to https://www.postgresql.org/docs/current/ddl-schemas.html

Default search path would be:

"$user", public

Normally to customize search path one would use:

SET search_path TO myschema,public;

In both cases search path contains multiple schemas delimited by comma.

This regex does not seem to respect multiple schemas in search path?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method only processes connection strings and does not support this parameter like 'search_path' in connection strings. If this parameter is not set, the database will default to reading public

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to https://www.npgsql.org/doc/connection-string-parameters.html parameter should be named "Search Path" and not "SearchPath"?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Common connection strings, like this
"Server=xxxx;Port=xxx;Database=xxx;User Id=xxxx;Password=xxxx;Pooling=false;Search Path=xxxx;IncludeErrorDetail=true;"
The schema parameters support SearchPath and Search Path and ignore case.
I modified the regular expression to support the above parameter types.

/// <summary>
/// Creates an upgrader for PostgreSQL databases.
/// </summary>
Expand All @@ -24,8 +26,21 @@ public static class PostgresqlExtensions
/// A builder for a database upgrader designed for PostgreSQL databases.
/// </returns>
public static UpgradeEngineBuilder PostgresqlDatabase(this SupportedDatabases supported, string connectionString)
=> PostgresqlDatabase(supported, connectionString, null);

=> PostgresqlDatabase(supported, connectionString, GetDefaultSchemaByConnectionString(connectionString));
/// <summary>
/// Get connection string use parameter SearchPath for defaultSchema
/// </summary>
/// <param name="connectionString">PostgreSQL database connection string.</param>
/// <returns></returns>
private static string GetDefaultSchemaByConnectionString(string connectionString)
{
Match match = Regex.Match(connectionString, pattern);
if (match.Success)
{
return match.Groups[1].Value;
}
return null;
}
/// <summary>
/// Creates an upgrader for PostgreSQL databases.
/// </summary>
Expand Down