Skip to content

Commit

Permalink
Merge pull request #45508 from giamma-frangipane/main
Browse files Browse the repository at this point in the history
Added support for multiple init scripts
  • Loading branch information
yrodiere authored Jan 14, 2025
2 parents 234fdd8 + 7fe0c35 commit 90dd16b
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.datasource.deployment.spi;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
Expand All @@ -15,7 +16,7 @@ public class DevServicesDatasourceContainerConfig {
private final Optional<String> dbName;
private final Optional<String> username;
private final Optional<String> password;
private final Optional<String> initScriptPath;
private final Optional<List<String>> initScriptPath;
private final Map<String, String> volumes;
private final boolean reuse;
private final boolean showLogs;
Expand All @@ -29,7 +30,7 @@ public DevServicesDatasourceContainerConfig(Optional<String> imageName,
Optional<String> dbName,
Optional<String> username,
Optional<String> password,
Optional<String> initScriptPath,
Optional<List<String>> initScriptPath,
Map<String, String> volumes,
boolean reuse,
boolean showLogs) {
Expand Down Expand Up @@ -84,7 +85,7 @@ public Optional<String> getPassword() {
return password;
}

public Optional<String> getInitScriptPath() {
public Optional<List<String>> getInitScriptPath() {
return initScriptPath;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.datasource.runtime;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
Expand Down Expand Up @@ -83,11 +84,11 @@ public interface DevServicesBuildTimeConfig {
Optional<String> password();

/**
* The path to a SQL script to be loaded from the classpath and applied to the Dev Service database.
* The paths to SQL scripts to be loaded from the classpath and applied to the Dev Service database.
* <p>
* This has no effect if the provider is not a container-based database, such as H2 or Derby.
*/
Optional<String> initScriptPath();
Optional<List<@WithConverter(TrimmedStringConverter.class) String>> initScriptPath();

/**
* The volumes to be mapped to the container.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public RunningDevServicesDatasource startDatabase(Optional<String> username, Opt

containerConfig.getAdditionalJdbcUrlProperties().forEach(container::withUrlParam);
containerConfig.getCommand().ifPresent(container::setCommand);
containerConfig.getInitScriptPath().ifPresent(container::withInitScript);
containerConfig.getInitScriptPath().ifPresent(container::withInitScripts);
if (containerConfig.isShowLogs()) {
container.withLogConsumer(new JBossLoggingConsumer(LOG));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public RunningDevServicesDatasource startDatabase(Optional<String> username, Opt

containerConfig.getAdditionalJdbcUrlProperties().forEach(container::withUrlParam);
containerConfig.getCommand().ifPresent(container::setCommand);
containerConfig.getInitScriptPath().ifPresent(container::withInitScript);
containerConfig.getInitScriptPath().ifPresent(container::withInitScripts);
if (containerConfig.isShowLogs()) {
container.withLogConsumer(new JBossLoggingConsumer(LOG));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public RunningDevServicesDatasource startDatabase(Optional<String> username, Opt

containerConfig.getAdditionalJdbcUrlProperties().forEach(container::withUrlParam);
containerConfig.getCommand().ifPresent(container::setCommand);
containerConfig.getInitScriptPath().ifPresent(container::withInitScript);
containerConfig.getInitScriptPath().ifPresent(container::withInitScripts);
if (containerConfig.isShowLogs()) {
container.withLogConsumer(new JBossLoggingConsumer(LOG));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public RunningDevServicesDatasource startDatabase(Optional<String> username, Opt

containerConfig.getAdditionalJdbcUrlProperties().forEach(container::withUrlParam);
containerConfig.getCommand().ifPresent(container::setCommand);
containerConfig.getInitScriptPath().ifPresent(container::withInitScript);
containerConfig.getInitScriptPath().ifPresent(container::withInitScripts);
if (containerConfig.isShowLogs()) {
container.withLogConsumer(new JBossLoggingConsumer(LOG));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public RunningDevServicesDatasource startDatabase(Optional<String> username, Opt

containerConfig.getAdditionalJdbcUrlProperties().forEach(container::withUrlParam);
containerConfig.getCommand().ifPresent(container::setCommand);
containerConfig.getInitScriptPath().ifPresent(container::withInitScript);
containerConfig.getInitScriptPath().ifPresent(container::withInitScripts);
if (containerConfig.isShowLogs()) {
container.withLogConsumer(new JBossLoggingConsumer(LOG));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public RunningDevServicesDatasource startDatabase(Optional<String> username, Opt
}
container.setCommand(augmentedCommand);

containerConfig.getInitScriptPath().ifPresent(container::withInitScript);
containerConfig.getInitScriptPath().ifPresent(container::withInitScripts);
if (containerConfig.isShowLogs()) {
container.withLogConsumer(new JBossLoggingConsumer(LOG));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.quarkus.jdbc.postgresql.deployment;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;

import javax.sql.DataSource;

import jakarta.inject.Inject;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

public class DevServicesPostgresqlDatasourceWithMultipleInitScriptsTestCase {

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.withApplicationRoot(javaArchive -> javaArchive.addAsResource("init-db.sql"))
.withApplicationRoot(javaArchive -> javaArchive.addAsResource("init-db-2.sql"))
.overrideConfigKey("quarkus.datasource.db-kind", "postgresql")
.overrideConfigKey("quarkus.datasource.devservices.init-script-path", "init-db.sql, init-db-2.sql");

@Inject
DataSource ds;

@Test
@DisplayName("Test if init-script-path executed successfully")
public void testDatasource() throws Exception {
int result = 0;
try (Connection con = ds.getConnection();
CallableStatement cs = con.prepareCall("SELECT my_func()");
ResultSet rs = cs.executeQuery()) {
if (rs.next()) {
result = rs.getInt(1);
}
}
assertEquals(100, result, "The first init script should have been executed");
try (Connection con = ds.getConnection();
CallableStatement cs = con.prepareCall("SELECT my_func_2()");
ResultSet rs = cs.executeQuery()) {
if (rs.next()) {
result = rs.getInt(1);
}
}
assertEquals(200, result, "The second init script should have been executed");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE OR REPLACE FUNCTION my_func_2() RETURNS integer LANGUAGE plpgsql as $func$ BEGIN return 200; END $func$;

0 comments on commit 90dd16b

Please sign in to comment.