Skip to content

Commit be7a252

Browse files
committed
creating a Schema CR instance should require the Database name in which the schema should be created
1 parent dbdffd7 commit be7a252

8 files changed

Lines changed: 85 additions & 19 deletions

File tree

docs/schema.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The `Schema` Custom Resource Definition (CRD) is responsible for managing Postgr
77
| Field | Type | Description | Required | Immutable |
88
|-----------------|--------------------|----------------------------------------------------------------------------------------------------|----------|-----------|
99
| `clusterRef` | `ClusterReference` | Reference to the `ClusterConnection` to use. | Yes | No |
10+
| `database` | `string` | The name of the database in which the schema is created. | Yes | Yes |
1011
| `name` | `string` | The name of the schema to create. | Yes | Yes |
1112
| `owner` | `string` | The owner of the schema. | No | No |
1213
| `reclaimPolicy` | `string` | The policy for reclaiming the schema when the CR is deleted. Values: `Retain` (Default), `Delete`. | No | No |
@@ -35,6 +36,7 @@ metadata:
3536
spec:
3637
clusterRef:
3738
name: my-postgres-connection
39+
database: my_database
3840
name: my_schema
3941
owner: my_role
4042
reclaimPolicy: Retain

operator/src/main/java/it/aboutbits/postgresql/crd/schema/SchemaReconciler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,12 @@ public UpdateControl<Schema> reconcile(
6767
.rescheduleAfter(60, TimeUnit.SECONDS);
6868
}
6969

70+
var database = spec.getDatabase();
7071
var clusterConnection = clusterConnectionOptional.get();
7172

7273
UpdateControl<Schema> updateControl;
7374

