Skip to content

Commit

Permalink
Merge branch 'main' into bug-fix-java-8-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jandroav committed May 22, 2024
2 parents 372440c + c44544d commit 2057fb5
Show file tree
Hide file tree
Showing 15 changed files with 106 additions and 99 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/attach-artifact-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ on:
pull_request:
types:
- closed
push:
branches:
- main

jobs:

attach-artifact-to-release:
uses: liquibase/build-logic/.github/workflows/extension-attach-artifact-release.yml@v0.5.5
uses: liquibase/build-logic/.github/workflows/extension-attach-artifact-release.yml@v0.7.7
secrets: inherit
18 changes: 18 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: CodeQL

on:
workflow_dispatch:
push:
branches: [ "main" ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ "main" ]
schedule:
- cron: '16 14 * * 4'

jobs:
codeql:
uses: liquibase/build-logic/.github/workflows/codeql.yml@v0.7.7
secrets: inherit
with:
languages: '["java"]'
2 changes: 1 addition & 1 deletion .github/workflows/create-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ permissions:

jobs:
create-release:
uses: liquibase/build-logic/.github/workflows/create-release.yml@v0.5.5
uses: liquibase/build-logic/.github/workflows/create-release.yml@v0.7.7
secrets: inherit
6 changes: 1 addition & 5 deletions .github/workflows/release-published.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ on:
workflow_dispatch:
release:
types: [published]

permissions:
contents: write
pull-requests: write

jobs:
release:
uses: liquibase/build-logic/.github/workflows/extension-release-published.yml@v0.5.5
uses: liquibase/build-logic/.github/workflows/extension-release-published.yml@v0.7.7
secrets: inherit
25 changes: 18 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,38 @@ name: Build and Test

on:
workflow_dispatch:
pull_request:
pull_request_target:
types:
- opened
- reopened
- synchronize
push:
branches:
- main

permissions:
contents: write
pull-requests: write

jobs:
build:
uses: liquibase/build-logic/.github/workflows/os-extension-test.yml@v0.5.5

authorize:
environment: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.repo.full_name != github.repository && 'external' || 'internal' }}
runs-on: ubuntu-latest
steps:
- run: true

build-test:
needs: authorize
uses: liquibase/build-logic/.github/workflows/os-extension-test.yml@v0.7.7
secrets: inherit
with:
java: '[11, 17, 18]'
java: "[11, 17, 21]"

integration-tests:
name: Integration Tests -- Java ${{ matrix.java }} on Cassandra ${{ matrix.cassandra }}
runs-on: ubuntu-latest
needs: build
needs: build-test
strategy:
matrix:
java: [11, 17]
Expand Down Expand Up @@ -78,5 +89,5 @@ jobs:

