HHH-15717 SQL script executed twice when using persistence.xml jakarta.persistence.sql-load-script-source property with the default sql script name

This commit is contained in:
Andrea Boriero 2022-11-15 16:25:07 +01:00 committed by Andrea Boriero
parent 9720037963
commit 476da28da9
7 changed files with 47 additions and 2 deletions

View File

@ -524,8 +524,13 @@ public class SchemaCreatorImpl implements SchemaCreator {
}
String charsetName = (String) options.getConfigurationValues().get( HBM2DDL_CHARSET_NAME );
boolean hasDefaultImportFileScriptBeenExecuted = false;
if ( importScriptSetting != null ) {
final ScriptSourceInput importScriptInput = interpretScriptSourceSetting( importScriptSetting, classLoaderService, charsetName );
final URL defaultImportFileUrl = classLoaderService.locateResource( DEFAULT_IMPORT_FILE );
if ( defaultImportFileUrl != null && importScriptInput.containsScript( defaultImportFileUrl ) ) {
hasDefaultImportFileScriptBeenExecuted = true;
}
final List<String> commands = importScriptInput.extract(
reader -> commandExtractor.extractCommands( reader, dialect )
);
@ -537,7 +542,7 @@ public class SchemaCreatorImpl implements SchemaCreator {
final String importFiles = ConfigurationHelper.getString(
AvailableSettings.HBM2DDL_IMPORT_FILES,
options.getConfigurationValues(),
DEFAULT_IMPORT_FILE
hasDefaultImportFileScriptBeenExecuted ? "" : DEFAULT_IMPORT_FILE
);
for ( String currentFile : importFiles.split( "," ) ) {

View File

@ -7,6 +7,7 @@
package org.hibernate.tool.schema.internal.exec;
import java.io.Reader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -62,6 +63,16 @@ public class ScriptSourceInputAggregate implements ScriptSourceInput {
return list;
}
@Override
public boolean containsScript(URL url) {
for ( int i = 0; i < inputs.length; i++ ) {
if ( inputs[i].containsScript( url ) ) {
return true;
}
}
return false;
}
@Override
public String toString() {
return "ScriptSourceInputAggregate(" + Arrays.toString( inputs ) + ")";

View File

@ -11,6 +11,7 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import org.hibernate.tool.schema.spi.SchemaManagementException;
@ -86,6 +87,11 @@ public class ScriptSourceInputFromFile extends AbstractScriptSourceInput {
}
}
@Override
public boolean containsScript(URL url) {
return file.getAbsolutePath().equals( url.getPath() );
}
@Override
public String toString() {
return "ScriptSourceInputFromFile(" + file.getAbsolutePath() + ")";

View File

@ -7,6 +7,7 @@
package org.hibernate.tool.schema.internal.exec;
import java.io.Reader;
import java.net.URL;
/**
* ScriptSourceInput implementation for explicitly given Readers.
@ -40,6 +41,11 @@ public class ScriptSourceInputFromReader extends AbstractScriptSourceInput {
// nothing to do
}
@Override
public boolean containsScript(URL url) {
return false;
}
@Override
public String toString() {
return "ScriptSourceInputFromReader()";

View File

@ -67,6 +67,11 @@ public class ScriptSourceInputFromUrl extends AbstractScriptSourceInput {
}
}
@Override
public boolean containsScript(URL url) {
return url.equals( url );
}
@Override
public String toString() {
return "ScriptSourceInputFromUrl(" + url.toExternalForm() + ")";

View File

@ -7,6 +7,7 @@
package org.hibernate.tool.schema.internal.exec;
import java.io.Reader;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
@ -39,6 +40,11 @@ public class ScriptSourceInputNonExistentImpl extends AbstractScriptSourceInput
}
@Override
public boolean containsScript(URL url) {
return false;
}
@Override
public List<String> extract(Function<Reader, List<String>> extractor) {
return Collections.emptyList();

View File

@ -7,6 +7,7 @@
package org.hibernate.tool.schema.spi;
import java.io.Reader;
import java.net.URL;
import java.util.List;
import java.util.function.Function;
@ -21,4 +22,9 @@ public interface ScriptSourceInput {
* Allows managed access to the input's Reader, returning a result
*/
List<String> extract(Function<Reader, List<String>> extractor);
default boolean containsScript(URL url) {
return false;
}
}