diff --git a/docker-compose.yml b/docker-compose.yml index 6d0c70b0..6c7ed639 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 diff --git a/docs/docs/elide/configuration.md b/docs/docs/elide/configuration.md index 4c6e0fc3..fa811af4 100644 --- a/docs/docs/elide/configuration.md +++ b/docs/docs/elide/configuration.md @@ -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 ----- @@ -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 diff --git a/src/main/java/com/qubitpi/ws/jersey/template/application/BinderFactory.java b/src/main/java/com/qubitpi/ws/jersey/template/application/BinderFactory.java index 616ddd56..e6aed860 100644 --- a/src/main/java/com/qubitpi/ws/jersey/template/application/BinderFactory.java +++ b/src/main/java/com/qubitpi/ws/jersey/template/application/BinderFactory.java @@ -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"); diff --git a/src/main/java/com/qubitpi/ws/jersey/template/config/JpaDatastoreConfig.java b/src/main/java/com/qubitpi/ws/jersey/template/config/JpaDatastoreConfig.java index d9c1a0f3..4a19311a 100644 --- a/src/main/java/com/qubitpi/ws/jersey/template/config/JpaDatastoreConfig.java +++ b/src/main/java/com/qubitpi/ws/jersey/template/config/JpaDatastoreConfig.java @@ -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. + *

+ * 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 + *
  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. + *
+ * 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(); } diff --git a/src/test/groovy/com/qubitpi/ws/jersey/template/application/AbstractITSpec.groovy b/src/test/groovy/com/qubitpi/ws/jersey/template/application/AbstractITSpec.groovy index f7c86e4d..3b402d95 100644 --- a/src/test/groovy/com/qubitpi/ws/jersey/template/application/AbstractITSpec.groovy +++ b/src/test/groovy/com/qubitpi/ws/jersey/template/application/AbstractITSpec.groovy @@ -62,6 +62,8 @@ abstract class AbstractITSpec extends Specification { RestAssured.port = WS_PORT RestAssured.basePath = "/v1/data/" + System.setProperty("HIBERNATE_HBM2DDL_AUTO", "create") + childSetupSpec() } @@ -69,6 +71,8 @@ abstract class AbstractITSpec extends Specification { RestAssured.reset() childCleanupSpec() + + System.clearProperty("HIBERNATE_HBM2DDL_AUTO") } def "JSON API allows for POSTing, GETing, PATCHing, and DELETing a book"() {