Skip to content

Commit

Permalink
Add the baseline feature
Browse files Browse the repository at this point in the history
This allows skipping some init migrations for existing environments
  • Loading branch information
François LAROCHE committed Oct 24, 2022
1 parent 94d7bd5 commit 5bd51ec
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 27 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ Elasticsearch-Evolution can be configured to your needs:
- **historyIndex** (default=es_evolution): Name of the history index that will be used by Elasticsearch-Evolution. In this index Elasticsearch-Evolution will persist his internal state and tracks which migration script has already been executed.
- **historyMaxQuerySize** (default=1000): The maximum query size while validating already executed scripts. This query size have to be higher than the total count of your migration scripts.
- **validateOnMigrate** (default=true): Whether to fail when a previously applied migration script has been modified after it was applied.
- **baselineVersion** (default=1.0): Version to use as a baseline. versions lower than it will not be applied.

### 5.1 Spring Boot

Expand Down Expand Up @@ -297,7 +298,7 @@ ElasticsearchEvolution.configure()
- added spring boot 2.7 compatibility tests
- added Elasticsearch 8.4, 8.3, and 8,2 compatibility test
- added Opensearch 2.3, 2.2, 2.1 and 2.0 compatibility tests

- It is now possible to set a `baselineVersion` to skip migrations with versions lower than the defined `baselineVersion` ([#164](https://github.com/senacor/elasticsearch-evolution/issues/164))

### v0.4.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ protected MigrationService createMigrationService() {
getRestClient(),
ContentType.parse(getConfig().getDefaultContentType()),
getConfig().getEncoding(),
getConfig().getValidateOnMigrate());
getConfig().getValidateOnMigrate(),
getConfig().getBaselineVersion());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.senacor.elasticsearch.evolution.core.api.config;

import com.senacor.elasticsearch.evolution.core.ElasticsearchEvolution;
import com.senacor.elasticsearch.evolution.core.internal.model.MigrationVersion;
import org.elasticsearch.client.RestClient;
import org.springframework.boot.context.properties.ConfigurationProperties;

Expand Down Expand Up @@ -88,6 +89,12 @@ public class ElasticsearchEvolutionConfig {
*/
private boolean validateOnMigrate = true;

/**
* version to use as a baseline.
* The baseline version will be the first one applied, the versions below will be ignored.
*/
private String baselineVersion = "1.0";

/**
* Loads this configuration into a new ElasticsearchEvolution instance.
*
Expand Down Expand Up @@ -129,6 +136,11 @@ public ElasticsearchEvolutionConfig validate() throws IllegalStateException, Nul
}
requireNotBlank(historyIndex, "historyIndex must not be empty");
requireCondition(historyMaxQuerySize, size -> size > 0, "historyMaxQuerySize value '%s' must be greater than 0", historyMaxQuerySize);
try {
MigrationVersion.fromVersion(baselineVersion);
} catch (RuntimeException e) {
throw new IllegalArgumentException("baselineVersion is invalid", e);
}
}
return this;
}
Expand Down Expand Up @@ -250,6 +262,15 @@ public ElasticsearchEvolutionConfig setValidateOnMigrate(boolean validateOnMigra
return this;
}

public String getBaselineVersion() {
return baselineVersion;
}

public ElasticsearchEvolutionConfig setBaselineVersion(String baselineVersion) {
this.baselineVersion = baselineVersion;
return this;
}

@Override
public String toString() {
return "ElasticsearchEvolutionConfig{" +
Expand All @@ -266,6 +287,7 @@ public String toString() {
", historyIndex='" + historyIndex + '\'' +
", historyMaxQuerySize=" + historyMaxQuerySize +
", validateOnMigrate=" + validateOnMigrate +
", baselineVersion='" + baselineVersion + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ public class MigrationServiceImpl implements MigrationService {
private final Charset encoding;
private final boolean validateOnMigrate;

private final String baselineVersion;
public MigrationServiceImpl(HistoryRepository historyRepository,
int waitUntilUnlockedMinTimeInMillis,
int waitUntilUnlockedMaxTimeInMillis,
RestClient restClient,
ContentType defaultContentType,
Charset encoding,
boolean validateOnMigrate) {
boolean validateOnMigrate,
String baselineVersion) {
this.historyRepository = requireNonNull(historyRepository, "historyRepository must not be null");
this.restClient = requireNonNull(restClient, "restClient must not be null");
this.defaultContentType = requireNonNull(defaultContentType);
Expand All @@ -56,6 +58,7 @@ public MigrationServiceImpl(HistoryRepository historyRepository,
"waitUntilUnlockedMinTimeInMillis (%s) must not be negative and must not be greater than waitUntilUnlockedMaxTimeInMillis (%s)",
waitUntilUnlockedMinTimeInMillis, waitUntilUnlockedMaxTimeInMillis);
this.waitUntilUnlockedMaxTimeInMillis = waitUntilUnlockedMaxTimeInMillis;
this.baselineVersion = baselineVersion;
}

@Override
Expand Down Expand Up @@ -166,6 +169,7 @@ ExecutionResult executeScript(ParsedMigrationScript scriptToExecute) {
List<ParsedMigrationScript> getPendingScriptsToBeExecuted(Collection<ParsedMigrationScript> migrationScripts) {
// order migrationScripts by version
List<ParsedMigrationScript> orderedScripts = new ArrayList<>(migrationScripts.stream()
.filter(script -> script.getFileNameInfo().getVersion().isAtLeast(baselineVersion))
.collect(Collectors.toMap(
script -> script.getFileNameInfo().getVersion(),
script -> script,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void OK_indexDocumentIsWrittenToElasticsearch(String versionInfo, EsUtils esUtil

MigrationServiceImpl underTest = new MigrationServiceImpl(historyRepositoryMock,
0, 0, restHighLevelClient.getLowLevelClient(),
defaultContentType, encoding, true);
defaultContentType, encoding, true, "1.0");

MigrationScriptProtocol res = underTest.executeScript(script).getProtocol();

Expand Down
Loading

0 comments on commit 5bd51ec

Please sign in to comment.