This is an automated email from the ASF dual-hosted git repository.
pefernan pushed a commit to branch main
in repository
https://gitbox.apache.org/repos/asf/incubator-kie-kogito-runtimes.git
The following commit(s) were added to refs/heads/main by this push:
new 113fd689fe kie-issues#1816: Make `kie-flyway` sanitize DataSource
product name before loading migrations. (#3843)
113fd689fe is described below
commit 113fd689fecd92e0b60625dee9965e6d30889d91
Author: Pere Fernández <[email protected]>
AuthorDate: Fri Feb 14 17:21:32 2025 +0100
kie-issues#1816: Make `kie-flyway` sanitize DataSource product name before
loading migrations. (#3843)
* kie-issues#1816: Make `kie-flyway` sanitize DataSource product name
before loading migrations.
* Reformat and small change
---
addons/common/flyway/pom.xml | 10 +++
.../flyway/initializer/KieFlywayInitializer.java | 22 ++----
.../kie/flyway/initializer/db/DataBaseInfo.java} | 38 +++++-----
.../initializer/db/KieFlywayDataBaseHelper.java | 53 ++++++++++++++
.../db/KieFlywayDataBaseHelperTest.java | 85 ++++++++++++++++++++++
.../test/dataSources/PostgreSQLTestDataSource.java | 2 +-
6 files changed, 177 insertions(+), 33 deletions(-)
diff --git a/addons/common/flyway/pom.xml b/addons/common/flyway/pom.xml
index 8c4eb821c6..0f87d80d7b 100644
--- a/addons/common/flyway/pom.xml
+++ b/addons/common/flyway/pom.xml
@@ -86,6 +86,16 @@
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-junit-jupiter</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
\ No newline at end of file
diff --git
a/addons/common/flyway/src/main/java/org/kie/flyway/initializer/KieFlywayInitializer.java
b/addons/common/flyway/src/main/java/org/kie/flyway/initializer/KieFlywayInitializer.java
index 8986b7a428..20ed87864a 100644
---
a/addons/common/flyway/src/main/java/org/kie/flyway/initializer/KieFlywayInitializer.java
+++
b/addons/common/flyway/src/main/java/org/kie/flyway/initializer/KieFlywayInitializer.java
@@ -19,7 +19,6 @@
package org.kie.flyway.initializer;
-import java.sql.Connection;
import java.util.*;
import java.util.stream.Collectors;
@@ -27,12 +26,14 @@ import javax.sql.DataSource;
import org.flywaydb.core.Flyway;
import org.kie.flyway.KieFlywayException;
+import org.kie.flyway.initializer.db.DataBaseInfo;
import org.kie.flyway.initializer.impl.DefaultKieModuleFlywayConfigLoader;
import org.kie.flyway.model.KieFlywayModuleConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static java.util.stream.Collectors.groupingBy;
+import static
org.kie.flyway.initializer.db.KieFlywayDataBaseHelper.readDataBaseInfo;
public class KieFlywayInitializer {
private static final String KIE_FLYWAY_BASELINE_VERSION = "0.0";
@@ -45,13 +46,13 @@ public class KieFlywayInitializer {
private final KieModuleFlywayConfigLoader configLoader;
private final DataSource dataSource;
- private final String databaseType;
+ private final DataBaseInfo databaseInfo;
private final List<String> moduleExclusions;
private KieFlywayInitializer(KieModuleFlywayConfigLoader configLoader,
DataSource dataSource, Collection<String> moduleExclusions) {
this.configLoader = configLoader;
this.dataSource = dataSource;
- this.databaseType = getDataSourceType(dataSource);
+ this.databaseInfo = readDataBaseInfo(dataSource);
this.moduleExclusions = new ArrayList<>(moduleExclusions);
}
@@ -80,15 +81,6 @@ public class KieFlywayInitializer {
}
}
- private String getDataSourceType(DataSource dataSource) {
- try (Connection con = dataSource.getConnection()) {
- return con.getMetaData().getDatabaseProductName().toLowerCase();
- } catch (Exception e) {
- LOGGER.error("Kie Flyway: Couldn't extract database product name
from datasource ", e);
- throw new KieFlywayException("Kie Flyway: Couldn't extract
database product name from datasource.", e);
- }
- }
-
private void runFlyway(KieFlywayModuleConfig config) {
LOGGER.debug("Running Flyway for module: {}", config.getModule());
@@ -97,11 +89,11 @@ public class KieFlywayInitializer {
return;
}
- String[] locations = config.getDBScriptLocations(databaseType);
+ String[] locations =
config.getDBScriptLocations(databaseInfo.getNormalizedName());
if (Objects.isNull(locations)) {
- LOGGER.warn("Cannot run Flyway migration for module `{}`, cannot
find SQL Script locations for db `{}`", config.getModule(), databaseType);
- throw new KieFlywayException("Cannot run Flyway migration for
module `" + config.getModule() + "`, cannot find SQL Script locations for db `"
+ databaseType + "`");
+ LOGGER.warn("Cannot run Flyway migration for module `{}`, cannot
find SQL Script locations for db `{}`", config.getModule(), databaseInfo);
+ throw new KieFlywayException("Cannot run Flyway migration for
module `" + config.getModule() + "`, cannot find SQL Script locations for db `"
+ databaseInfo + "`");
}
Flyway.configure()
diff --git
a/addons/common/flyway/src/test/java/org/kie/flyway/test/dataSources/PostgreSQLTestDataSource.java
b/addons/common/flyway/src/main/java/org/kie/flyway/initializer/db/DataBaseInfo.java
similarity index 52%
copy from
addons/common/flyway/src/test/java/org/kie/flyway/test/dataSources/PostgreSQLTestDataSource.java
copy to
addons/common/flyway/src/main/java/org/kie/flyway/initializer/db/DataBaseInfo.java
index dc909d0fa5..6bafef374b 100644
---
a/addons/common/flyway/src/test/java/org/kie/flyway/test/dataSources/PostgreSQLTestDataSource.java
+++
b/addons/common/flyway/src/main/java/org/kie/flyway/initializer/db/DataBaseInfo.java
@@ -17,31 +17,35 @@
* under the License.
*/
-package org.kie.flyway.test.dataSources;
+package org.kie.flyway.initializer.db;
-import javax.sql.DataSource;
+public class DataBaseInfo {
-import org.kie.kogito.testcontainers.KogitoPostgreSqlContainer;
-import org.postgresql.ds.PGSimpleDataSource;
+ private final String name;
+ private final String version;
+ private final String normalizedName;
-public class PostgreSQLTestDataSource implements TestDataSource {
+ public DataBaseInfo(String name, String version) {
+ this.name = name;
+ this.version = version;
+ this.normalizedName = normalizeName(name);
+ }
- private PGSimpleDataSource dataSource;
+ public String getName() {
+ return name;
+ }
- public PostgreSQLTestDataSource(KogitoPostgreSqlContainer pgContainer) {
- dataSource = new PGSimpleDataSource();
- dataSource.setUrl(pgContainer.getJdbcUrl());
- dataSource.setUser(pgContainer.getUsername());
- dataSource.setPassword(pgContainer.getPassword());
+ public String getVersion() {
+ return version;
}
- @Override
- public String getDbType() {
- return "postgresql";
+ public String getNormalizedName() {
+ return normalizedName;
}
- @Override
- public DataSource getDataSource() {
- return dataSource;
+ private String normalizeName(String name) {
+ final String NORMALIZATION_REGEX = "[^a-zA-Z0-9]+";
+ String[] fragments = name.split(NORMALIZATION_REGEX);
+ return String.join("-", fragments).toLowerCase();
}
}
diff --git
a/addons/common/flyway/src/main/java/org/kie/flyway/initializer/db/KieFlywayDataBaseHelper.java
b/addons/common/flyway/src/main/java/org/kie/flyway/initializer/db/KieFlywayDataBaseHelper.java
new file mode 100644
index 0000000000..fa8b7bb074
--- /dev/null
+++
b/addons/common/flyway/src/main/java/org/kie/flyway/initializer/db/KieFlywayDataBaseHelper.java
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.kie.flyway.initializer.db;
+
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+
+import javax.sql.DataSource;
+
+import org.kie.flyway.KieFlywayException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class KieFlywayDataBaseHelper {
+ private static final Logger LOGGER =
LoggerFactory.getLogger(KieFlywayDataBaseHelper.class);
+
+ private KieFlywayDataBaseHelper() {
+ }
+
+ public static DataBaseInfo readDataBaseInfo(DataSource ds) {
+ try (Connection con = ds.getConnection()) {
+
+ DatabaseMetaData metadata = con.getMetaData();
+
+ String name = metadata.getDatabaseProductName();
+ String version = metadata.getDatabaseProductVersion();
+
+ LOGGER.info("Reading DataBase Product: '{}' Version: '{}'", name,
version);
+
+ return new DataBaseInfo(name, version);
+ } catch (Exception e) {
+ LOGGER.error("Kie Flyway: Couldn't extract database product name
from datasource ", e);
+ throw new KieFlywayException("Kie Flyway: Couldn't extract
database product name from datasource.", e);
+ }
+ }
+}
diff --git
a/addons/common/flyway/src/test/java/org/kie/flyway/initializer/db/KieFlywayDataBaseHelperTest.java
b/addons/common/flyway/src/test/java/org/kie/flyway/initializer/db/KieFlywayDataBaseHelperTest.java
new file mode 100644
index 0000000000..d4d0247073
--- /dev/null
+++
b/addons/common/flyway/src/test/java/org/kie/flyway/initializer/db/KieFlywayDataBaseHelperTest.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.kie.flyway.initializer.db;
+
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.util.stream.Stream;
+
+import javax.sql.DataSource;
+
+import org.assertj.core.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.kie.flyway.KieFlywayException;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import static
org.kie.flyway.initializer.db.KieFlywayDataBaseHelper.readDataBaseInfo;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+public class KieFlywayDataBaseHelperTest {
+
+ @Mock
+ private DataSource dataSource;
+
+ @Mock
+ private Connection connection;
+
+ @Mock
+ private DatabaseMetaData metaData;
+
+ @BeforeEach
+ public void setup() throws Exception {
+ when(dataSource.getConnection()).thenReturn(connection);
+ when(connection.getMetaData()).thenReturn(metaData);
+ }
+
+ @Test
+ public void testReadDataBaseInfoWithException() {
+ Assertions.assertThatThrownBy(() -> readDataBaseInfo(dataSource))
+ .isInstanceOf(KieFlywayException.class)
+ .hasMessage("Kie Flyway: Couldn't extract database product
name from datasource.");
+ }
+
+ @ParameterizedTest
+ @MethodSource("getDataBaseData")
+ public void testReadDataBaseInfo(String productName, String version,
String normalizedName) throws Exception {
+ when(metaData.getDatabaseProductName()).thenReturn(productName);
+ when(metaData.getDatabaseProductVersion()).thenReturn(version);
+
+ Assertions.assertThat(readDataBaseInfo(dataSource))
+ .hasFieldOrPropertyWithValue("name", productName)
+ .hasFieldOrPropertyWithValue("version", version)
+ .hasFieldOrPropertyWithValue("normalizedName", normalizedName);
+ }
+
+ public static Stream<Arguments> getDataBaseData() {
+ return Stream.of(Arguments.of("H2", "2.3.232", "h2"),
+ Arguments.of("PostgreSQL", "42.7.4", "postgresql"),
+ Arguments.of("My Custom DB Type.", "v1.0",
"my-custom-db-type"));
+ }
+
+}
diff --git
a/addons/common/flyway/src/test/java/org/kie/flyway/test/dataSources/PostgreSQLTestDataSource.java
b/addons/common/flyway/src/test/java/org/kie/flyway/test/dataSources/PostgreSQLTestDataSource.java
index dc909d0fa5..b837449358 100644
---
a/addons/common/flyway/src/test/java/org/kie/flyway/test/dataSources/PostgreSQLTestDataSource.java
+++
b/addons/common/flyway/src/test/java/org/kie/flyway/test/dataSources/PostgreSQLTestDataSource.java
@@ -26,7 +26,7 @@ import org.postgresql.ds.PGSimpleDataSource;
public class PostgreSQLTestDataSource implements TestDataSource {
- private PGSimpleDataSource dataSource;
+ private final PGSimpleDataSource dataSource;
public PostgreSQLTestDataSource(KogitoPostgreSqlContainer pgContainer) {
dataSource = new PGSimpleDataSource();
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]