-
Notifications
You must be signed in to change notification settings - Fork 226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
kie-issues#1816: Make kie-flyway
sanitize DataSource product name before loading migrations.
#3843
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
addons/common/flyway/src/main/java/org/kie/flyway/initializer/db/DataBaseInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* 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; | ||
|
||
public class DataBaseInfo { | ||
|
||
private final String name; | ||
private final String version; | ||
private final String normalizedName; | ||
|
||
public DataBaseInfo(String name, String version) { | ||
this.name = name; | ||
this.version = version; | ||
this.normalizedName = normalizeName(name); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getVersion() { | ||
return version; | ||
} | ||
|
||
public String getNormalizedName() { | ||
return normalizedName; | ||
} | ||
|
||
private String normalizeName(String name) { | ||
final String NORMALIZATION_REGEX = "[^a-zA-Z0-9]+"; | ||
String[] fragments = name.split(NORMALIZATION_REGEX); | ||
return String.join("-", fragments).toLowerCase(); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...ns/common/flyway/src/main/java/org/kie/flyway/initializer/db/KieFlywayDataBaseHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
...ommon/flyway/src/test/java/org/kie/flyway/initializer/db/KieFlywayDataBaseHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nicely done @pefernan.
Just wondering if we need to override
toString()
to print the database info