From 853a2e207e9cef80b52cb4330a42c1de41b3ce01 Mon Sep 17 00:00:00 2001 From: Marcell Cruz Date: Thu, 8 Dec 2022 09:37:37 -0300 Subject: [PATCH] Introduces quarkus.flyway.username, quarkus.flyway.password and quarkus.flyway.jdbc-url config Co-Authored-By: George Gastaldi --- .../test/FlywayExtensionConfigFixture.java | 12 ++++++++++ .../quarkus/flyway/runtime/FlywayCreator.java | 24 ++++++++++++++++++- .../FlywayDataSourceRuntimeConfig.java | 24 +++++++++++++++++++ integration-tests/flyway/pom.xml | 15 ++++++------ .../java/io/quarkus/it/flyway/AppEntity.java | 10 ++++++++ .../flyway/FlywayFunctionalityResource.java | 9 ++++++- .../src/main/resources/application.properties | 20 +++++++++------- .../db/location1/V1.0.1__Quarkus.sql | 5 ++-- .../db/location2/V1.0.0__Quarkus.sql | 6 ++--- .../flyway/src/main/resources/initdb.sql | 3 +++ .../it/flyway/FlywayFunctionalityTest.java | 16 +++++++++++-- .../it/flyway/FlywayTestResources.java | 8 ------- 12 files changed, 119 insertions(+), 33 deletions(-) mode change 100644 => 100755 extensions/flyway/deployment/src/test/java/io/quarkus/flyway/test/FlywayExtensionConfigFixture.java mode change 100644 => 100755 extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayCreator.java mode change 100644 => 100755 extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayDataSourceRuntimeConfig.java create mode 100644 integration-tests/flyway/src/main/resources/initdb.sql delete mode 100644 integration-tests/flyway/src/test/java/io/quarkus/it/flyway/FlywayTestResources.java diff --git a/extensions/flyway/deployment/src/test/java/io/quarkus/flyway/test/FlywayExtensionConfigFixture.java b/extensions/flyway/deployment/src/test/java/io/quarkus/flyway/test/FlywayExtensionConfigFixture.java old mode 100644 new mode 100755 index 29a1dd6910a93..06995fe92d44c --- a/extensions/flyway/deployment/src/test/java/io/quarkus/flyway/test/FlywayExtensionConfigFixture.java +++ b/extensions/flyway/deployment/src/test/java/io/quarkus/flyway/test/FlywayExtensionConfigFixture.java @@ -140,6 +140,18 @@ public boolean migrateAtStart(String datasourceName) { return getBooleanValue("quarkus.flyway.migrate-at-start", datasourceName); } + public String username(String datasourceName) { + return getStringValue("quarkus.flyway.%s.username", datasourceName); + } + + public String password(String datasourceName) { + return getStringValue("quarkus.flyway.%s.password", datasourceName); + } + + public String jdbcUrl(String datasourceName) { + return getStringValue("quarkus.flyway.%s.jdbc-url", datasourceName); + } + private String getStringValue(String parameterName, String datasourceName) { return getValue(parameterName, datasourceName, String.class); } diff --git a/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayCreator.java b/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayCreator.java old mode 100644 new mode 100755 index 47dc567decbaf..4a808f33cd33e --- a/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayCreator.java +++ b/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayCreator.java @@ -12,6 +12,8 @@ import org.flywaydb.core.api.callback.Callback; import org.flywaydb.core.api.configuration.FluentConfiguration; +import io.agroal.api.AgroalDataSource; + class FlywayCreator { private static final String[] EMPTY_ARRAY = new String[0]; @@ -33,7 +35,27 @@ public FlywayCreator withCallbacks(Collection callbacks) { public Flyway createFlyway(DataSource dataSource) { FluentConfiguration configure = Flyway.configure(); - configure.dataSource(dataSource); + + if (flywayRuntimeConfig.jdbcUrl.isPresent()) { + if (flywayRuntimeConfig.username.isPresent() && flywayRuntimeConfig.password.isPresent()) { + configure.dataSource(flywayRuntimeConfig.jdbcUrl.get(), flywayRuntimeConfig.username.get(), + flywayRuntimeConfig.password.get()); + } else { + throw new RuntimeException("User name and password must be set when the JDBC URL is provided"); + } + } else { + if (flywayRuntimeConfig.username.isPresent() && flywayRuntimeConfig.password.isPresent()) { + AgroalDataSource agroalDataSource = (AgroalDataSource) dataSource; + String jdbcUrl = agroalDataSource.getConfiguration().connectionPoolConfiguration() + .connectionFactoryConfiguration().jdbcUrl(); + + configure.dataSource(jdbcUrl, flywayRuntimeConfig.username.get(), + flywayRuntimeConfig.password.get()); + } else { + + configure.dataSource(dataSource); + } + } if (flywayRuntimeConfig.initSql.isPresent()) { configure.initSql(flywayRuntimeConfig.initSql.get()); } diff --git a/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayDataSourceRuntimeConfig.java b/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayDataSourceRuntimeConfig.java old mode 100644 new mode 100755 index c76cbfcbae06d..859e030ed5bd1 --- a/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayDataSourceRuntimeConfig.java +++ b/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayDataSourceRuntimeConfig.java @@ -43,6 +43,30 @@ public static FlywayDataSourceRuntimeConfig defaultConfig() { @ConfigItem public Optional defaultSchema = Optional.empty(); + /** + * The JDBC URL that Flyway will use to connect to the datasource. + * Falls back to the datasource URL if not specified. + */ + + @ConfigItem + public Optional jdbcUrl = Optional.empty(); + + /** + * Sets the user that Flyway will use to connect to the datasource. + * Falls back to the datasource username if not specified. + */ + + @ConfigItem + public Optional username = Optional.empty(); + + /** + * Sets the password that Flyway will use to connect to the datasource. + * Falls back to the datasource password if not specified. + */ + + @ConfigItem + public Optional password = Optional.empty(); + /** * Comma-separated case-sensitive list of schemas managed by Flyway. * The first schema in the list will be automatically set as the default one during the migration. diff --git a/integration-tests/flyway/pom.xml b/integration-tests/flyway/pom.xml index 45b355ddeb9aa..bddf06c096556 100644 --- a/integration-tests/flyway/pom.xml +++ b/integration-tests/flyway/pom.xml @@ -21,9 +21,15 @@ io.quarkus quarkus-hibernate-orm + + io.quarkus + quarkus-junit5-internal + test + + io.quarkus - quarkus-jdbc-h2 + quarkus-jdbc-postgresql io.quarkus @@ -39,11 +45,6 @@ quarkus-junit5 test - - io.quarkus - quarkus-test-h2 - test - io.rest-assured rest-assured @@ -85,7 +86,7 @@ io.quarkus - quarkus-jdbc-h2-deployment + quarkus-jdbc-postgresql-deployment ${project.version} pom test diff --git a/integration-tests/flyway/src/main/java/io/quarkus/it/flyway/AppEntity.java b/integration-tests/flyway/src/main/java/io/quarkus/it/flyway/AppEntity.java index e0f5de42a5f84..ee2446424124e 100644 --- a/integration-tests/flyway/src/main/java/io/quarkus/it/flyway/AppEntity.java +++ b/integration-tests/flyway/src/main/java/io/quarkus/it/flyway/AppEntity.java @@ -18,6 +18,8 @@ public class AppEntity { private String name; + private String createdBy; + public int getId() { return id; } @@ -34,6 +36,14 @@ public void setName(final String name) { this.name = name; } + public String getCreatedBy() { + return createdBy; + } + + public void setCreatedBy(String createdBy) { + this.createdBy = createdBy; + } + @Override public boolean equals(final Object o) { if (this == o) diff --git a/integration-tests/flyway/src/main/java/io/quarkus/it/flyway/FlywayFunctionalityResource.java b/integration-tests/flyway/src/main/java/io/quarkus/it/flyway/FlywayFunctionalityResource.java index 5b86041d96ed9..7661d9bc90ff3 100644 --- a/integration-tests/flyway/src/main/java/io/quarkus/it/flyway/FlywayFunctionalityResource.java +++ b/integration-tests/flyway/src/main/java/io/quarkus/it/flyway/FlywayFunctionalityResource.java @@ -71,8 +71,15 @@ public String returnInitSql() { @GET @Path("init-sql-result") public Integer returnInitSqlResult() { - return (Integer) entityManager.createNativeQuery("SELECT ONE_HUNDRED") + return (Integer) entityManager.createNativeQuery("SELECT f_my_constant()") .getSingleResult(); } + @GET + @Path("created-by") + public String returnCreatedByUser() { + return entityManager.createQuery("select a from AppEntity a where a.id = 1", AppEntity.class) + .getSingleResult().getCreatedBy(); + } + } diff --git a/integration-tests/flyway/src/main/resources/application.properties b/integration-tests/flyway/src/main/resources/application.properties index 5c17c56d61759..c4068807ba33f 100644 --- a/integration-tests/flyway/src/main/resources/application.properties +++ b/integration-tests/flyway/src/main/resources/application.properties @@ -1,16 +1,19 @@ quarkus.log.console.level=DEBUG quarkus.log.category."org.flywaydb.core".level=DEBUG -quarkus.log.category."io.quarkus.flyway".level=DEBUG +quarkus.log.category."io.quarkus.test".level=DEBUG + # Agroal config -quarkus.datasource.db-kind=h2 -quarkus.datasource.username=sa -quarkus.datasource.password=sa +quarkus.datasource.db-kind=postgresql +quarkus.datasource.devservices.init-script-path=initdb.sql + # default flyway configuration properties -quarkus.datasource.jdbc.url=jdbc:h2:tcp://localhost/mem:test_quarkus;DB_CLOSE_DELAY=-1 quarkus.flyway.connect-retries=10 quarkus.flyway.schemas=TEST_SCHEMA quarkus.flyway.table=flyway_quarkus_history +quarkus.flyway.username=scott +quarkus.flyway.password=tiger + # spaces should be trimmed quarkus.flyway.locations= db/location1 , classpath:db/location2, classpath:/db/location4 quarkus.flyway.sql-migration-prefix=V @@ -19,16 +22,15 @@ quarkus.flyway.placeholders.foo=bar quarkus.flyway.placeholders.title=REPLACED quarkus.flyway.placeholder-prefix=#[ quarkus.flyway.placeholder-suffix=] -quarkus.flyway.init-sql=CREATE CONSTANT IF NOT EXISTS ONE_HUNDRED VALUE 100 - +quarkus.flyway.init-sql=CREATE SCHEMA IF NOT EXISTS TEST_SCHEMA;CREATE OR REPLACE FUNCTION f_my_constant() RETURNS integer LANGUAGE plpgsql as $func$ BEGIN return 100; END $func$; quarkus.hibernate-orm.database.generation=validate # second Agroal config -quarkus.datasource.second-datasource.db-kind=h2 -quarkus.datasource.second-datasource.jdbc.url=jdbc:h2:tcp://localhost/mem:test_multiple_flyway_datasource;DB_CLOSE_DELAY=-1 +quarkus.datasource.second-datasource.db-kind=postgresql # configuration to test multiple flyway datasources quarkus.flyway.second-datasource.locations=db/location3 quarkus.flyway.second-datasource.sql-migration-prefix=V quarkus.flyway.second-datasource.migrate-at-start=true quarkus.flyway.second-datasource.placeholders.mambo=poa + diff --git a/integration-tests/flyway/src/main/resources/db/location1/V1.0.1__Quarkus.sql b/integration-tests/flyway/src/main/resources/db/location1/V1.0.1__Quarkus.sql index 9560c6c740522..a09ee0986da15 100644 --- a/integration-tests/flyway/src/main/resources/db/location1/V1.0.1__Quarkus.sql +++ b/integration-tests/flyway/src/main/resources/db/location1/V1.0.1__Quarkus.sql @@ -1,7 +1,8 @@ CREATE TABLE TEST_SCHEMA.quarkus_table2 ( id INT, - name VARCHAR(20) + name VARCHAR(20), + createdBy VARCHAR(100) DEFAULT CURRENT_USER NOT NULL ); INSERT INTO TEST_SCHEMA.quarkus_table2(id, name) -VALUES (1, '1.0.1 #[title]'); \ No newline at end of file +VALUES (1, '1.0.1 #[title]'); diff --git a/integration-tests/flyway/src/main/resources/db/location2/V1.0.0__Quarkus.sql b/integration-tests/flyway/src/main/resources/db/location2/V1.0.0__Quarkus.sql index a727ba1ead903..fa82e5645dc3e 100644 --- a/integration-tests/flyway/src/main/resources/db/location2/V1.0.0__Quarkus.sql +++ b/integration-tests/flyway/src/main/resources/db/location2/V1.0.0__Quarkus.sql @@ -1,7 +1,7 @@ CREATE TABLE TEST_SCHEMA.quarkus ( id INT, - name VARCHAR(20) + name VARCHAR(20), + createdBy VARCHAR(100) DEFAULT CURRENT_USER NOT NULL ); -INSERT INTO TEST_SCHEMA.quarkus(id, name) -VALUES (1, '#[foo]'); \ No newline at end of file +INSERT INTO TEST_SCHEMA.quarkus(id, name) VALUES (1, '#[foo]'); \ No newline at end of file diff --git a/integration-tests/flyway/src/main/resources/initdb.sql b/integration-tests/flyway/src/main/resources/initdb.sql new file mode 100644 index 0000000000000..d37e68a1e1a63 --- /dev/null +++ b/integration-tests/flyway/src/main/resources/initdb.sql @@ -0,0 +1,3 @@ +CREATE USER scott with login password 'tiger'; +GRANT ALL ON DATABASE quarkus TO scott; + diff --git a/integration-tests/flyway/src/test/java/io/quarkus/it/flyway/FlywayFunctionalityTest.java b/integration-tests/flyway/src/test/java/io/quarkus/it/flyway/FlywayFunctionalityTest.java index ee6f17a074f75..59bd504667ea8 100644 --- a/integration-tests/flyway/src/test/java/io/quarkus/it/flyway/FlywayFunctionalityTest.java +++ b/integration-tests/flyway/src/test/java/io/quarkus/it/flyway/FlywayFunctionalityTest.java @@ -5,11 +5,14 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.condition.OS; import io.quarkus.test.junit.QuarkusTest; @QuarkusTest @DisplayName("Tests flyway extension") +@DisabledOnOs(value = OS.WINDOWS, disabledReason = "Our Windows CI does not have Docker installed properly") public class FlywayFunctionalityTest { @Test @@ -43,9 +46,10 @@ public void testPlaceholdersPrefixSuffix() { } @Test - @DisplayName("Returns whether the init-sql is CREATE CONSTANT IF NOT EXISTS ONE_HUNDRED VALUE 100 or not") + @DisplayName("Returns whether the init-sql is CREATE SCHEMA IF NOT EXISTS TEST_SCHEMA;CREATE OR REPLACE FUNCTION f_my_constant() RETURNS integer LANGUAGE plpgsql as $func$ BEGIN return 100; END $func$; or not") public void testReturnInitSql() { - when().get("/flyway/init-sql").then().body(is("CREATE CONSTANT IF NOT EXISTS ONE_HUNDRED VALUE 100")); + when().get("/flyway/init-sql").then().body(is( + "CREATE SCHEMA IF NOT EXISTS TEST_SCHEMA;CREATE OR REPLACE FUNCTION f_my_constant() RETURNS integer LANGUAGE plpgsql as $func$ BEGIN return 100; END $func$;")); } @Test @@ -54,4 +58,12 @@ public void testInitSqlExecuted() { when().get("/flyway/init-sql-result").then().body(is("100")); } + @Test + @DisplayName("Returns if the created-by user is scott") + public void testCreatedByUserIsDifferent() { + when().get("/flyway/created-by").then() + .log().ifValidationFails() + .body(is("scott")); + } + } diff --git a/integration-tests/flyway/src/test/java/io/quarkus/it/flyway/FlywayTestResources.java b/integration-tests/flyway/src/test/java/io/quarkus/it/flyway/FlywayTestResources.java deleted file mode 100644 index a21293f94ad99..0000000000000 --- a/integration-tests/flyway/src/test/java/io/quarkus/it/flyway/FlywayTestResources.java +++ /dev/null @@ -1,8 +0,0 @@ -package io.quarkus.it.flyway; - -import io.quarkus.test.common.QuarkusTestResource; -import io.quarkus.test.h2.H2DatabaseTestResource; - -@QuarkusTestResource(H2DatabaseTestResource.class) -public class FlywayTestResources { -}