-
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.
Merge pull request #88 from himanshu9891/main
Fix: Liquibase TAG command execution failure issue with Cassandra
- Loading branch information
Showing
2 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
src/main/java/liquibase/ext/cassandra/sqlgenerator/TagDatabaseGeneratorCassandra.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,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); | ||
} | ||
} | ||
} |
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