|
6 | 6 | using NUnit.Framework; |
7 | 7 | using ServiceStack.DataAnnotations; |
8 | 8 | using ServiceStack.OrmLite.Tests; |
| 9 | +using ServiceStack.Text; |
9 | 10 |
|
10 | 11 | namespace ServiceStack.OrmLite.PostgreSQL.Tests |
11 | 12 | { |
@@ -48,5 +49,55 @@ private class CreatePostgreSQLTablesTests_dummy_table |
48 | 49 | [StringLength(100)] |
49 | 50 | public String String100Characters { get; set; } |
50 | 51 | } |
| 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 | + } |
51 | 102 | } |
52 | 103 | } |
0 commit comments