-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Unzip zip files in hibernate HBM2DDL_IMPORT_FILES setting #47338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alihmzyv
wants to merge
3
commits into
quarkusio:main
Choose a base branch
from
alihmzyv:feature/unzip-zip-hibernate-import-files
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
28 changes: 28 additions & 0 deletions
28
.../quarkus/hibernate/orm/sql_load_script/ImportMultipleSqlLoadScriptsAsZipFileTestCase.java
This file contains hidden or 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,28 @@ | ||
package io.quarkus.hibernate.orm.sql_load_script; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.hibernate.orm.MyEntity; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
|
||
public class ImportMultipleSqlLoadScriptsAsZipFileTestCase { | ||
@RegisterExtension | ||
static QuarkusUnitTest runner = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(MyEntity.class, SqlLoadScriptTestResource.class) | ||
.addAsResource("application-multiple-load-script-files-as-zip-file-test.properties", | ||
"application.properties") | ||
.addAsResource("multiple-load-script-files.zip")); | ||
|
||
@Test | ||
public void testMultipleLoadScriptFilesAsZipFile() { | ||
String name1 = "import-1.sql load script entity"; | ||
String name2 = "import-2.sql load script entity"; | ||
|
||
RestAssured.when().get("/orm-sql-load-script/1").then().body(Matchers.is(name1)); | ||
RestAssured.when().get("/orm-sql-load-script/2").then().body(Matchers.is(name2)); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...t/java/io/quarkus/hibernate/orm/sql_load_script/ImportSqlLoadScriptAsZipFileTestCase.java
This file contains hidden or 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.hibernate.orm.sql_load_script; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.hibernate.orm.MyEntity; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
|
||
public class ImportSqlLoadScriptAsZipFileTestCase { | ||
@RegisterExtension | ||
static QuarkusUnitTest runner = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(MyEntity.class, SqlLoadScriptTestResource.class) | ||
.addAsResource("application-load-script-as-zip-file-test.properties", "application.properties") | ||
.addAsResource("load-script-test.zip")); | ||
|
||
@Test | ||
public void testSqlLoadScriptAsZipFile() { | ||
String name = "other-load-script sql load script entity"; | ||
RestAssured.when().get("/orm-sql-load-script/3").then().body(Matchers.is(name)); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...quarkus/hibernate/orm/sql_load_script/ImportSqlLoadScriptsAsMultipleZipFilesTestCase.java
This file contains hidden or 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,29 @@ | ||
package io.quarkus.hibernate.orm.sql_load_script; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.hibernate.orm.MyEntity; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
|
||
public class ImportSqlLoadScriptsAsMultipleZipFilesTestCase { | ||
@RegisterExtension | ||
static QuarkusUnitTest runner = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(MyEntity.class, SqlLoadScriptTestResource.class) | ||
.addAsResource("application-load-scripts-as-multiple-zip-files-test.properties", | ||
"application.properties") | ||
.addAsResource("import-multiple-load-scripts-1.zip") | ||
.addAsResource("import-multiple-load-scripts-2.zip")); | ||
|
||
@Test | ||
public void testSqlLoadScriptsAsMultipleZipFiles() { | ||
String name1 = "import-1.sql load script entity"; | ||
String name2 = "import-2.sql load script entity"; | ||
|
||
RestAssured.when().get("/orm-sql-load-script/1").then().body(Matchers.is(name1)); | ||
RestAssured.when().get("/orm-sql-load-script/2").then().body(Matchers.is(name2)); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...arkus/hibernate/orm/sql_load_script/ImportSqlLoadScriptsAsZipFilesAndSqlFileTestCase.java
This file contains hidden or 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,32 @@ | ||
package io.quarkus.hibernate.orm.sql_load_script; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.hibernate.orm.MyEntity; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
|
||
public class ImportSqlLoadScriptsAsZipFilesAndSqlFileTestCase { | ||
@RegisterExtension | ||
static QuarkusUnitTest runner = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(MyEntity.class, SqlLoadScriptTestResource.class) | ||
.addAsResource("application-load-scripts-as-multiple-zip-files-and-sql-file-test.properties", | ||
"application.properties") | ||
.addAsResource("load-script-test.sql") | ||
.addAsResource("import-multiple-load-scripts-1.zip") | ||
.addAsResource("import-multiple-load-scripts-2.zip")); | ||
|
||
@Test | ||
public void testSqlLoadScriptsAsZipFilesAndSqlFile() { | ||
String name = "other-load-script sql load script entity"; | ||
String name1 = "import-1.sql load script entity"; | ||
String name2 = "import-2.sql load script entity"; | ||
|
||
RestAssured.when().get("/orm-sql-load-script/1").then().body(Matchers.is(name1)); | ||
RestAssured.when().get("/orm-sql-load-script/2").then().body(Matchers.is(name2)); | ||
RestAssured.when().get("/orm-sql-load-script/3").then().body(Matchers.is(name)); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...ate-orm/deployment/src/test/resources/application-load-script-as-zip-file-test.properties
This file contains hidden or 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,5 @@ | ||
quarkus.datasource.db-kind=h2 | ||
quarkus.datasource.jdbc.url=jdbc:h2:mem:test | ||
|
||
quarkus.hibernate-orm.database.generation=drop-and-create | ||
quarkus.hibernate-orm.sql-load-script=load-script-test.zip |
5 changes: 5 additions & 0 deletions
5
...est/resources/application-load-scripts-as-multiple-zip-files-and-sql-file-test.properties
This file contains hidden or 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,5 @@ | ||
quarkus.datasource.db-kind=h2 | ||
quarkus.datasource.jdbc.url=jdbc:h2:mem:test | ||
|
||
quarkus.hibernate-orm.database.generation=drop-and-create | ||
quarkus.hibernate-orm.sql-load-script=load-script-test.sql, import-multiple-load-scripts-1.zip, import-multiple-load-scripts-2.zip |
5 changes: 5 additions & 0 deletions
5
...loyment/src/test/resources/application-load-scripts-as-multiple-zip-files-test.properties
This file contains hidden or 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,5 @@ | ||
quarkus.datasource.db-kind=h2 | ||
quarkus.datasource.jdbc.url=jdbc:h2:mem:test | ||
|
||
quarkus.hibernate-orm.database.generation=drop-and-create | ||
quarkus.hibernate-orm.sql-load-script=import-multiple-load-scripts-1.zip, import-multiple-load-scripts-2.zip |
5 changes: 5 additions & 0 deletions
5
...ent/src/test/resources/application-multiple-load-script-files-as-zip-file-test.properties
This file contains hidden or 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,5 @@ | ||
quarkus.datasource.db-kind=h2 | ||
quarkus.datasource.jdbc.url=jdbc:h2:mem:test | ||
|
||
quarkus.hibernate-orm.database.generation=drop-and-create | ||
quarkus.hibernate-orm.sql-load-script=multiple-load-script-files.zip |
Binary file added
BIN
+293 Bytes
extensions/hibernate-orm/deployment/src/test/resources/import-multiple-load-scripts-1.zip
Binary file not shown.
Binary file added
BIN
+293 Bytes
extensions/hibernate-orm/deployment/src/test/resources/import-multiple-load-scripts-2.zip
Binary file not shown.
Binary file added
BIN
+267 Bytes
extensions/hibernate-orm/deployment/src/test/resources/load-script-test.zip
Binary file not shown.
Binary file added
BIN
+564 Bytes
extensions/hibernate-orm/deployment/src/test/resources/multiple-load-script-files.zip
Binary file not shown.
10 changes: 10 additions & 0 deletions
10
...sions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/Constants.java
This file contains hidden or 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,10 @@ | ||
package io.quarkus.hibernate.orm.runtime; | ||
|
||
public final class Constants { | ||
public static final String COMMA = ","; | ||
public static final String ZIP_FILE_EXTENSION = ".zip"; | ||
public static final String SQL_LOAD_SCRIPT_UNZIPPED_DIR_PREFIX = "import-sql-unzip-"; | ||
|
||
private Constants() { | ||
} | ||
} |
This file contains hidden or 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
53 changes: 53 additions & 0 deletions
53
...bernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/SchemaToolingUtil.java
This file contains hidden or 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,53 @@ | ||
package io.quarkus.hibernate.orm.runtime; | ||
|
||
import static io.quarkus.hibernate.orm.runtime.Constants.COMMA; | ||
import static io.quarkus.hibernate.orm.runtime.Constants.SQL_LOAD_SCRIPT_UNZIPPED_DIR_PREFIX; | ||
import static io.quarkus.hibernate.orm.runtime.Constants.ZIP_FILE_EXTENSION; | ||
|
||
import java.net.URL; | ||
import java.nio.file.DirectoryStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
import org.jboss.logging.Logger; | ||
|
||
import io.quarkus.fs.util.ZipUtils; | ||
|
||
public class SchemaToolingUtil { | ||
private static final Logger log = Logger.getLogger(SchemaToolingUtil.class); | ||
|
||
public static String unzipZipFilesAndReplaceZips(String commaSeparatedFileNames) { | ||
List<String> unzippedFilesNames = new LinkedList<>(); | ||
if (commaSeparatedFileNames != null) { | ||
String[] fileNames = commaSeparatedFileNames.split(COMMA); | ||
for (String fileName : fileNames) { | ||
if (fileName.endsWith(ZIP_FILE_EXTENSION)) { | ||
try { | ||
Path unzipDir = Files.createTempDirectory(SQL_LOAD_SCRIPT_UNZIPPED_DIR_PREFIX); | ||
URL resource = Thread.currentThread() | ||
.getContextClassLoader() | ||
.getResource(fileName); | ||
Path zipFile = Paths.get(resource.toURI()); | ||
ZipUtils.unzip(zipFile, unzipDir); | ||
try (DirectoryStream<Path> paths = Files.newDirectoryStream(unzipDir)) { | ||
for (Path path : paths) { | ||
unzippedFilesNames.add(path.toAbsolutePath().toString()); | ||
} | ||
} | ||
} catch (Exception e) { | ||
log.errorf("Error unzipping import file %s: %s", fileName, e.getMessage()); | ||
throw new IllegalStateException(e); | ||
} | ||
} else { | ||
unzippedFilesNames.add(fileName); | ||
} | ||
} | ||
return String.join(COMMA, unzippedFilesNames); | ||
} else { | ||
return null; | ||
} | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should also update the documentation of the property itself; see:
quarkus/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmConfigPersistenceUnit.java
Lines 48 to 85 in bc3fe92
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done