Skip to content

Commit

Permalink
implement clearAllCheckSums
Browse files Browse the repository at this point in the history
this allows executing the [`clear-checksums`] liquibase action.

[`clear-checksums`]: https://docs.liquibase.com/commands/utility/clear-checksums.html
  • Loading branch information
rursprung authored and filipelautert committed Sep 11, 2024
1 parent 65ea0e5 commit cb6b42f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,14 @@ protected void removeRanChangeSet(final ChangeSet changeSet) throws DatabaseExce

@Override
public void clearAllCheckSums() throws DatabaseException {
throw new UnsupportedOperationException();
try {
this.getOpenSearchClient()
.updateByQuery(r -> r.index(this.getDatabaseChangeLogTableName())
.script(s -> s.inline(i -> i.source("ctx._source.lastCheckSum = null")
.lang("painless"))));
} catch (IOException e) {
throw new DatabaseException(e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import liquibase.command.CommandScope;
import liquibase.command.core.UpdateCommandStep;
import liquibase.command.core.helpers.DbUrlConnectionArgumentsCommandStep;
import liquibase.command.core.helpers.DbUrlConnectionCommandStep;
import liquibase.database.DatabaseFactory;
import liquibase.ext.opensearch.database.OpenSearchConnection;
Expand All @@ -10,6 +11,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInstance;
import org.opensearch.client.opensearch.OpenSearchClient;
import org.opensearch.client.opensearch._types.query_dsl.Query;
import org.opensearch.client.opensearch.core.CountRequest;
import org.opensearch.client.opensearch.indices.ExistsRequest;
import org.opensearch.testcontainers.OpensearchContainer;
Expand Down Expand Up @@ -50,7 +52,7 @@ protected OpenSearchClient getOpenSearchClient() {

protected void doLiquibaseUpdate(final String changeLogFile, final String contexts) throws Exception {
new CommandScope(UpdateCommandStep.COMMAND_NAME)
.addArgumentValue(DbUrlConnectionCommandStep.DATABASE_ARG, this.database)
.addArgumentValue(DbUrlConnectionArgumentsCommandStep.DATABASE_ARG, this.database)
.addArgumentValue(UpdateCommandStep.CHANGELOG_FILE_ARG, changeLogFile)
.addArgumentValue(UpdateCommandStep.CONTEXTS_ARG, contexts)
.execute();
Expand All @@ -67,11 +69,16 @@ protected boolean indexExists(final String indexName) throws Exception {
return this.getOpenSearchClient().indices().exists(request).value();
}

protected long getDocumentCount(final String indexName) throws Exception {
protected long getDocumentCount(final String indexName, final Query query) throws Exception {
final var request = new CountRequest.Builder()
.index(indexName)
.query(query)
.build();
return this.getOpenSearchClient().count(request).count();
}

protected long getDocumentCount(final String indexName) throws Exception {
return this.getDocumentCount(indexName, null);
}

}
23 changes: 23 additions & 0 deletions src/test/java/liquibase/ext/opensearch/OpenSearchLiquibaseIT.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package liquibase.ext.opensearch;

import liquibase.command.CommandScope;
import liquibase.command.core.ClearChecksumsCommandStep;
import liquibase.command.core.UpdateCommandStep;
import liquibase.command.core.helpers.DbUrlConnectionArgumentsCommandStep;
import liquibase.command.core.helpers.DbUrlConnectionCommandStep;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
import org.opensearch.client.opensearch._types.query_dsl.Query;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -52,4 +58,21 @@ public void itRespectsTheContextFilter() {
assertThat(this.indexExists("testindex2")).isFalse();
}

@SneakyThrows
@Test
public void itCanClearAllChecksums() {
// run at least one change set
this.doLiquibaseUpdate("liquibase/ext/changelog.httprequest.yaml");

final var countBeforeClear = this.getDocumentCount("databasechangelog", new Query.Builder().exists(e -> e.field("lastCheckSum")).build());
assertThat(countBeforeClear).isNotZero();

new CommandScope(ClearChecksumsCommandStep.COMMAND_NAME)
.addArgumentValue(DbUrlConnectionArgumentsCommandStep.DATABASE_ARG, this.database)
.execute();

final var countAfterClear = this.getDocumentCount("databasechangelog", new Query.Builder().exists(e -> e.field("lastCheckSum")).build());
assertThat(countAfterClear).isZero();
}

}

0 comments on commit cb6b42f

Please sign in to comment.