Skip to content

Commit

Permalink
Make hibernate.hbm2ddl.auto configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
QubitPi committed Jan 21, 2024
1 parent 6a2bc29 commit 80e46b3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ services:
- DB_URL=jdbc:mysql://db/elide?serverTimezone=UTC
- OAUTH_ENABLED=${OAUTH_ENABLED:-false}
- JWKS_URL=${JWKS_URL}
- HIBERNATE_HBM2DDL_AUTO=create
depends_on:
db:
condition: service_healthy
Expand Down
16 changes: 16 additions & 0 deletions docs/docs/elide/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ The following configurations can be placed in the properties file called **jpada
- **DB_URL**: The persistence DB URL, such as "jdbc:mysql://localhost/elide?serverTimezone=UTC".
- **DB_DRIVER**: The SQL DB driver class name, such as "com.mysql.jdbc.Driver".
- **DB_DIALECT**: The SQL DB dialect name, such as "org.hibernate.dialect.MySQLDialect".
- **HIBERNATE_HBM2DDL_AUTO**: What to do with existing JPA database when webservice starts; can be one of the 4 values:

1. _validate_: validate that the schema matches, make no changes to the schema of the database. _This is the default
value of **HIBERNATE_HBM2DDL_AUTO**_
2. _update_: update the schema to reflect the entities being persisted
3. _create_: creates the schema necessary for your entities, destroying any previous data.
4. _create-drop_: create the schema as in create above, but also drop the schema at the end of the session. This is
great in development or for testing.

:::note

This property is exactly the same as [Hibernate `hibernate.hbm2ddl.auto` property].

:::

CI/CD
-----
Expand Down Expand Up @@ -115,6 +129,8 @@ Note that how `my-webservice-repo-changes` is used to bridge the event dispatche

[GitHub Action - How to set up]: https://docs.github.com/en/actions/security-guides/encrypted-secrets

[Hibernate `hibernate.hbm2ddl.auto` property]: https://stackoverflow.com/questions/18077327/hibernate-hbm2ddl-auto-possible-values-and-what-they-do

[Java system properties]: https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html
[jersey-webservice-template-jpa-data-models]: https://github.com/QubitPi/jersey-webservice-template-jpa-data-models
[jersey-webservice-template-jpa-data-models-acceptance-tests]: https://github.com/QubitPi/jersey-webservice-template-jpa-data-models-acceptance-tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private static Properties getDefaultDbConfigs() {
final Properties dbProperties = new Properties();

dbProperties.put("hibernate.show_sql", "true");
dbProperties.put("hibernate.hbm2ddl.auto", "update");
dbProperties.put("hibernate.hbm2ddl.auto", JPA_DATASTORE_CONFIG.hibernateMbm2ddlAuto());
dbProperties.put("hibernate.dialect", JPA_DATASTORE_CONFIG.dbDialect());
dbProperties.put("hibernate.current_session_context_class", "thread");
dbProperties.put("hibernate.jdbc.use_scrollable_resultset", "true");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,27 @@ public interface JpaDatastoreConfig extends Config {
@NotNull
@Key("DB_DIALECT")
String dbDialect();


/**
* What to do with existing JPA database when webservice starts.
* <p>
* Can be one of the 4 values:
* <ol>
* <li> validate - validate that the schema matches, make no changes to the schema of the database. This is the
* default value
* <li> update - update the schema to reflect the entities being persisted
* <li> create - creates the schema necessary for your entities, destroying any previous data.
* <li> create-drop - create the schema as in create above, but also drop the schema at the end of the session.
* This is great in development or for testing.
* </ol>
* See https://stackoverflow.com/questions/18077327/hibernate-hbm2ddl-auto-possible-values-and-what-they-do for more
* details.
*
* @return a DB config string
*/
@NotNull
@DefaultValue("validate")
@Key("HIBERNATE_HBM2DDL_AUTO")
String hibernateMbm2ddlAuto();
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,17 @@ abstract class AbstractITSpec extends Specification {
RestAssured.port = WS_PORT
RestAssured.basePath = "/v1/data/"

System.setProperty("HIBERNATE_HBM2DDL_AUTO", "create")

childSetupSpec()
}

def cleanupSpec() {
RestAssured.reset()

childCleanupSpec()

System.clearProperty("HIBERNATE_HBM2DDL_AUTO")
}

def "JSON API allows for POSTing, GETing, PATCHing, and DELETing a book"() {
Expand Down

0 comments on commit 80e46b3

Please sign in to comment.