74-
try (var dsl = contextFactory.getDSLContext(clusterConnection)) {
75+
try (var dsl = contextFactory.getDSLContext(clusterConnection, database)) {
7576
// Run everything in a single transaction
7677
updateControl = dsl.transactionResult(
7778
cfg -> reconcileInTransaction(
@@ -149,9 +150,10 @@ public DeleteControl cleanup(
149150
.rescheduleAfter(60, TimeUnit.SECONDS);
150151
}
151152

153+
var database = spec.getDatabase();
152154
var clusterConnection = clusterConnectionOptional.get();
153155

154-
try (var dsl = contextFactory.getDSLContext(clusterConnection)) {
156+
try (var dsl = contextFactory.getDSLContext(clusterConnection, database)) {
155157
schemaService.dropSchema(dsl, spec);
156158

157159
return DeleteControl.defaultDelete();

operator/src/main/java/it/aboutbits/postgresql/crd/schema/SchemaSpec.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ public class SchemaSpec {
1616
@Required
1717
private ClusterReference clusterRef = new ClusterReference();
1818

19+
@Required
20+
@ValidationRule(
21+
value = "self == oldSelf",
22+
message = "The Schema database is immutable. Moving a schema to another database requires dumping and restoring the schema to the new database."
23+
)
24+
@ValidationRule(
25+
value = "self.trim().size() > 0",
26+
message = "The Schema database must not be empty."
27+
)
28+
private String database = "";
29+
1930
@Required
2031
@ValidationRule(
2132
value = "self == oldSelf",

operator/src/test/java/it/aboutbits/postgresql/_support/testdata/persisted/creator/ClusterConnectionCreate.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ protected ClusterConnection create(int index) {
8282
);
8383

8484
var spec = new ClusterConnectionSpec();
85+
8586
spec.setHost(getHost());
8687
spec.setPort(getPort());
8788
spec.setDatabase(getDatabase());

operator/src/test/java/it/aboutbits/postgresql/_support/testdata/persisted/creator/DatabaseCreate.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,16 @@ protected Database create(int index) {
7272
.build()
7373
);
7474

75-
var spec = new DatabaseSpec();
76-
spec.setName(name);
77-
spec.setReclaimPolicy(withReclaimPolicy);
78-
spec.setOwner(withOwner);
79-
8075
var clusterRef = new ClusterReference();
8176
clusterRef.setName(getClusterConnectionName());
8277
clusterRef.setNamespace(withClusterConnectionNamespace);
78+
79+
var spec = new DatabaseSpec();
80+
8381
spec.setClusterRef(clusterRef);
82+
spec.setName(name);
83+
spec.setReclaimPolicy(withReclaimPolicy);
84+
spec.setOwner(withOwner);
8485

8586
item.setSpec(spec);
8687

operator/src/test/java/it/aboutbits/postgresql/_support/testdata/persisted/creator/RoleCreate.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ protected Role create(int index) {
9393
.build()
9494
);
9595

96-
var spec = new RoleSpec();
97-
spec.setName(name);
98-
spec.setComment(withComment);
99-
10096
var clusterRef = new ClusterReference();
10197
clusterRef.setName(getClusterConnectionName());
10298
clusterRef.setNamespace(withClusterConnectionNamespace);
103-
spec.setClusterRef(clusterRef);
10499

100+
var spec = new RoleSpec();
101+
102+
spec.setClusterRef(clusterRef);
103+
spec.setName(name);
104+
spec.setComment(withComment);
105105
spec.setPasswordSecretRef(withPasswordSecretRef);
106106

107107
if (withFlags != null) {

operator/src/test/java/it/aboutbits/postgresql/_support/testdata/persisted/creator/SchemaCreate.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import java.util.concurrent.TimeUnit;
1818

19+
import static it.aboutbits.postgresql.core.ReclaimPolicy.DELETE;
20+
1921
@NullMarked
2022
@Setter
2123
@Accessors(fluent = true, chain = true)
@@ -38,6 +40,9 @@ public class SchemaCreate extends TestDataCreator<Schema> {
3840
@Nullable
3941
private String withClusterConnectionNamespace;
4042

43+
@Nullable
44+
private String withDatabase;
45+
4146
private ReclaimPolicy withReclaimPolicy = ReclaimPolicy.RETAIN;
4247

4348
@Nullable
@@ -72,15 +77,20 @@ protected Schema create(int index) {
7277
.build()
7378
);
7479

75-
var spec = new SchemaSpec();
76-
spec.setName(name);
77-
spec.setReclaimPolicy(withReclaimPolicy);
78-
spec.setOwner(withOwner);
80+
// We have to create the database first which also modifies the specified withClusterConnectionName so the connection points to the newly created DB
81+
var database = getDatabase();
7982

8083
var clusterRef = new ClusterReference();
8184
clusterRef.setName(getClusterConnectionName());
8285
clusterRef.setNamespace(withClusterConnectionNamespace);
86+
87+
var spec = new SchemaSpec();
88+
8389
spec.setClusterRef(clusterRef);
90+
spec.setDatabase(database);
91+
spec.setName(name);
92+
spec.setReclaimPolicy(withReclaimPolicy);
93+
spec.setOwner(withOwner);
8494

8595
item.setSpec(spec);
8696

@@ -139,4 +149,30 @@ private String getClusterConnectionName() {
139149

140150
return clusterConnection.getMetadata().getName();
141151
}
152+
153+
private String getDatabase() {
154+
if (withDatabase != null) {
155+
return withDatabase;
156+
}
157+
158+
var item = given.one()
159+
.database()
160+
.withClusterConnectionName(getClusterConnectionName())
161+
.withClusterConnectionNamespace(withClusterConnectionNamespace)
162+
.withReclaimPolicy(DELETE)
163+
.returnFirst();
164+
165+
withDatabase = item.getSpec().getName();
166+
167+
var clusterConnectionDb = given.one()
168+
.clusterConnection()
169+
.withName(getClusterConnectionName() + "-db")
170+
.withNamespace(withClusterConnectionNamespace)
171+
.withDatabase(withDatabase)
172+
.returnFirst();
173+
174+
withClusterConnectionName = clusterConnectionDb.getMetadata().getName();
175+
176+
return withDatabase;
177+
}
142178
}

operator/src/test/java/it/aboutbits/postgresql/crd/schema/SchemaReconcilerTest.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,32 @@ void resetEnvironment() {
3939
@DisplayName("When a Schema is created, it should be reconciled to READY")
4040
void createSchema_andStatusReady() {
4141
// given
42-
var clusterConnection = given.one()
42+
var clusterConnectionMain = given.one()
4343
.clusterConnection()
4444
.withName("test-connection-schema")
4545
.returnFirst();
4646

47+
var database = given.one()
48+
.database()
49+
.withClusterConnectionName(clusterConnectionMain.getMetadata().getName())
50+
.returnFirst();
51+
52+
// Creating a second ClusterConnection CR is only required for the tests
53+
var clusterConnectionDb = given.one()
54+
.clusterConnection()
55+
.withName("test-connection-schema-db")
56+
.withDatabase(database.getSpec().getName())
57+
.returnFirst();
58+
4759
var now = OffsetDateTime.now(ZoneOffset.UTC);
4860
var schemaName = "test-schema";
4961

5062
// when
5163
var schema = given.one()
5264
.schema()
65+
.withDatabase(database.getName())
5366
.withName(schemaName)
54-
.withClusterConnectionName(clusterConnection.getMetadata().getName())
67+
.withClusterConnectionName(clusterConnectionDb.getMetadata().getName())
5568
.withReclaimPolicy(DELETE)
5669
.returnFirst();
5770

@@ -67,7 +80,7 @@ void createSchema_andStatusReady() {
6780
now
6881
);
6982

70-
var dsl = postgreSQLContextFactory.getDSLContext(clusterConnection);
83+
var dsl = postgreSQLContextFactory.getDSLContext(clusterConnectionDb);
7184

7285
assertThat(schemaService.schemaExists(dsl, schema.getSpec())).isTrue();
7386
}

0 commit comments

Comments
 (0)