From dd9fcb3f5c73f65539051c51d3373aabdbce37aa Mon Sep 17 00:00:00 2001 From: Stuart Douglas Date: Wed, 24 Jan 2024 09:45:26 +1100 Subject: [PATCH] Hibernate DDL created twice In dev mode the hibernate DDL is eagerly created just in case is might be needed in the dev UI. This fixes that, and also adds the ability to view the update script which can be really useful to create flyway migrations. Fixes #38357 (cherry picked from commit 992d65ebac942787e2967b69db2d9cd39786ff56) --- .../dev-ui/hibernate-orm-persistence-units.js | 17 ++++++++- .../dev/HibernateOrmDevController.java | 36 ++++++++++++++++--- .../orm/runtime/dev/HibernateOrmDevInfo.java | 34 ++++++++++++++---- 3 files changed, 74 insertions(+), 13 deletions(-) diff --git a/extensions/hibernate-orm/deployment/src/main/resources/dev-ui/hibernate-orm-persistence-units.js b/extensions/hibernate-orm/deployment/src/main/resources/dev-ui/hibernate-orm-persistence-units.js index 63781ff8220da..f28762193739a 100644 --- a/extensions/hibernate-orm/deployment/src/main/resources/dev-ui/hibernate-orm-persistence-units.js +++ b/extensions/hibernate-orm/deployment/src/main/resources/dev-ui/hibernate-orm-persistence-units.js @@ -83,6 +83,21 @@ export class HibernateOrmPersistenceUnitsComponent extends LitElement {
${pu.createDDL}
+ + + + Update Script + + + Copy + + + +
${pu.updateDDL}
+
{ + + private final Action action; + private final Metadata metadata; + private final ServiceRegistry serviceRegistry; + private final String importFile; + + DDLSupplier(Action action, Metadata metadata, ServiceRegistry serviceRegistry, String importFile) { + this.action = action; + this.metadata = metadata; + this.serviceRegistry = serviceRegistry; + this.importFile = importFile; + } + + @Override + public String get() { + return generateDDL(action, metadata, serviceRegistry, importFile); + } } void clearData() { @@ -131,8 +154,11 @@ public void accept(String command) { SchemaDropper schemaDropper = tool.getSchemaDropper(executionOptions.getConfigurationValues()); schemaDropper.doDrop(metadata, executionOptions, ContributableMatcher.ALL, source, target); } else if (action == Action.CREATE) { - SchemaCreator schemaDropper = tool.getSchemaCreator(executionOptions.getConfigurationValues()); - schemaDropper.doCreation(metadata, executionOptions, ContributableMatcher.ALL, source, target); + SchemaCreator schemaCreator = tool.getSchemaCreator(executionOptions.getConfigurationValues()); + schemaCreator.doCreation(metadata, executionOptions, ContributableMatcher.ALL, source, target); + } else if (action == Action.UPDATE) { + SchemaMigrator schemaMigrator = tool.getSchemaMigrator(executionOptions.getConfigurationValues()); + schemaMigrator.doMigration(metadata, executionOptions, ContributableMatcher.ALL, target); } return writer.toString(); } catch (RuntimeException e) { diff --git a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/dev/HibernateOrmDevInfo.java b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/dev/HibernateOrmDevInfo.java index e51e25916b76d..02a05e4f67d1c 100644 --- a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/dev/HibernateOrmDevInfo.java +++ b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/dev/HibernateOrmDevInfo.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.Map; import java.util.TreeMap; +import java.util.function.Supplier; import org.hibernate.LockOptions; import org.hibernate.boot.query.NamedHqlQueryDefinition; @@ -44,18 +45,24 @@ public static class PersistenceUnit { private final List managedEntities; private final List namedQueries; private final List namedNativeQueries; - private final String createDDL; - private final String dropDDL; + private String createDDL; + private String dropDDL; + private String updateDDL; + private final Supplier createDDLSupplier; + private final Supplier dropDDLSupplier; + private final Supplier updateDDLSupplier; public PersistenceUnit(String name, List managedEntities, List namedQueries, - List namedNativeQueries, String createDDL, String dropDDL) { + List namedNativeQueries, Supplier createDDL, Supplier dropDDL, + Supplier updateDDLSupplier) { this.name = name; this.managedEntities = managedEntities; this.namedQueries = namedQueries; this.namedNativeQueries = namedNativeQueries; - this.createDDL = createDDL; - this.dropDDL = dropDDL; + this.createDDLSupplier = createDDL; + this.dropDDLSupplier = dropDDL; + this.updateDDLSupplier = updateDDLSupplier; } public String getName() { @@ -81,14 +88,27 @@ public List getAllNamedQueries() { return allQueries; } - public String getCreateDDL() { + public synchronized String getCreateDDL() { + if (createDDL == null) { + createDDL = createDDLSupplier.get(); + } return createDDL; } - public String getDropDDL() { + public synchronized String getDropDDL() { + if (dropDDL == null) { + dropDDL = dropDDLSupplier.get(); + } return dropDDL; } + public synchronized String getUpdateDDL() { + if (updateDDL == null) { + updateDDL = updateDDLSupplier.get(); + } + return updateDDL; + } + } public static class Entity {