Skip to content

Commit e577740

Browse files
authored
fix(db): New config property to set the name of the db schema (#5675)
* New config property to set the name of the schema (used for db init only). * Add new property to config file * Fix a copy/paste error in the new config property * Removed unneeded property from application.properties * Try to fix the config property
1 parent f2664d7 commit e577740

File tree

9 files changed

+42
-27
lines changed

9 files changed

+42
-27
lines changed

app/src/main/java/io/apicurio/registry/storage/impl/sql/AbstractSqlRegistryStorage.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ public abstract class AbstractSqlRegistryStorage implements RegistryStorage {
177177
private static int DB_VERSION = Integer
178178
.valueOf(IoUtil.toString(AbstractSqlRegistryStorage.class.getResourceAsStream("db-version")))
179179
.intValue();
180-
private static final Object inmemorySequencesMutex = new Object();
181180

182181
private static final ObjectMapper mapper = new ObjectMapper();
183182

@@ -234,6 +233,10 @@ protected SqlStatements sqlStatements() {
234233
@Info(category = "storage", description = "SQL init", availableSince = "2.0.0.Final")
235234
boolean initDB;
236235

236+
@ConfigProperty(name = "apicurio.sql.db-schema", defaultValue = "*")
237+
@Info(category = "storage", description = "Database schema name (only needed when running two instances of Registry against the same database, in multiple schemas)", availableSince = "3.0.6")
238+
String dbSchema;
239+
237240
@Inject
238241
@ConfigProperty(name = "apicurio.events.kafka.topic", defaultValue = "registry-events")
239242
@Info(category = "storage", description = "Storage event topic")
@@ -327,8 +330,15 @@ protected void initialize(HandleFactory handleFactory, boolean emitStorageReadyE
327330
*/
328331
private boolean isDatabaseInitializedRaw(Handle handle) {
329332
log.info("Checking to see if the DB is initialized.");
330-
int count = handle.createQuery(this.sqlStatements.isDatabaseInitialized()).mapTo(Integer.class).one();
331-
return count > 0;
333+
if ("*".equals(dbSchema)) {
334+
int count = handle.createQuery(this.sqlStatements.isDatabaseInitialized()).mapTo(Integer.class)
335+
.one();
336+
return count > 0;
337+
} else {
338+
int count = handle.createQuery(this.sqlStatements.isDatabaseSchemaInitialized()).bind(0, dbSchema)
339+
.mapTo(Integer.class).one();
340+
return count > 0;
341+
}
332342
}
333343

334344
/**

app/src/main/java/io/apicurio/registry/storage/impl/sql/CommonSqlStatements.java

+13
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ public abstract class CommonSqlStatements implements SqlStatements {
1616
public CommonSqlStatements() {
1717
}
1818

19+
/**
20+
* @see SqlStatements#isDatabaseInitialized()
21+
*/
22+
@Override
23+
public String isDatabaseInitialized() {
24+
return "SELECT count(*) AS count FROM information_schema.tables WHERE table_name = 'apicurio'";
25+
}
26+
27+
@Override
28+
public String isDatabaseSchemaInitialized() {
29+
return "SELECT count(*) AS count FROM information_schema.tables WHERE table_schema = ? AND table_name = 'apicurio'";
30+
}
31+
1932
/**
2033
* @see io.apicurio.registry.storage.impl.sql.SqlStatements#databaseInitialization()
2134
*/

app/src/main/java/io/apicurio/registry/storage/impl/sql/H2SqlStatements.java

+5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public String isDatabaseInitialized() {
4545
return "SELECT COUNT(*) AS count FROM information_schema.tables WHERE table_name = 'APICURIO'";
4646
}
4747

48+
@Override
49+
public String isDatabaseSchemaInitialized() {
50+
return "SELECT COUNT(*) AS count FROM information_schema.tables WHERE table_schema = ? AND table_name = 'APICURIO'";
51+
}
52+
4853
/**
4954
* @see io.apicurio.registry.storage.impl.sql.SqlStatements#getNextSequenceValue()
5055
*/

app/src/main/java/io/apicurio/registry/storage/impl/sql/MySQLSqlStatements.java

-8
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,6 @@ public boolean isForeignKeyViolation(Exception error) {
3636
return error.getMessage().contains("foreign key constraint fails");
3737
}
3838

39-
/**
40-
* @see io.apicurio.registry.storage.impl.sql.SqlStatements#isDatabaseInitialized()
41-
*/
42-
@Override
43-
public String isDatabaseInitialized() {
44-
return "SELECT count(*) AS count FROM information_schema.tables WHERE table_name = 'artifacts'";
45-
}
46-
4739
/**
4840
* @see io.apicurio.registry.storage.impl.sql.SqlStatements#getNextSequenceValue()
4941
*/

app/src/main/java/io/apicurio/registry/storage/impl/sql/PostgreSQLSqlStatements.java

-8
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,6 @@ public boolean isForeignKeyViolation(Exception error) {
3636
return error.getMessage().contains("violates foreign key constraint");
3737
}
3838

39-
/**
40-
* @see io.apicurio.registry.storage.impl.sql.SqlStatements#isDatabaseInitialized()
41-
*/
42-
@Override
43-
public String isDatabaseInitialized() {
44-
return "SELECT count(*) AS count FROM information_schema.tables WHERE table_name = 'artifacts'";
45-
}
46-
4739
/**
4840
* @see io.apicurio.registry.storage.impl.sql.SqlStatements#getNextSequenceValue()
4941
*/

app/src/main/java/io/apicurio/registry/storage/impl/sql/SQLServerSqlStatements.java

-8
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,6 @@ public boolean isForeignKeyViolation(Exception error) {
3737
return error.getMessage().contains("conflicted with the FOREIGN KEY constraint");
3838
}
3939

40-
/**
41-
* @see SqlStatements#isDatabaseInitialized()
42-
*/
43-
@Override
44-
public String isDatabaseInitialized() {
45-
return "SELECT count(*) AS count FROM information_schema.tables WHERE table_name = 'artifacts'";
46-
}
47-
4840
@Override
4941
public String upsertBranch() {
5042
return """

app/src/main/java/io/apicurio/registry/storage/impl/sql/SqlStatements.java

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public interface SqlStatements {
2828
*/
2929
public String isDatabaseInitialized();
3030

31+
/**
32+
* A statement that returns 'true' if the database (with schema) has already been initialized.
33+
*/
34+
public String isDatabaseSchemaInitialized();
35+
3136
/**
3237
* A sequence of statements needed to initialize the database.
3338
*/

app/src/main/resources/application.properties

+1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ apicurio.import.work-dir=${java.io.tmpdir}
163163
## SQL Storage
164164
apicurio.storage.sql.kind=h2
165165
apicurio.sql.init=true
166+
apicurio.sql.db-schema=*
166167

167168
apicurio.datasource.url=jdbc:h2:mem:db_${quarkus.uuid}
168169
apicurio.datasource.username=sa

docs/modules/ROOT/partials/getting-started/ref-registry-all-configs.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,11 @@ The following {registry} configuration options are available for each component
815815
|`true`
816816
|
817817
|Kafka sql storage topic auto create
818+
|`apicurio.sql.db-schema`
819+
|`string`
820+
|
821+
|`3.0.6`
822+
|Database schema name (only needed when running two instances of Registry against the same database, in multiple schemas)
818823
|`apicurio.sql.init`
819824
|`boolean`
820825
|`true`

0 commit comments

Comments
 (0)