-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
) Co-authored-by: Daniel Mallorga <[email protected]> Co-authored-by: Jake Newton <[email protected]>
- Loading branch information
1 parent
242dce4
commit 56a5e0f
Showing
4 changed files
with
111 additions
and
0 deletions.
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" | ||
|
||
} | ||
|
||
} |