liquibase-cassandra icon indicating copy to clipboard operation
liquibase-cassandra copied to clipboard

Integration with spring boot

Open dzasti opened this issue 1 year ago • 11 comments

Hello, is there a possibility to run liquibase cassandra extension via spring boot? Can’t find any example online. Also I’ve done some research: https://github.com/spring-projects/spring-boot/issues/29991#issuecomment-1050240805 , https://github.com/liquibase/liquibase-cassandra/issues/4#issuecomment-689828658. Seems confusing, could someone show me an example, if its possible ?

thanks

dzasti avatar Sep 26 '23 05:09 dzasti

Hello @dzasti,

It should be possible to use Spring Liquibase with Cassandra as I successfully ran it with the example below.

First of all, I used a slightly modified version of liquibase-cassandra using cassandra-jdbc-wrapper (4.9.1 or above). For further details, see here: https://github.com/liquibase/liquibase-cassandra/issues/148#issuecomment-1704090680 But, I guess it should also work with Simba JDBC driver (if you have an appropriate licence).

The dependencies to include in your POM file are:

<!-- Using Spring Boot 3.1.4 and Java 17 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
	<groupId>org.liquibase</groupId>
	<artifactId>liquibase-core</artifactId>
</dependency>
<!-- Include cassandra-jdbc-wrapper if you don't use Simba JDBC, otherwise include a dependency to Simba driver -->
<dependency>
	<groupId>com.ing.data</groupId>
	<artifactId>cassandra-jdbc-wrapper</artifactId>
	<version>4.10.0</version>
</dependency>
<dependency>
	<groupId>org.liquibase.ext</groupId>
	<artifactId>liquibase-cassandra</artifactId>
	<!-- Use here a version modified to use cassandra-jdbc-wrapper instead of Simba if necessary-->
	<version>4.23.2</version>
</dependency>

<!-- Required to avoid runtime exceptions due to a conflict of version between Spring Boot dependencies and cassandra-jdbc-wrapper -->
<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>com.datastax.oss</groupId>
			<artifactId>java-driver-core</artifactId>
			<version>4.17.0</version>
		</dependency>
	</dependencies>
</dependencyManagement>

Assuming you have a Cassandra instance running on localhost, port 9042, with a keyspace named test_keyspace.

In your application.properties file, you should have this:

# Liquibase configuration
spring.liquibase.url=jdbc:cassandra://localhost:9042/test_keyspace?localdatacenter=datacenter1&user=cassandra&password=cassandra&compliancemode=Liquibase
spring.liquibase.change-log=classpath:/db/changelog/db.changelog-master.yaml
spring.liquibase.driver-class-name=com.ing.data.cassandra.jdbc.CassandraDriver

# Spring datasource configuration
spring.datasource.name=cassandra_datasource
spring.datasource.url=jdbc:cassandra://localhost:9042/test_keyspace?localdatacenter=datacenter1&user=cassandra&password=cassandra
spring.datasource.driver-class-name=com.ing.data.cassandra.jdbc.CassandraDriver
spring.datasource.hikari.minimum-idle=1

In your project resources, you should have the following files:

  • db/changelog/db.changelog-master.yaml
databaseChangeLog:
  - include:
      file: classpath:/db/changelog/init.sql
  • db/changelog/init.sql. Note the extension MUST be "sql" otherwise Liquibase will fail to parse the script.
USE test_keyspace;
CREATE TABLE IF NOT EXISTS tbl_test (
keyname text PRIMARY KEY,
bool_col boolean,
int_col int);

INSERT INTO tbl_test(keyname, bool_col, int_col) VALUES ('key1', true, 1) IF NOT EXISTS;
INSERT INTO tbl_test(keyname, bool_col, int_col) VALUES ('key2', false, 2) IF NOT EXISTS;

Finally, your Spring Boot application:

@SpringBootApplication
public class SpringLiquibaseCassandraApplication {

	@Autowired
	JdbcTemplate jdbcTemplate;

	@EventListener(ApplicationReadyEvent.class)
	public void checkData() throws SQLException {
		jdbcTemplate.query("SELECT * FROM tbl_test", rs -> {
            System.out.println("------------------------------------");
            System.out.println("keyname:  " + rs.getString(1));
            System.out.println("bool_col: " + rs.getBoolean(2));
            System.out.println("int_col:  " + rs.getInt(3));
        });
    }

	public static void main(String[] args) {
		SpringApplication.run(SpringLiquibaseCassandraApplication.class, args);
	}

}

Running your application, the output should be the following, proving the Liquibase changes have been executed successfully:

------------------------------------
keyname:  key1
bool_col: true
int_col:  1
------------------------------------
keyname:  key2
bool_col: false
int_col:  2

