Skip to content

Commit

Permalink
Fix missing null check for JDBC init script (#9118)
Browse files Browse the repository at this point in the history
In 6a07650, a new feature was introduced to support multiple init scripts in JdbcDatabaseContainer. However, during this update, a critical null check was inadvertently removed from the `JdbcDatabaseContainer.runInitScriptIfRequired` method. This check was responsible for ignoring null values for the `initScript` parameter.

As a result, when no init script is defined, a null value is passed to `ScriptUtils.runInitScript`, leading to an exception being thrown and preventing the database container from starting.

This commit reinstates the missing null check, ensuring that undefined init scripts are properly handled. Additionally, a test has been added to verify that the issue is resolved and the database container can start without an init script.

---------

Co-authored-by: Eddú Meléndez <[email protected]>
  • Loading branch information
Reza Morshedi and eddumelendez authored Aug 20, 2024
1 parent cb7a793 commit 251bca5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -361,7 +362,10 @@ protected void optionallyMapResourceParameterAsVolume(
* Load init script content and apply it to the database if initScriptPath is set
*/
protected void runInitScriptIfRequired() {
initScriptPaths.forEach(path -> ScriptUtils.runInitScript(getDatabaseDelegate(), path));
initScriptPaths
.stream()
.filter(Objects::nonNull)
.forEach(path -> ScriptUtils.runInitScript(getDatabaseDelegate(), path));
}

public void setParameters(Map<String, String> parameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.logging.LogManager;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;

public class SimplePostgreSQLTest extends AbstractContainerDatabaseTest {
static {
Expand Down Expand Up @@ -59,6 +60,16 @@ public void testUnsetCommand() throws SQLException {
}
}

@Test
public void testMissingInitScript() {
try (
PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(PostgreSQLTestImages.POSTGRES_TEST_IMAGE)
.withInitScript(null)
) {
assertThatNoException().isThrownBy(postgres::start);
}
}

@Test
public void testExplicitInitScript() throws SQLException {
try (
Expand Down

0 comments on commit 251bca5

Please sign in to comment.