Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit df39072

Browse files
committed
Merge pull request #233 from chrismcv/Schema_fix
Support for single schema in search path for table creation
2 parents ca68d5a + c98a3dc commit df39072

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/ServiceStack.OrmLite.PostgreSQL.Tests/CreatePostgreSQLTablesTests.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using NUnit.Framework;
77
using ServiceStack.DataAnnotations;
88
using ServiceStack.OrmLite.Tests;
9+
using ServiceStack.Text;
910

1011
namespace ServiceStack.OrmLite.PostgreSQL.Tests
1112
{
@@ -48,5 +49,55 @@ private class CreatePostgreSQLTablesTests_dummy_table
4849
[StringLength(100)]
4950
public String String100Characters { get; set; }
5051
}
52+
53+
[Test]
54+
public void can_create_same_table_in_multiple_schemas_based_on_conn_string_search_path()
55+
{
56+
var builder = new Npgsql.NpgsqlConnectionStringBuilder(ConnectionString);
57+
var schema1 = "schema_1";
58+
var schema2 = "schema_2";
59+
using (var db = ConnectionString.OpenDbConnection())
60+
{
61+
CreateSchemaIfNotExists(db, schema1);
62+
CreateSchemaIfNotExists(db, schema2);
63+
}
64+
65+
builder.SearchPath = schema1;
66+
using (var dbS1 = builder.ToString().OpenDbConnection())
67+
{
68+
dbS1.DropTable<CreatePostgreSQLTablesTests_dummy_table>();
69+
dbS1.CreateTable<CreatePostgreSQLTablesTests_dummy_table>();
70+
Assert.That(dbS1.Count<CreatePostgreSQLTablesTests_dummy_table>(), Is.EqualTo(0));
71+
}
72+
builder.SearchPath = schema2;
73+
74+
using (var dbS2 = builder.ToString().OpenDbConnection())
75+
{
76+
dbS2.DropTable<CreatePostgreSQLTablesTests_dummy_table>();
77+
dbS2.CreateTable<CreatePostgreSQLTablesTests_dummy_table>();
78+
Assert.That(dbS2.Count<CreatePostgreSQLTablesTests_dummy_table>(), Is.EqualTo(0));
79+
}
80+
81+
}
82+
83+
private void CreateSchemaIfNotExists(System.Data.IDbConnection db, string name)
84+
{
85+
string createSchemaSQL = @"DO $$
86+
BEGIN
87+
88+
IF NOT EXISTS(
89+
SELECT schema_name
90+
FROM information_schema.schemata
91+
WHERE schema_name = '{0}'
92+
)
93+
THEN
94+
EXECUTE 'CREATE SCHEMA ""{0}""';
95+
END IF;
96+
97+
END
98+
$$;"
99+
.Fmt(name);
100+
db.ExecuteSql(createSchemaSQL);
101+
}
51102
}
52103
}

src/ServiceStack.OrmLite.PostgreSQL/PostgreSQLDialectProvider.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,15 @@ public override bool DoesTableExist(IDbCommand dbCmd, string tableName)
157157
{
158158
var sql = "SELECT COUNT(*) FROM pg_class WHERE relname = {0}"
159159
.SqlFormat(tableName);
160-
160+
var conn = dbCmd.Connection;
161+
if (conn != null)
162+
{
163+
var builder = new NpgsqlConnectionStringBuilder(conn.ConnectionString);
164+
// If a search path (schema) is specified, and there is only one, then assume the CREATE TABLE directive should apply to that schema.
165+
if (!String.IsNullOrEmpty(builder.SearchPath) && !builder.SearchPath.Contains(","))
166+
sql = "SELECT COUNT(*) FROM pg_class JOIN pg_catalog.pg_namespace n ON n.oid = pg_class.relnamespace WHERE relname = {0} AND nspname = {1}"
167+
.SqlFormat(tableName, builder.SearchPath);
168+
}
161169
dbCmd.CommandText = sql;
162170
var result = dbCmd.GetLongScalar();
163171

0 commit comments

Comments
 (0)