-
Notifications
You must be signed in to change notification settings - Fork 34
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
Fix issue #278: add compliancemode parameter in JDBC URL if absent #298
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fbe93ae
Fix issue #278: add compliancemode parameter in JDBC URL if absent
maximevw b52eb38
Merge branch 'main' into fix/issue-278
MalloD12 105b33e
Merge branch 'main' into fix/issue-278
jnewton03 5fa253a
Merge branch 'main' into fix/issue-278
jnewton03 fd511cf
Merge branch 'main' into fix/issue-278
jnewton03 2d5950f
Merge branch 'liquibase:main' into fix/issue-278
maximevw fe15966
Merge branch 'main' into fix/issue-278
maximevw 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
65 changes: 65 additions & 0 deletions
65
src/main/java/liquibase/ext/cassandra/database/CassandraDatabaseConnection.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,65 @@ | ||
package liquibase.ext.cassandra.database; | ||
|
||
import com.ing.data.cassandra.jdbc.CassandraDriver; | ||
import liquibase.Scope; | ||
import liquibase.database.jvm.JdbcConnection; | ||
import liquibase.exception.DatabaseException; | ||
|
||
import java.net.URI; | ||
import java.sql.Driver; | ||
import java.util.Properties; | ||
|
||
public class CassandraDatabaseConnection extends JdbcConnection { | ||
|
||
@Override | ||
public int getPriority() { | ||
return 201; | ||
} | ||
|
||
@Override | ||
public void open(String url, Driver driverObject, Properties driverProperties) throws DatabaseException { | ||
String jdbcUrl = url; | ||
|
||
// When using Cassandra with com.ing.data.cassandra.jdbc.CassandraDriver, it is required to specify the | ||
// compliance mode "Liquibase" in the JDBC URL. So, do it by default when it's necessary. | ||
if (driverObject instanceof CassandraDriver) { | ||
try { | ||
boolean complianceModePresent = false; | ||
final String liquibaseComplianceModeParameter = "compliancemode=Liquibase"; | ||
// Replace the actual protocol (jdbc:cassandra:// or any other) by a valid protocol for URI | ||
// parsing (jdbc://) | ||
final int protocolEndIdx = jdbcUrl.indexOf("://"); | ||
final String parseableUrl = "jdbc" + jdbcUrl.substring(protocolEndIdx); | ||
final URI jdbcUri = URI.create(parseableUrl); | ||
final String queryPart = jdbcUri.getQuery(); | ||
if (queryPart != null) { | ||
String[] queryParams = queryPart.split("&"); | ||
for (String queryParam : queryParams) { | ||
if (liquibaseComplianceModeParameter.equals(queryParam)) { | ||
complianceModePresent = true; | ||
break; | ||
} | ||
} | ||
if (!complianceModePresent) { | ||
jdbcUrl += "&" + liquibaseComplianceModeParameter; | ||
} | ||
} else { | ||
jdbcUrl += "?" + liquibaseComplianceModeParameter; | ||
} | ||
Scope.getCurrentScope().getLog(CassandraDatabaseConnection.class) | ||
.info("Connecting to Cassandra using: " + jdbcUrl); | ||
} catch (IllegalArgumentException e) { | ||
Scope.getCurrentScope().getLog(CassandraDatabaseConnection.class) | ||
.warning("Unable to check compliance mode in JDBC URL, connecting with configured URL. " | ||
+ "The compliance mode might be incorrect."); | ||
} | ||
} | ||
|
||
openConnection(jdbcUrl, driverObject, driverProperties); | ||
} | ||
|
||
void openConnection(String url, Driver driverObject, Properties driverProperties) throws DatabaseException { | ||
super.open(url, driverObject, driverProperties); | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
src/main/resources/META-INF/services/liquibase.database.DatabaseConnection
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 @@ | ||
liquibase.ext.cassandra.database.CassandraDatabaseConnection |
39 changes: 39 additions & 0 deletions
39
src/test/groovy/liquibase/ext/cassandra/database/CassandraDatabaseConnectionTest.groovy
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,39 @@ | ||
package liquibase.ext.cassandra.database | ||
|
||
import com.ing.data.cassandra.jdbc.CassandraDriver | ||
import liquibase.exception.DatabaseException | ||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
|
||
import java.sql.Driver | ||
|
||
class CassandraDatabaseConnectionTest extends Specification { | ||
|
||
@Unroll | ||
def "open with #url"() { | ||
given: | ||
def cassandraConnection = Spy(new CassandraDatabaseConnection() { | ||
// Overriding the real connection call, we just want to test it's called with the expected URL. | ||
@Override | ||
void openConnection(String jdbcUrl, Driver driverObject, Properties driverProperties) throws DatabaseException { | ||
println("Mock open connection with URL: $jdbcUrl") | ||
} | ||
}) | ||
def cassandraDriver = new CassandraDriver() | ||
def cassandraDriverProperties = new Properties() | ||
|
||
when: | ||
cassandraConnection.open(url, cassandraDriver, cassandraDriverProperties) | ||
|
||
then: | ||
1 * cassandraConnection.openConnection(expectedUrl, cassandraDriver, cassandraDriverProperties) | ||
|
||
where: | ||
url | expectedUrl | ||
"jdbc:cassandra://localhost:9042/betterbotz?compliancemode=Liquibase&localdatacenter=datacenter1" | "jdbc:cassandra://localhost:9042/betterbotz?compliancemode=Liquibase&localdatacenter=datacenter1" | ||
"jdbc:cassandra://localhost:9042/betterbotz?localdatacenter=datacenter1" | "jdbc:cassandra://localhost:9042/betterbotz?localdatacenter=datacenter1&compliancemode=Liquibase" | ||
"jdbc:cassandra://localhost:9042/betterbotz" | "jdbc:cassandra://localhost:9042/betterbotz?compliancemode=Liquibase" | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.
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.
Usually we do CONSTANT + increment, but we can fix it 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.
Thanks for the recommandation @filipelautert: I didn't know how was fixed this value, so I took a value for which I was almost sure the
CassandraDatabaseConnection
will be used. I can fix that in a separate PR, as you suggested; do you have an example of the good way to do that?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.
@maximevw at liquibase-core we have PRIORITY_DATABASE + 5, like in https://github.com/liquibase/liquibase/blob/53db8b6d12a9db371bbcd620f99aca954d3bbbfd/liquibase-standard/src/main/java/liquibase/database/core/EnterpriseDBDatabase.java#L46 . But could be +200, no problems!