-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generate flyway migrations from hibernate update scripts
- Loading branch information
Showing
8 changed files
with
219 additions
and
5 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
...sions/agroal/spi/src/main/java/io/quarkus/agroal/spi/JdbcUpdateSQLGeneratorBuildItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.quarkus.agroal.spi; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import io.quarkus.builder.item.MultiBuildItem; | ||
|
||
public final class JdbcUpdateSQLGeneratorBuildItem extends MultiBuildItem { | ||
|
||
final String databaseName; | ||
final Supplier<String> sqlSupplier; | ||
|
||
public JdbcUpdateSQLGeneratorBuildItem(String databaseName, Supplier<String> sqlSupplier) { | ||
this.databaseName = databaseName; | ||
this.sqlSupplier = sqlSupplier; | ||
} | ||
|
||
public String getDatabaseName() { | ||
return databaseName; | ||
} | ||
|
||
public Supplier<String> getSqlSupplier() { | ||
return sqlSupplier; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...deployment/src/test/java/io/quarkus/flyway/test/FlywayDevModeUpdateFromHibernateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package io.quarkus.flyway.test; | ||
|
||
import java.io.File; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Map; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
|
||
import io.quarkus.dev.console.DevConsoleManager; | ||
import io.quarkus.devui.tests.DevUIJsonRPCTest; | ||
import io.quarkus.test.QuarkusDevModeTest; | ||
|
||
public class FlywayDevModeUpdateFromHibernateTest extends DevUIJsonRPCTest { | ||
|
||
public FlywayDevModeUpdateFromHibernateTest() { | ||
super("io.quarkus.quarkus-flyway"); | ||
} | ||
|
||
@RegisterExtension | ||
static final QuarkusDevModeTest config = new QuarkusDevModeTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(FlywayDevModeUpdateFromHibernateTest.class, Fruit.class) | ||
.addAsResource(new StringAsset( | ||
" create sequence Fruit_SEQ start with 1 increment by 50;\n" + | ||
"\n" + | ||
" create table Fruit (\n" + | ||
" id integer not null,\n" + | ||
" primary key (id)\n" + | ||
" );"), | ||
"db/update/V2.0.0__Quarkus.sql") | ||
.addAsResource(new StringAsset( | ||
"quarkus.flyway.migrate-at-start=true\nquarkus.flyway.locations=db/update"), | ||
"application.properties")); | ||
|
||
@Test | ||
public void testGenerateMigrationFromHibernate() throws Exception { | ||
|
||
Map<String, Object> params = Map.of("ds", "<default>"); | ||
JsonNode devuiresponse = super.executeJsonRPCMethod("update", params); | ||
|
||
Assertions.assertNotNull(devuiresponse); | ||
String type = devuiresponse.get("type").asText(); | ||
Assertions.assertNotNull(type); | ||
Assertions.assertEquals("success", type); | ||
|
||
File migrationsDir = DevConsoleManager.getHotReplacementContext().getResourcesDir().get(0).resolve("db/update").toFile(); | ||
File[] newMigrations = migrationsDir.listFiles((dir, name) -> !name.equals("V2.0.0__Quarkus.sql")); | ||
Assertions.assertNotNull(newMigrations); | ||
Assertions.assertEquals(1, newMigrations.length); | ||
Assertions.assertTrue(newMigrations[0].getName().startsWith("V2.")); | ||
|
||
String content = Files.readString(newMigrations[0].toPath()); | ||
|
||
Assertions.assertEquals("\n" + | ||
" alter table if exists Fruit \n" + | ||
" add column name varchar(40);\n" + | ||
"\n" + | ||
" alter table if exists Fruit \n" + | ||
" drop constraint if exists UKqn1mp5t3oovyl0h02glapi2iv;\n" + | ||
"\n" + | ||
" alter table if exists Fruit \n" + | ||
" add constraint UKqn1mp5t3oovyl0h02glapi2iv unique (name);\n", content); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
.../main/java/io/quarkus/hibernate/orm/runtime/dev/HibernateOrmDevInfoUpdateDDLSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.quarkus.hibernate.orm.runtime.dev; | ||
|
||
import java.util.Collection; | ||
import java.util.Objects; | ||
import java.util.function.Supplier; | ||
|
||
import io.quarkus.runtime.annotations.RecordableConstructor; | ||
|
||
public class HibernateOrmDevInfoUpdateDDLSupplier implements Supplier<String> { | ||
|
||
private final String puName; | ||
|
||
@RecordableConstructor | ||
public HibernateOrmDevInfoUpdateDDLSupplier(String puName) { | ||
this.puName = puName; | ||
} | ||
|
||
@Override | ||
public String get() { | ||
Collection<HibernateOrmDevInfo.PersistenceUnit> persistenceUnits = HibernateOrmDevController.get() | ||
.getInfo().getPersistenceUnits(); | ||
for (var p : persistenceUnits) { | ||
if (Objects.equals(puName, p.getName())) { | ||
return p.getUpdateDDL(); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public String getPuName() { | ||
return puName; | ||
} | ||
} |