Skip to content

Commit 545907d

Browse files
authored
Merge pull request #47338 from alihmzyv/feature/unzip-zip-hibernate-import-files
Unzip zip files in hibernate HBM2DDL_IMPORT_FILES setting
2 parents 9b966fa + 6f16571 commit 545907d

20 files changed

+205
-6
lines changed

docs/src/main/asciidoc/hibernate-orm.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,7 @@ your entity changes or any change to your `import.sql` is immediately picked up
774774
====
775775
By default, in `dev` and `test` modes, Hibernate ORM, upon boot, will read and execute the SQL statements in the `/import.sql` file (if present).
776776
You can change the file name by changing the property `quarkus.hibernate-orm.sql-load-script` in `application.properties`.
777+
You can also provide a `.zip` file in the same way which should contain only the files containing the SQL statements to be executed.
777778
====
778779

779780
The second approach is to use `quarkus.hibernate-orm.schema-management.strategy=update`.

extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmConfigPersistenceUnit.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ public interface HibernateOrmConfigPersistenceUnit {
7474
*
7575
* [NOTE]
7676
* ====
77-
* Quarkus supports `.sql` file with SQL statements or comments spread over multiple lines.
77+
* Quarkus supports files with SQL statements or comments spread over multiple lines,
78+
* or `.zip` files containing those files.
7879
* Each SQL statement must be terminated by a semicolon.
7980
* ====
8081
*

extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/util/HibernateProcessorUtil.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,12 @@ public static void configureSqlLoadScript(String persistenceUnitName,
363363

364364
// only set the found import files if configured
365365
if (persistenceUnitConfig.sqlLoadScript().isPresent()) {
366-
descriptor.getProperties().setProperty(AvailableSettings.HBM2DDL_IMPORT_FILES, String.join(",", importFiles));
366+
descriptor.getProperties().setProperty(AvailableSettings.JAKARTA_HBM2DDL_LOAD_SCRIPT_SOURCE,
367+
String.join(",", importFiles));
367368
}
368369
} else {
369370
//Disable implicit loading of the default import script (import.sql)
370-
descriptor.getProperties().setProperty(AvailableSettings.HBM2DDL_IMPORT_FILES, "");
371+
descriptor.getProperties().setProperty(AvailableSettings.JAKARTA_HBM2DDL_LOAD_SCRIPT_SOURCE, "");
371372
descriptor.getProperties().setProperty(AvailableSettings.HBM2DDL_SKIP_DEFAULT_IMPORT_FILE, "true");
372373
}
373374
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.quarkus.hibernate.orm.sql_load_script;
2+
3+
import org.hamcrest.Matchers;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.api.extension.RegisterExtension;
6+
7+
import io.quarkus.hibernate.orm.MyEntity;
8+
import io.quarkus.test.QuarkusUnitTest;
9+
import io.restassured.RestAssured;
10+
11+
public class ImportMultipleSqlLoadScriptsAsZipFileTestCase {
12+
@RegisterExtension
13+
static QuarkusUnitTest runner = new QuarkusUnitTest()
14+
.withApplicationRoot((jar) -> jar
15+
.addClasses(MyEntity.class, SqlLoadScriptTestResource.class)
16+
.addAsResource("application-multiple-load-script-files-as-zip-file-test.properties",
17+
"application.properties")
18+
.addAsResource("multiple-load-script-files.zip"));
19+
20+
@Test
21+
public void testMultipleLoadScriptFilesAsZipFile() {
22+
String name1 = "import-1.sql load script entity";
23+
String name2 = "import-2.sql load script entity";
24+
25+
RestAssured.when().get("/orm-sql-load-script/1").then().body(Matchers.is(name1));
26+
RestAssured.when().get("/orm-sql-load-script/2").then().body(Matchers.is(name2));
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.quarkus.hibernate.orm.sql_load_script;
2+
3+
import org.hamcrest.Matchers;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.api.extension.RegisterExtension;
6+
7+
import io.quarkus.hibernate.orm.MyEntity;
8+
import io.quarkus.test.QuarkusUnitTest;
9+
import io.restassured.RestAssured;
10+
11+
public class ImportSqlLoadScriptAsZipFileTestCase {
12+
@RegisterExtension
13+
static QuarkusUnitTest runner = new QuarkusUnitTest()
14+
.withApplicationRoot((jar) -> jar
15+
.addClasses(MyEntity.class, SqlLoadScriptTestResource.class)
16+
.addAsResource("application-load-script-as-zip-file-test.properties", "application.properties")
17+
.addAsResource("load-script-test.zip"));
18+
19+
@Test
20+
public void testSqlLoadScriptAsZipFile() {
21+
String name = "other-load-script sql load script entity";
22+
RestAssured.when().get("/orm-sql-load-script/3").then().body(Matchers.is(name));
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.quarkus.hibernate.orm.sql_load_script;
2+
3+
import org.hamcrest.Matchers;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.api.extension.RegisterExtension;
6+
7+
import io.quarkus.hibernate.orm.MyEntity;
8+
import io.quarkus.test.QuarkusUnitTest;
9+
import io.restassured.RestAssured;
10+
11+
public class ImportSqlLoadScriptsAsMultipleZipFilesTestCase {
12+
@RegisterExtension
13+
static QuarkusUnitTest runner = new QuarkusUnitTest()
14+
.withApplicationRoot((jar) -> jar
15+
.addClasses(MyEntity.class, SqlLoadScriptTestResource.class)
16+
.addAsResource("application-load-scripts-as-multiple-zip-files-test.properties",
17+
"application.properties")
18+
.addAsResource("import-multiple-load-scripts-1.zip")
19+
.addAsResource("import-multiple-load-scripts-2.zip"));
20+
21+
@Test
22+
public void testSqlLoadScriptsAsMultipleZipFiles() {
23+
String name1 = "import-1.sql load script entity";
24+
String name2 = "import-2.sql load script entity";
25+
26+
RestAssured.when().get("/orm-sql-load-script/1").then().body(Matchers.is(name1));
27+
RestAssured.when().get("/orm-sql-load-script/2").then().body(Matchers.is(name2));
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.quarkus.hibernate.orm.sql_load_script;
2+
3+
import org.hamcrest.Matchers;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.api.extension.RegisterExtension;
6+
7+
import io.quarkus.hibernate.orm.MyEntity;
8+
import io.quarkus.test.QuarkusUnitTest;
9+
import io.restassured.RestAssured;
10+
11+
public class ImportSqlLoadScriptsAsZipFilesAndSqlFileTestCase {
12+
@RegisterExtension
13+
static QuarkusUnitTest runner = new QuarkusUnitTest()
14+
.withApplicationRoot((jar) -> jar
15+
.addClasses(MyEntity.class, SqlLoadScriptTestResource.class)
16+
.addAsResource("application-load-scripts-as-multiple-zip-files-and-sql-file-test.properties",
17+
"application.properties")
18+
.addAsResource("load-script-test.sql")
19+
.addAsResource("import-multiple-load-scripts-1.zip")
20+
.addAsResource("import-multiple-load-scripts-2.zip"));
21+
22+
@Test
23+
public void testSqlLoadScriptsAsZipFilesAndSqlFile() {
24+
String name = "other-load-script sql load script entity";
25+
String name1 = "import-1.sql load script entity";
26+
String name2 = "import-2.sql load script entity";
27+
28+
RestAssured.when().get("/orm-sql-load-script/1").then().body(Matchers.is(name1));
29+
RestAssured.when().get("/orm-sql-load-script/2").then().body(Matchers.is(name2));
30+
RestAssured.when().get("/orm-sql-load-script/3").then().body(Matchers.is(name));
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
quarkus.datasource.db-kind=h2
2+
quarkus.datasource.jdbc.url=jdbc:h2:mem:test
3+
4+
quarkus.hibernate-orm.database.generation=drop-and-create
5+
quarkus.hibernate-orm.sql-load-script=load-script-test.zip
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
quarkus.datasource.db-kind=h2
2+
quarkus.datasource.jdbc.url=jdbc:h2:mem:test
3+
4+
quarkus.hibernate-orm.database.generation=drop-and-create
5+
quarkus.hibernate-orm.sql-load-script=load-script-test.sql, import-multiple-load-scripts-1.zip, import-multiple-load-scripts-2.zip
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
quarkus.datasource.db-kind=h2
2+
quarkus.datasource.jdbc.url=jdbc:h2:mem:test
3+
4+
quarkus.hibernate-orm.database.generation=drop-and-create
5+
quarkus.hibernate-orm.sql-load-script=import-multiple-load-scripts-1.zip, import-multiple-load-scripts-2.zip

0 commit comments

Comments
 (0)