Skip to content

Commit

Permalink
Merge pull request #88 from himanshu9891/main
Browse files Browse the repository at this point in the history
Fix: Liquibase TAG command execution failure issue with Cassandra
  • Loading branch information
molivasdat authored Mar 30, 2022
2 parents acb4e99 + 5184f8d commit bad0a28
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package liquibase.ext.cassandra.sqlgenerator;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.Calendar;
import java.util.TimeZone;

import liquibase.Scope;
import liquibase.database.Database;
import liquibase.database.ObjectQuotingStrategy;
import liquibase.datatype.DataTypeFactory;
import liquibase.ext.cassandra.database.CassandraDatabase;
import liquibase.sql.Sql;
import liquibase.sql.UnparsedSql;
import liquibase.sqlgenerator.SqlGeneratorChain;
import liquibase.sqlgenerator.core.TagDatabaseGenerator;
import liquibase.statement.core.TagDatabaseStatement;
import liquibase.structure.core.Column;

public class TagDatabaseGeneratorCassandra extends TagDatabaseGenerator {

@Override
public int getPriority() {
return PRIORITY_DATABASE;
}

@Override
public boolean supports(TagDatabaseStatement statement, Database database) {
return database instanceof CassandraDatabase;
}

@Override
public Sql[] generateSql(TagDatabaseStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
ObjectQuotingStrategy currentStrategy = database.getObjectQuotingStrategy();
database.setObjectQuotingStrategy(ObjectQuotingStrategy.LEGACY);

try {
String tagEscaped = DataTypeFactory.getInstance().fromObject(statement.getTag(), database).objectToSql(statement.getTag(), database);


Statement statement1 = ((CassandraDatabase) database).getStatement();
//Query to get last executed changeset date
String query1 = "SELECT TOUNIXTIMESTAMP(MAX(DATEEXECUTED)) as DATEEXECUTED FROM " +
database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), "databasechangelog");
ResultSet rs1 = statement1.executeQuery(query1);
String date = "";
while (rs1.next()) {
date = rs1.getString("DATEEXECUTED");
}
rs1.close();
//Query to get composite key details of last executed change set
String query2 = "select id,author, filename from " +
database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), "databasechangelog")
+ " where dateexecuted = '"+date+"' ALLOW FILTERING";
ResultSet rs2 = statement1.executeQuery(query2);
String id = "", author = "", filename = "";
while (rs2.next()) {
id = rs2.getString("id");
author = rs2.getString("author");
filename = rs2.getString("filename");
}
rs2.close();
statement1.close();
//Query to update tag
String updateQuery = "UPDATE "
+ database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), "databasechangelog")
+ " SET TAG = "+tagEscaped
+ " WHERE id = '"+ id +"' and author = '"+ author +"' and filename = '"+ filename+ "'";

return new Sql[]{
new UnparsedSql(updateQuery)
};



} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return super.generateSql(statement, database, sqlGeneratorChain);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return super.generateSql(statement, database, sqlGeneratorChain);
} finally {
database.setObjectQuotingStrategy(currentStrategy);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ liquibase.ext.cassandra.sqlgenerator.DeleteGeneratorCassandra
liquibase.ext.cassandra.sqlgenerator.InitializeDatabaseChangeLogLockTableGeneratorCassandra
liquibase.ext.cassandra.sqlgenerator.CreateTableGeneratorCassandra
liquibase.ext.cassandra.sqlgenerator.DropColumnGeneratorCassandra
liquibase.ext.cassandra.sqlgenerator.RenameColumnGeneratorCassandra
liquibase.ext.cassandra.sqlgenerator.RenameColumnGeneratorCassandra
liquibase.ext.cassandra.sqlgenerator.TagDatabaseGeneratorCassandra

0 comments on commit bad0a28

Please sign in to comment.