1616
1717import 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}
0 commit comments