maximevw avatar Sep 30 '23 15:09 maximevw

as like we are connecting sql db from spring boot using liquibase it is possible to connect aws keyspace using liquibase without jdbc in this example you guys put local cassendra can we use amazone keypace to run our jar at ec2 and connect with keyspace and create tables there

shubha11m avatar May 25 '24 10:05 shubha11m

@shubha11m Liquibase is based on JDBC, so it's not possible to use it without JDBC. But it should not really be a problem. Theoretically, it could be possible to connect to AWS Keyspaces, but as I said in previous discussions, some operations performed by Liquibase are currently not supported by AWS Keyspaces (see #297 for further details). However, to connect to AWS Keyspaces using JDBC, you could use this minimal connection string: jdbc:cassandra://cassandra.{ec2-region}.amazonaws.com:9142/{keyspace}?compliancemode=Liquibase (the parameter compliancemode=Liquibase is only required when used with Liquibase, {keyspace} is the target keyspace you want to use and {ec2-region} has to be replaced by the appropriate EC2 region of your server). The documentation of the JDBC connection string for the driver used by liquibase-cassandra is available here.

maximevw avatar May 25 '24 12:05 maximevw

HI @maximevw thankyou for you response so as previously the snip of code is there to connect keyspace with spring boot is it whole code or do we need to add cql config file and other than that and are we not able to put our changes into xml file rather then .sql

i had applied all changes https://github.com/liquibase/liquibase-cassandra/issues/212#issuecomment-1741795280 but when i run the appliction its just run as plain spring boot application and not executing the the liquibase chnagelog file

shubha11m avatar May 25 '24 18:05 shubha11m

Hi @shubha11m

The example given in this issue is just an example among a lot of possible configurations, so, obviously you can adapt it according to your needs. If you prefer use xml changelog, you can.

A lot of documentation about running Liquibase with SpringBoot is available here and in the linked pages.

Maybe try to specify the changelog location in the application.properties file:

spring.liquibase.change-log=classpath:db/changelog/db.changelog-master.xml # Adapt the path to match your changelog file.

Also, ensure the dependencies are up-to-date (those mentioned in this example are old and may contain some bugs):

<dependency>
	<groupId>com.ing.data</groupId>
	<artifactId>cassandra-jdbc-wrapper</artifactId>
	<version>4.12.0</version>
</dependency>
<dependency>
	<groupId>org.liquibase.ext</groupId>
	<artifactId>liquibase-cassandra</artifactId>
	<version>4.28.0</version>
</dependency>

maximevw avatar May 26 '24 12:05 maximevw

Hi @maximevw, I have tried with the versions you suggested, scripts appears as executed in databasechangelog table but the changes are not reflected, do you have any idea of the problem?

nicobte avatar May 28 '24 13:05 nicobte

Hello @nicobte Hard to say... did you check the logs of your Spring Boot application? By default, they should indicate each item in the changelog and if it ran. Did you not see any errors? You can also set a lower level of log if necessary. It also could depend on your changelog, some changes could be filtered for some reason.

maximevw avatar May 28 '24 17:05 maximevw

@maximevw i have a question about usage of cassandra-jdbc-wrapper:4.12.0 in last release of liquibase-cassandra (4.28). Seems, like in LockServiceCassandra there are multiple checks for updatedRows count. but with debugging i've realized, that in cassandra-jdbc-wrapper DefaultOptionSet always returns 0 in getSQLUpdateResponse() method. so it's impossible to get correct updated rows count for LockServiceCassandra

Danil9966 avatar Jul 01 '24 07:07 Danil9966

Hello @Danil9966

Cassandra database is not able to return the number of updated rows, that's why you meet this behaviour, but when running with Liquibase, your JDBC URL should always have the query parameter compliancemode=Liquibase as explained here. In this case, the default option set is not used and replaced by a specific behaviour for Liquibase.

maximevw avatar Jul 01 '24 11:07 maximevw

@maximevw thank you! Is there a proper way to set this query parameter from configs or java code? CassandraDataSource (4.12.0 version) doesn't have complienceMode parameter

Danil9966 avatar Jul 01 '24 12:07 Danil9966

@Danil9966 Indeed, CassandraDataSource currently doesn't support compliance mode parameter. For this, it's better to open a dedicated issue here in the JDBC driver project.

Alternatively, using Java code, you have to use the DriverManager for now, for example:

final String url = "jdbc:cassandra://localhost:9042/keyspace?localdatacenter=DC1&compliancemode=Liquibase";
final Connection connection = DriverManager.getConnection(url);

maximevw avatar Jul 01 '24 12:07 maximevw