dependabot:
needs: integration-tests
uses: liquibase/build-logic/.github/workflows/dependabot-automerge.yml@v0.5.3
secrets: inherit
uses: liquibase/build-logic/.github/workflows/dependabot-automerge.yml@v0.7.7
secrets: inherit
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
.classpath
.project
.settings/
/build
10 changes: 0 additions & 10 deletions .snyk

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ liquibase-cassandra[![Build and Test Extention](https://github.com/liquibase/liq
Liquibase extension for Cassandra Support.

# Using the Liquibase Test Harness in the Cassandra Extension
The liquibase-snowflake extension now comes with integration test support via the liquibase-test-harness.
The liquibase-cassandra extension now comes with integration test support via the liquibase-test-harness.
This Liquibase test framework is designed to *also* make it easy for you to test your extensions.

### Configuring your project
Expand Down
Binary file removed lib/cassandra-jdbc-wrapper-4.9.1-bundle.jar
Binary file not shown.
5 changes: 2 additions & 3 deletions liquibase.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
changeLogFile: changelog.xml
url: jdbc:cassandra://localhost:9042;AuthMech=1;DefaultKeyspace=betterbotz
url: jdbc:cassandra://localhost:9042/betterbotz?compliancemode=Liquibase&localdatacenter=datacenter1
username: cassandra
password: Password1
driver: com.simba.cassandra.jdbc42.Driver
defaultSchemaName: betterbotz
defaultSchemaName: betterbotz
31 changes: 9 additions & 22 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-parent-pom</artifactId>
<version>0.2.6</version> <!-- Replace with the desired version -->
<version>0.3.9</version> <!-- Replace with the desired version -->
</parent>

<groupId>org.liquibase.ext</groupId>
<artifactId>liquibase-cassandra</artifactId>
<version>4.24.1-SNAPSHOT</version>
<version>4.28.0-SNAPSHOT</version>
<name>Liquibase Extension: Cassandra Database Support</name>
<description>Adds support for Cassandra database</description>
<url>https://github.com/liquibase/liquibase-cassandra</url>

<properties>
<maven.enforcer.requireJavaVersion>${maven.compiler.source}</maven.enforcer.requireJavaVersion>
<liquibase.version>4.24.0</liquibase.version>
<liquibase.version>4.27.0</liquibase.version>
<liquibase.sdk.github.token>${env.GITHUB_TOKEN}</liquibase.sdk.github.token>
<spock-core.version>1.3-groovy-2.4</spock-core.version>
<spock-reports.version>1.3.1</spock-reports.version>
<groovy-all.version>2.4.17</groovy-all.version>
<sonar.tests>src/test/groovy</sonar.tests>
</properties>

Expand All @@ -43,29 +39,20 @@
<dependency>
<groupId>com.ing.data</groupId>
<artifactId>cassandra-jdbc-wrapper</artifactId>
<version>4.10.1</version>
<scope>system</scope>
<systemPath>${basedir}/lib/cassandra-jdbc-wrapper-4.9.1-bundle.jar</systemPath>
<version>4.12.0</version>
</dependency>
<dependency>
<groupId>com.datastax.oss</groupId>
<artifactId>java-driver-parent</artifactId>
<version>4.17.0</version>
<type>pom</type>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.2.224</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit4</artifactId>
<version>3.2.1</version>
</dependency>
</dependencies>
<configuration>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ public String getCurrentDateTimeFunction() {
public String getKeyspace() {
if (keyspace == null) {
try {
keyspace = ((CassandraConnection) (this).getConnection()).getSession().getKeyspace().toString();
if (this.getConnection() instanceof JdbcConnection) {
keyspace = ((CassandraConnection) ((JdbcConnection) this.getConnection())
.getUnderlyingConnection()).getSchema();
}
} catch (Exception e) {
Scope.getCurrentScope().getLog(CassandraDatabase.class)
.severe("Could not get keyspace from connection", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import liquibase.Scope;
import liquibase.database.Database;
import liquibase.database.ObjectQuotingStrategy;
import liquibase.database.core.MSSQLDatabase;
import liquibase.exception.DatabaseException;
import liquibase.exception.LiquibaseException;
import liquibase.exception.LockException;
Expand All @@ -12,15 +11,15 @@
import liquibase.executor.ExecutorService;
import liquibase.ext.cassandra.database.CassandraDatabase;
import liquibase.lockservice.StandardLockService;
import liquibase.sql.Sql;
import liquibase.sqlgenerator.SqlGeneratorFactory;
import liquibase.statement.core.LockDatabaseChangeLogStatement;
import liquibase.statement.core.RawSqlStatement;
import liquibase.statement.core.UnlockDatabaseChangeLogStatement;
import liquibase.util.NetUtil;

import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;

public class LockServiceCassandra extends StandardLockService {

Expand Down Expand Up @@ -62,19 +61,6 @@ public boolean acquireLock() throws LockException {
// another node was faster
return false;
}
if ((rowsUpdated == -1) && (database instanceof MSSQLDatabase)) {

Scope.getCurrentScope().getLog(this.getClass()).info("Database did not return a proper row count (Might have NOCOUNT enabled)");
database.rollback();
Sql[] sql = SqlGeneratorFactory.getInstance().generateSql(
new LockDatabaseChangeLogStatement(), database
);
if (sql.length != 1) {
throw new UnexpectedLiquibaseException("Did not expect " + sql.length + " statements");
}
rowsUpdated = executor.update(new RawSqlStatement("EXEC sp_executesql N'SET NOCOUNT OFF " +
sql[0].toSql().replace("'", "''") + "'"));
}
if (rowsUpdated > 1) {
throw new LockException("Did not update change log lock correctly");
}
Expand Down Expand Up @@ -113,7 +99,7 @@ public void releaseLock() throws LockException {

Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database);
try {
if (this.hasDatabaseChangeLogLockTable()) {
if (this.isDatabaseChangeLogLockTableCreated()) {
executor.comment("Release Database Lock");
database.rollback();
executor.update(new UnlockDatabaseChangeLogStatement());
Expand All @@ -138,15 +124,15 @@ public void releaseLock() throws LockException {


@Override
public boolean hasDatabaseChangeLogLockTable() {
public boolean isDatabaseChangeLogLockTableCreated() {
boolean hasChangeLogLockTable;
try {
Statement statement = ((CassandraDatabase) database).getStatement();
statement.executeQuery("SELECT ID from " + getChangeLogLockTableName());
statement.executeQuery("SELECT ID FROM " + getChangeLogLockTableName());
statement.close();
hasChangeLogLockTable = true;
} catch (SQLException e) {
Scope.getCurrentScope().getLog(getClass()).info("No " + getChangeLogLockTableName() + " available in cassandra.");
Scope.getCurrentScope().getLog(getClass()).info("No " + getChangeLogLockTableName() + " available in Cassandra.");
hasChangeLogLockTable = false;
} catch (DatabaseException e) {
e.printStackTrace();
Expand All @@ -158,14 +144,13 @@ public boolean hasDatabaseChangeLogLockTable() {
}

@Override
public boolean isDatabaseChangeLogLockTableInitialized(final boolean tableJustCreated) {
if (!isDatabaseChangeLogLockTableInitialized) {
public boolean isDatabaseChangeLogLockTableInitialized(final boolean tableJustCreated, final boolean forceRecheck) {
if (!isDatabaseChangeLogLockTableInitialized || forceRecheck) {
Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database);

try {
isDatabaseChangeLogLockTableInitialized = executor.queryForInt(
new RawSqlStatement("SELECT COUNT(*) FROM " + getChangeLogLockTableName())
) > 0;
isDatabaseChangeLogLockTableInitialized = executeCountQueryWithAlternative(executor,
"SELECT COUNT(*) FROM " + getChangeLogLockTableName()) > 0;
} catch (LiquibaseException e) {
if (executor.updatesDatabase()) {
throw new UnexpectedLiquibaseException(e);
Expand All @@ -179,18 +164,15 @@ public boolean isDatabaseChangeLogLockTableInitialized(final boolean tableJustCr
}

private boolean isLocked(Executor executor) throws DatabaseException {
return executor.queryForInt(
new RawSqlStatement("SELECT COUNT(*) FROM " + getChangeLogLockTableName() + " where " +
"locked = TRUE ALLOW FILTERING")
) > 0;
// Check to see if current process holds the lock each time
return isLockedByCurrentInstance(executor);
}

private boolean isLockedByCurrentInstance(Executor executor) throws DatabaseException {
final String lockedBy = NetUtil.getLocalHostName() + " (" + NetUtil.getLocalHostAddress() + ")";
return executor.queryForInt(
new RawSqlStatement("SELECT COUNT(*) FROM " + getChangeLogLockTableName() + " where " +
"LOCKED = TRUE AND LOCKEDBY = '" + lockedBy + "' ALLOW FILTERING")
) > 0;
return executeCountQueryWithAlternative(executor,
"SELECT COUNT(*) FROM " + getChangeLogLockTableName() + " WHERE " +
"LOCKED = TRUE AND LOCKEDBY = '" + lockedBy + "' ALLOW FILTERING") > 0;
}

private String getChangeLogLockTableName() {
Expand All @@ -200,4 +182,21 @@ private String getChangeLogLockTableName() {
return database.getDatabaseChangeLogLockTableName();
}
}

private int executeCountQueryWithAlternative(final Executor executor, final String query) throws DatabaseException {
if (!query.contains("SELECT COUNT(*)")) {
throw new UnexpectedLiquibaseException("Invalid count query: " + query);
}
try {
return executor.queryForInt(new RawSqlStatement(query));
} catch (DatabaseException e) {
// If the count query failed (for example, because counting rows is not implemented - see issue #289 with
// AWS Keyspaces where aggregate functions like COUNT are not supported:
// https://docs.aws.amazon.com/keyspaces/latest/devguide/cassandra-apis.html#cassandra-functions), try to
// execute the same query without the COUNT function then programmatically count returned rows.
final String altQuery = query.replace("SELECT COUNT(*)", "SELECT *");
final List<Map<String, ?>> rows = executor.queryForList(new RawSqlStatement(altQuery));
return rows.size();
}
}
}
Loading

0 comments on commit 2057fb5

Please sign in to comment.