Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LB-2095: Custom change log and -lock tables with keyspace issue #106

Merged
merged 2 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public boolean hasDatabaseChangeLogTable() {
boolean hasChangeLogTable;
try {
Statement statement = ((CassandraDatabase) getDatabase()).getStatement();
statement.executeQuery("select ID from " + getDatabase().getDefaultCatalogName() + ".DATABASECHANGELOG");
statement.executeQuery("select ID from " + getChangeLogTableName());
statement.close();
hasChangeLogTable = true;
} catch (SQLException e) {
Expand All @@ -54,8 +54,7 @@ public int getNextSequenceValue() {
int next = 0;
try {
Statement statement = ((CassandraDatabase) getDatabase()).getStatement();
ResultSet rs = statement.executeQuery("SELECT ID, AUTHOR, ORDEREXECUTED FROM " +
getDatabase().getDefaultCatalogName() + ".DATABASECHANGELOG");
ResultSet rs = statement.executeQuery("SELECT ID, AUTHOR, ORDEREXECUTED FROM " + getChangeLogTableName());
while (rs.next()) {
int order = rs.getInt("ORDEREXECUTED");
next = Math.max(order, next);
Expand All @@ -70,11 +69,17 @@ public int getNextSequenceValue() {

@Override
public List<Map<String, ?>> queryDatabaseChangeLogTable(Database database) throws DatabaseException {
RawSqlStatement select = new RawSqlStatement("SELECT * FROM " + database.getDefaultCatalogName() +
".DATABASECHANGELOG");
RawSqlStatement select = new RawSqlStatement("SELECT * FROM " + getChangeLogTableName());
final List<Map<String, ?>> returnList = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database).queryForList(select);
returnList.sort(Comparator.comparing((Map<String, ?> o) -> (Date) o.get("DATEEXECUTED")).thenComparingInt(o -> (Integer) o.get("ORDEREXECUTED")));
return returnList;
}

private String getChangeLogTableName() {
if(getDatabase().getDefaultCatalogName() != null) {
return getDatabase().getDefaultCatalogName() + "." + getDatabase().getDatabaseChangeLogTableName();
} else {
return getDatabase().getDatabaseChangeLogTableName();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public boolean acquireLock() throws LockException {


boolean locked = executor.queryForInt(
new RawSqlStatement("SELECT COUNT(*) FROM " + database.getDefaultCatalogName() + ".DATABASECHANGELOGLOCK where " +
new RawSqlStatement("SELECT COUNT(*) FROM " + getChangeLogTableName() + " where " +
"locked = TRUE ALLOW FILTERING")
) > 0;

Expand Down Expand Up @@ -143,7 +143,7 @@ public boolean hasDatabaseChangeLogLockTable() {
boolean hasChangeLogLockTable;
try {
Statement statement = ((CassandraDatabase) database).getStatement();
statement.executeQuery("SELECT ID from " + database.getDefaultCatalogName() + ".DATABASECHANGELOGLOCK");
statement.executeQuery("SELECT ID from " + getChangeLogTableName());
statement.close();
hasChangeLogLockTable = true;
} catch (SQLException e) {
Expand All @@ -165,7 +165,7 @@ public boolean isDatabaseChangeLogLockTableInitialized(final boolean tableJustCr

try {
isDatabaseChangeLogLockTableInitialized = executor.queryForInt(
new RawSqlStatement("SELECT COUNT(*) FROM " + database.getDefaultCatalogName() + ".DATABASECHANGELOGLOCK")
new RawSqlStatement("SELECT COUNT(*) FROM " + getChangeLogTableName())
) > 0;
} catch (LiquibaseException e) {
if (executor.updatesDatabase()) {
Expand All @@ -179,4 +179,12 @@ public boolean isDatabaseChangeLogLockTableInitialized(final boolean tableJustCr
return isDatabaseChangeLogLockTableInitialized;
}

private String getChangeLogTableName() {
nvoxland marked this conversation as resolved.
Show resolved Hide resolved
if(database.getDefaultCatalogName() != null) {
return database.getDefaultCatalogName() + "." + database.getDatabaseChangeLogTableName();
} else {
return database.getDatabaseChangeLogTableName();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public boolean supports(CreateDatabaseChangeLogLockTableStatement statement, Dat
public Sql[] generateSql(CreateDatabaseChangeLogLockTableStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {

RawSqlStatement createTableStatement = new RawSqlStatement("CREATE TABLE IF NOT EXISTS " +
database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), "databasechangeloglock") +
database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogLockTableName()) +
" (ID INT, LOCKED BOOLEAN, LOCKGRANTED timestamp, LOCKEDBY TEXT, PRIMARY KEY (ID))");

return SqlGeneratorFactory.getInstance().generateSql(createTableStatement, database);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Sql[] generateSql(CreateDatabaseChangeLogTableStatement statement, Databa


RawSqlStatement createTableStatement = new RawSqlStatement("CREATE TABLE IF NOT EXISTS " +
database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), "databasechangelog") +
database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()) +
"( ID TEXT, AUTHOR TEXT, FILENAME TEXT, DATEEXECUTED timestamp, ORDEREXECUTED INT, EXECTYPE TEXT, MD5SUM TEXT, DESCRIPTION TEXT, COMMENTS TEXT, TAG TEXT, LIQUIBASE TEXT, CONTEXTS TEXT, LABELS TEXT, DEPLOYMENT_ID TEXT,PRIMARY KEY (ID, AUTHOR, FILENAME))");

return SqlGeneratorFactory.getInstance().generateSql(createTableStatement, database);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public Sql[] generateSql(InitializeDatabaseChangeLogLockTableStatement statement
RawSqlStatement deleteStatement = new RawSqlStatement("TRUNCATE TABLE " + database.escapeTableName(
database.getLiquibaseCatalogName(),
database.getLiquibaseSchemaName(),
"databasechangeloglock".toUpperCase()));
database.getDatabaseChangeLogLockTableName().toUpperCase()));

return SqlGeneratorFactory.getInstance().generateSql(deleteStatement, database);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public boolean supports(LockDatabaseChangeLogStatement statement, Database datab
@Override
public Sql[] generateSql(LockDatabaseChangeLogStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
RawSqlStatement updateStatement = new RawSqlStatement("UPDATE " +
database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), "databasechangeloglock") +
database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogLockTableName()) +
" SET LOCKED = TRUE, LOCKEDBY = '" + hostname + " (" + hostaddress + ")" + "', LOCKGRANTED = " + System.currentTimeMillis() + " WHERE ID = 1");
return SqlGeneratorFactory.getInstance().generateSql(updateStatement, database);
}
Expand Down