Skip to content

Commit d4f258e

Browse files
tengfei8771droyad
andauthored
Enhance the behavior of obtaining default schema through database con… (#32)
* Enhance the behavior of obtaining default schema through database connection string by specifying the default schema through the SearchPath parameter in the connection string * 1.支持searchpath和search path两种情况 --------- Co-authored-by: Robert Wagner <[email protected]>
1 parent b19d9f7 commit d4f258e

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/dbup-postgresql/PostgresqlExtensions.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Data;
33
using System.Security.Cryptography.X509Certificates;
4+
using System.Text.RegularExpressions;
45
using DbUp;
56
using DbUp.Builder;
67
using DbUp.Engine.Output;
@@ -15,6 +16,7 @@
1516
/// </summary>
1617
public static class PostgresqlExtensions
1718
{
19+
private static readonly string pattern= @"(?i)Search\s?Path=([^;]+)";
1820
/// <summary>
1921
/// Creates an upgrader for PostgreSQL databases.
2022
/// </summary>
@@ -24,8 +26,21 @@ public static class PostgresqlExtensions
2426
/// A builder for a database upgrader designed for PostgreSQL databases.
2527
/// </returns>
2628
public static UpgradeEngineBuilder PostgresqlDatabase(this SupportedDatabases supported, string connectionString)
27-
=> PostgresqlDatabase(supported, connectionString, null);
28-
29+
=> PostgresqlDatabase(supported, connectionString, GetDefaultSchemaByConnectionString(connectionString));
30+
/// <summary>
31+
/// Get connection string use parameter SearchPath for defaultSchema
32+
/// </summary>
33+
/// <param name="connectionString">PostgreSQL database connection string.</param>
34+
/// <returns></returns>
35+
private static string GetDefaultSchemaByConnectionString(string connectionString)
36+
{
37+
Match match = Regex.Match(connectionString, pattern);
38+
if (match.Success)
39+
{
40+
return match.Groups[1].Value;
41+
}
42+
return null;
43+
}
2944
/// <summary>
3045
/// Creates an upgrader for PostgreSQL databases.
3146
/// </summary>

0 commit comments

Comments
 (0)