Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The `Schema` Custom Resource Definition (CRD) is responsible for managing Postgr
| Field | Type | Description | Required | Immutable |
|-----------------|--------------------|----------------------------------------------------------------------------------------------------|----------|-----------|
| `clusterRef` | `ClusterReference` | Reference to the `ClusterConnection` to use. | Yes | No |
| `database` | `string` | The name of the database in which the schema is created. | Yes | Yes |
| `name` | `string` | The name of the schema to create. | Yes | Yes |
| `owner` | `string` | The owner of the schema. | No | No |
| `reclaimPolicy` | `string` | The policy for reclaiming the schema when the CR is deleted. Values: `Retain` (Default), `Delete`. | No | No |
Expand Down Expand Up @@ -35,6 +36,7 @@ metadata:
spec:
clusterRef:
name: my-postgres-connection
database: my_database
name: my_schema
owner: my_role
reclaimPolicy: Retain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ public UpdateControl<Schema> reconcile(
.rescheduleAfter(60, TimeUnit.SECONDS);
}

var database = spec.getDatabase();
var clusterConnection = clusterConnectionOptional.get();

UpdateControl<Schema> updateControl;

try (var dsl = contextFactory.getDSLContext(clusterConnection)) {
try (var dsl = contextFactory.getDSLContext(clusterConnection, database)) {
// Run everything in a single transaction
updateControl = dsl.transactionResult(
cfg -> reconcileInTransaction(
Expand Down Expand Up @@ -149,9 +150,10 @@ public DeleteControl cleanup(
.rescheduleAfter(60, TimeUnit.SECONDS);
}

var database = spec.getDatabase();
var clusterConnection = clusterConnectionOptional.get();

try (var dsl = contextFactory.getDSLContext(clusterConnection)) {
try (var dsl = contextFactory.getDSLContext(clusterConnection, database)) {
schemaService.dropSchema(dsl, spec);

return DeleteControl.defaultDelete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ public class SchemaSpec {
@Required
private ClusterReference clusterRef = new ClusterReference();

@Required
@ValidationRule(
value = "self == oldSelf",
message = "The Schema database is immutable. Moving a schema to another database requires dumping and restoring the schema to the new database."
)
@ValidationRule(
value = "self.trim().size() > 0",
message = "The Schema database must not be empty."
)
private String database = "";

@Required
@ValidationRule(
value = "self == oldSelf",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ protected ClusterConnection create(int index) {
);

var spec = new ClusterConnectionSpec();

spec.setHost(getHost());
spec.setPort(getPort());
spec.setDatabase(getDatabase());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,16 @@ protected Database create(int index) {
.build()
);

var spec = new DatabaseSpec();
spec.setName(name);
spec.setReclaimPolicy(withReclaimPolicy);
spec.setOwner(withOwner);

var clusterRef = new ClusterReference();
clusterRef.setName(getClusterConnectionName());
clusterRef.setNamespace(withClusterConnectionNamespace);

var spec = new DatabaseSpec();

spec.setClusterRef(clusterRef);
spec.setName(name);
spec.setReclaimPolicy(withReclaimPolicy);
spec.setOwner(withOwner);

item.setSpec(spec);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,29 +234,11 @@ private String getSchema() {
return withSchema;
}

var databaseName = getDatabase();
if (databaseName.isBlank()) {
databaseName = given.one()
.database()
.withClusterConnectionName(getClusterConnectionName())
.withClusterConnectionNamespace(withClusterConnectionNamespace)
.withReclaimPolicy(DELETE)
.returnFirst()
.getSpec()
.getName();
}

var clusterConnectionDb = given.one()
.clusterConnection()
.withName(getClusterConnectionName() + "-db")
.withNamespace(withClusterConnectionNamespace)
.withDatabase(databaseName)
.returnFirst();

var item = given.one()
.schema()
.withClusterConnectionName(clusterConnectionDb.getMetadata().getName())
.withClusterConnectionNamespace(clusterConnectionDb.getMetadata().getNamespace())
.withClusterConnectionName(getClusterConnectionName())
.withClusterConnectionNamespace(withClusterConnectionNamespace)
.withDatabase(getDatabase())
.withReclaimPolicy(DELETE)
.returnFirst();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,9 @@ private String getSchema() {
return withSchema;
}

var databaseName = getDatabase();
if (databaseName.isBlank()) {
databaseName = given.one()
var database = getDatabase();
if (database.isBlank()) {
database = given.one()
.database()
.withClusterConnectionName(getClusterConnectionName())
.withClusterConnectionNamespace(withClusterConnectionNamespace)
Expand All @@ -248,17 +248,11 @@ private String getSchema() {
.getName();
}

var clusterConnectionDb = given.one()
.clusterConnection()
.withName(getClusterConnectionName() + "-db")
.withNamespace(withClusterConnectionNamespace)
.withDatabase(databaseName)
.returnFirst();

var item = given.one()
.schema()
.withClusterConnectionName(clusterConnectionDb.getMetadata().getName())
.withClusterConnectionNamespace(clusterConnectionDb.getMetadata().getNamespace())
.withClusterConnectionName(getClusterConnectionName())
.withClusterConnectionNamespace(withClusterConnectionNamespace)
.withDatabase(database)
.withReclaimPolicy(DELETE)
.returnFirst();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ protected Role create(int index) {
.build()
);

var spec = new RoleSpec();
spec.setName(name);
spec.setComment(withComment);

var clusterRef = new ClusterReference();
clusterRef.setName(getClusterConnectionName());
clusterRef.setNamespace(withClusterConnectionNamespace);
spec.setClusterRef(clusterRef);

var spec = new RoleSpec();

spec.setClusterRef(clusterRef);
spec.setName(name);
spec.setComment(withComment);
spec.setPasswordSecretRef(withPasswordSecretRef);

if (withFlags != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import java.util.concurrent.TimeUnit;

import static it.aboutbits.postgresql.core.ReclaimPolicy.DELETE;

@NullMarked
@Setter
@Accessors(fluent = true, chain = true)
Expand All @@ -38,6 +40,9 @@ public class SchemaCreate extends TestDataCreator<Schema> {
@Nullable
private String withClusterConnectionNamespace;

@Nullable
private String withDatabase;

private ReclaimPolicy withReclaimPolicy = ReclaimPolicy.RETAIN;

@Nullable
Expand Down Expand Up @@ -72,15 +77,20 @@ protected Schema create(int index) {
.build()
);

var spec = new SchemaSpec();
spec.setName(name);
spec.setReclaimPolicy(withReclaimPolicy);
spec.setOwner(withOwner);
// We have to create the database first which also modifies the specified withClusterConnectionName so the connection points to the newly created DB
var database = getDatabase();

var clusterRef = new ClusterReference();
clusterRef.setName(getClusterConnectionName());
clusterRef.setNamespace(withClusterConnectionNamespace);

var spec = new SchemaSpec();

spec.setClusterRef(clusterRef);
spec.setDatabase(database);
spec.setName(name);
spec.setReclaimPolicy(withReclaimPolicy);
spec.setOwner(withOwner);

item.setSpec(spec);

Expand Down Expand Up @@ -139,4 +149,21 @@ private String getClusterConnectionName() {

return clusterConnection.getMetadata().getName();
}

private String getDatabase() {
if (withDatabase != null) {
return withDatabase;
}

var item = given.one()
.database()
.withClusterConnectionName(getClusterConnectionName())
.withClusterConnectionNamespace(withClusterConnectionNamespace)
.withReclaimPolicy(DELETE)
.returnFirst();

withDatabase = item.getSpec().getName();

return withDatabase;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ void errorWhenUnsupportedMaintainTablePrivilege() {
var schema = given.one()
.schema()
.withClusterConnectionName(clusterConnectionDb.getMetadata().getName())
.withDatabase(database.getSpec().getName())
.withReclaimPolicy(DELETE)
.returnFirst();

Expand Down Expand Up @@ -497,6 +498,7 @@ void defaultPrivilegeOnTable(
var schema = given.one()
.schema()
.withClusterConnectionName(clusterConnectionDb.getMetadata().getName())
.withDatabase(database.getSpec().getName())
.withReclaimPolicy(DELETE)
.returnFirst();

Expand Down Expand Up @@ -623,6 +625,7 @@ void defaultPrivilegeOnSequence() {
var schema = given.one()
.schema()
.withClusterConnectionName(clusterConnectionDb.getMetadata().getName())
.withDatabase(database.getSpec().getName())
.withReclaimPolicy(DELETE)
.returnFirst();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ void errorWhenUnsupportedMaintainTablePrivilege() {
var schema = given.one()
.schema()
.withClusterConnectionName(clusterConnectionDb.getMetadata().getName())
.withDatabase(database.getSpec().getName())
.withReclaimPolicy(DELETE)
.returnFirst();

Expand Down Expand Up @@ -505,6 +506,7 @@ void grantOnSchema() {
var schema = given.one()
.schema()
.withClusterConnectionName(clusterConnectionDb.getMetadata().getName())
.withDatabase(database.getSpec().getName())
.withReclaimPolicy(DELETE)
.returnFirst();

Expand Down Expand Up @@ -627,6 +629,7 @@ void grantOnTable(
var schema = given.one()
.schema()
.withClusterConnectionName(clusterConnectionDb.getMetadata().getName())
.withDatabase(database.getSpec().getName())
.withReclaimPolicy(DELETE)
.returnFirst();

Expand Down Expand Up @@ -762,6 +765,7 @@ void grantOnAllTables(
var schema = given.one()
.schema()
.withClusterConnectionName(clusterConnectionDb.getMetadata().getName())
.withDatabase(database.getSpec().getName())
.withReclaimPolicy(DELETE)
.returnFirst();

Expand Down Expand Up @@ -945,6 +949,7 @@ void grantOnSequence() {
var schema = given.one()
.schema()
.withClusterConnectionName(clusterConnectionDb.getMetadata().getName())
.withDatabase(database.getSpec().getName())
.withReclaimPolicy(DELETE)
.returnFirst();

Expand Down Expand Up @@ -1078,6 +1083,7 @@ void grantOnAllSequences() {
var schema = given.one()
.schema()
.withClusterConnectionName(clusterConnectionDb.getMetadata().getName())
.withDatabase(database.getSpec().getName())
.withReclaimPolicy(DELETE)
.returnFirst();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,32 @@ void resetEnvironment() {
@DisplayName("When a Schema is created, it should be reconciled to READY")
void createSchema_andStatusReady() {
// given
var clusterConnection = given.one()
var clusterConnectionMain = given.one()
.clusterConnection()
.withName("test-connection-schema")
.returnFirst();

var database = given.one()
.database()
.withClusterConnectionName(clusterConnectionMain.getMetadata().getName())
.returnFirst();

// Creating a second ClusterConnection CR is only required for the tests
var clusterConnectionDb = given.one()
.clusterConnection()
.withName("test-connection-schema-db")
.withDatabase(database.getSpec().getName())
.returnFirst();

var now = OffsetDateTime.now(ZoneOffset.UTC);
var schemaName = "test-schema";

// when
var schema = given.one()
.schema()
.withClusterConnectionName(clusterConnectionDb.getMetadata().getName())
.withDatabase(database.getSpec().getName())
.withName(schemaName)
.withClusterConnectionName(clusterConnection.getMetadata().getName())
.withReclaimPolicy(DELETE)
.returnFirst();

Expand All @@ -67,7 +80,7 @@ void createSchema_andStatusReady() {
now
);

var dsl = postgreSQLContextFactory.getDSLContext(clusterConnection);
var dsl = postgreSQLContextFactory.getDSLContext(clusterConnectionDb);

assertThat(schemaService.schemaExists(dsl, schema.getSpec())).isTrue();
}
Expand Down