Skip to content

Commit 7c2f26f

Browse files
authored
Creating a Schema CR instance should require the Database name in which the schema should be created (#27)
* creating a Schema CR instance should require the Database name in which the schema should be created * fix failing tests which depend on SchemaCreate * fix failing tests for good
1 parent f664bda commit 7c2f26f

12 files changed

Lines changed: 94 additions & 52 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/DefaultPrivilegeCreate.java

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -234,29 +234,11 @@ private String getSchema() {
234234
return withSchema;
235235
}
236236

237-
var databaseName = getDatabase();
238-
if (databaseName.isBlank()) {
239-
databaseName = given.one()
240-
.database()
241-
.withClusterConnectionName(getClusterConnectionName())
242-
.withClusterConnectionNamespace(withClusterConnectionNamespace)
243-
.withReclaimPolicy(DELETE)
244-
.returnFirst()
245-
.getSpec()
246-
.getName();
247-
}
248-
249-
var clusterConnectionDb = given.one()
250-
.clusterConnection()
251-
.withName(getClusterConnectionName() + "-db")
252-
.withNamespace(withClusterConnectionNamespace)
253-
.withDatabase(databaseName)
254-
.returnFirst();
255-
256237
var item = given.one()
257238
.schema()
258-
.withClusterConnectionName(clusterConnectionDb.getMetadata().getName())
259-
.withClusterConnectionNamespace(clusterConnectionDb.getMetadata().getNamespace())
239+
.withClusterConnectionName(getClusterConnectionName())
240+
.withClusterConnectionNamespace(withClusterConnectionNamespace)
241+
.withDatabase(getDatabase())
260242
.withReclaimPolicy(DELETE)
261243
.returnFirst();
262244

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,9 @@ private String getSchema() {
236236
return withSchema;
237237
}
238238

239-
var databaseName = getDatabase();
240-
if (databaseName.isBlank()) {
241-
databaseName = given.one()
239+
var database = getDatabase();
240+
if (database.isBlank()) {
241+
database = given.one()
242242
.database()
243243
.withClusterConnectionName(getClusterConnectionName())
244244
.withClusterConnectionNamespace(withClusterConnectionNamespace)
@@ -248,17 +248,11 @@ private String getSchema() {
248248
.getName();
249249
}
250250

251-
var clusterConnectionDb = given.one()
252-
.clusterConnection()
253-
.withName(getClusterConnectionName() + "-db")
254-
.withNamespace(withClusterConnectionNamespace)
255-
.withDatabase(databaseName)
256-
.returnFirst();
257-
258251
var item = given.one()
259252
.schema()
260-
.withClusterConnectionName(clusterConnectionDb.getMetadata().getName())
261-
.withClusterConnectionNamespace(clusterConnectionDb.getMetadata().getNamespace())
253+
.withClusterConnectionName(getClusterConnectionName())
254+
.withClusterConnectionNamespace(withClusterConnectionNamespace)
255+
.withDatabase(database)
262256
.withReclaimPolicy(DELETE)
263257
.returnFirst();
264258

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: 31 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,21 @@ 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+
return withDatabase;
168+
}
142169
}

operator/src/test/java/it/aboutbits/postgresql/crd/defaultprivilege/DefaultPrivilegeReconcilerTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ void errorWhenUnsupportedMaintainTablePrivilege() {
328328
var schema = given.one()
329329
.schema()
330330
.withClusterConnectionName(clusterConnectionDb.getMetadata().getName())
331+
.withDatabase(database.getSpec().getName())
331332
.withReclaimPolicy(DELETE)
332333
.returnFirst();
333334

@@ -497,6 +498,7 @@ void defaultPrivilegeOnTable(
497498
var schema = given.one()
498499
.schema()
499500
.withClusterConnectionName(clusterConnectionDb.getMetadata().getName())
501+
.withDatabase(database.getSpec().getName())
500502
.withReclaimPolicy(DELETE)
501503
.returnFirst();
502504

@@ -623,6 +625,7 @@ void defaultPrivilegeOnSequence() {
623625
var schema = given.one()
624626
.schema()
625627
.withClusterConnectionName(clusterConnectionDb.getMetadata().getName())
628+
.withDatabase(database.getSpec().getName())
626629
.withReclaimPolicy(DELETE)
627630
.returnFirst();
628631

0 commit comments

Comments
 (0)