Skip to content

Commit

Permalink
fix(db): New config property to set the name of the db schema (#5675)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
EricWittmann authored Dec 6, 2024
1 parent f2664d7 commit e577740
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ public abstract class AbstractSqlRegistryStorage implements RegistryStorage {
private static int DB_VERSION = Integer
.valueOf(IoUtil.toString(AbstractSqlRegistryStorage.class.getResourceAsStream("db-version")))
.intValue();
private static final Object inmemorySequencesMutex = new Object();

private static final ObjectMapper mapper = new ObjectMapper();

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

@ConfigProperty(name = "apicurio.sql.db-schema", defaultValue = "*")
@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")
String dbSchema;

@Inject
@ConfigProperty(name = "apicurio.events.kafka.topic", defaultValue = "registry-events")
@Info(category = "storage", description = "Storage event topic")
Expand Down Expand Up @@ -327,8 +330,15 @@ protected void initialize(HandleFactory handleFactory, boolean emitStorageReadyE
*/
private boolean isDatabaseInitializedRaw(Handle handle) {
log.info("Checking to see if the DB is initialized.");
int count = handle.createQuery(this.sqlStatements.isDatabaseInitialized()).mapTo(Integer.class).one();
return count > 0;
if ("*".equals(dbSchema)) {
int count = handle.createQuery(this.sqlStatements.isDatabaseInitialized()).mapTo(Integer.class)
.one();
return count > 0;
} else {
int count = handle.createQuery(this.sqlStatements.isDatabaseSchemaInitialized()).bind(0, dbSchema)
.mapTo(Integer.class).one();
return count > 0;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ public abstract class CommonSqlStatements implements SqlStatements {
public CommonSqlStatements() {
}

/**
* @see SqlStatements#isDatabaseInitialized()
*/
@Override
public String isDatabaseInitialized() {
return "SELECT count(*) AS count FROM information_schema.tables WHERE table_name = 'apicurio'";
}

@Override
public String isDatabaseSchemaInitialized() {
return "SELECT count(*) AS count FROM information_schema.tables WHERE table_schema = ? AND table_name = 'apicurio'";
}

/**
* @see io.apicurio.registry.storage.impl.sql.SqlStatements#databaseInitialization()
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public String isDatabaseInitialized() {
return "SELECT COUNT(*) AS count FROM information_schema.tables WHERE table_name = 'APICURIO'";
}

@Override
public String isDatabaseSchemaInitialized() {
return "SELECT COUNT(*) AS count FROM information_schema.tables WHERE table_schema = ? AND table_name = 'APICURIO'";
}

/**
* @see io.apicurio.registry.storage.impl.sql.SqlStatements#getNextSequenceValue()
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@ public boolean isForeignKeyViolation(Exception error) {
return error.getMessage().contains("foreign key constraint fails");
}

/**
* @see io.apicurio.registry.storage.impl.sql.SqlStatements#isDatabaseInitialized()
*/
@Override
public String isDatabaseInitialized() {
return "SELECT count(*) AS count FROM information_schema.tables WHERE table_name = 'artifacts'";
}

/**
* @see io.apicurio.registry.storage.impl.sql.SqlStatements#getNextSequenceValue()
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@ public boolean isForeignKeyViolation(Exception error) {
return error.getMessage().contains("violates foreign key constraint");
}

/**
* @see io.apicurio.registry.storage.impl.sql.SqlStatements#isDatabaseInitialized()
*/
@Override
public String isDatabaseInitialized() {
return "SELECT count(*) AS count FROM information_schema.tables WHERE table_name = 'artifacts'";
}

/**
* @see io.apicurio.registry.storage.impl.sql.SqlStatements#getNextSequenceValue()
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,6 @@ public boolean isForeignKeyViolation(Exception error) {
return error.getMessage().contains("conflicted with the FOREIGN KEY constraint");
}

/**
* @see SqlStatements#isDatabaseInitialized()
*/
@Override
public String isDatabaseInitialized() {
return "SELECT count(*) AS count FROM information_schema.tables WHERE table_name = 'artifacts'";
}

@Override
public String upsertBranch() {
return """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public interface SqlStatements {
*/
public String isDatabaseInitialized();

/**
* A statement that returns 'true' if the database (with schema) has already been initialized.
*/
public String isDatabaseSchemaInitialized();

/**
* A sequence of statements needed to initialize the database.
*/
Expand Down
1 change: 1 addition & 0 deletions app/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ apicurio.import.work-dir=${java.io.tmpdir}
## SQL Storage
apicurio.storage.sql.kind=h2
apicurio.sql.init=true
apicurio.sql.db-schema=*

apicurio.datasource.url=jdbc:h2:mem:db_${quarkus.uuid}
apicurio.datasource.username=sa
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,11 @@ The following {registry} configuration options are available for each component
|`true`
|
|Kafka sql storage topic auto create
|`apicurio.sql.db-schema`
|`string`
|
|`3.0.6`
|Database schema name (only needed when running two instances of Registry against the same database, in multiple schemas)
|`apicurio.sql.init`
|`boolean`
|`true`
Expand Down

0 comments on commit e577740

Please sign in to comment.