Skip to content

Commit

Permalink
ensure order & uniqueness of migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
bossenti committed Oct 23, 2023
1 parent 20a9ddf commit cd0e41d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,13 @@
import org.apache.streampipes.extensions.api.extractor.IStaticPropertyExtractor;
import org.apache.streampipes.model.connect.adapter.AdapterDescription;

public interface AdapterMigrator extends ModelMigrator<AdapterDescription, IStaticPropertyExtractor> {
public abstract class AdapterMigrator implements ModelMigrator<AdapterDescription, IStaticPropertyExtractor> {

@Override
public boolean equals(Object obj) {
if (obj instanceof ModelMigrator<?, ?>) {
return this.config().equals(((ModelMigrator<?, ?>) obj).config());
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,13 @@
import org.apache.streampipes.extensions.api.extractor.IDataSinkParameterExtractor;
import org.apache.streampipes.model.graph.DataSinkInvocation;

public interface DataSinkMigrator extends ModelMigrator<DataSinkInvocation, IDataSinkParameterExtractor> {
public abstract class DataSinkMigrator implements ModelMigrator<DataSinkInvocation, IDataSinkParameterExtractor> {

@Override
public boolean equals(Object obj) {
if (obj instanceof ModelMigrator<?, ?>) {
return this.config().equals(((ModelMigrator<?, ?>) obj).config());
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,30 @@
import org.apache.streampipes.model.base.NamedStreamPipesEntity;
import org.apache.streampipes.model.migration.ModelMigratorConfig;

public interface ModelMigrator<T extends NamedStreamPipesEntity, ExT extends IParameterExtractor<?>> {
public interface ModelMigrator<
T extends NamedStreamPipesEntity,
ExT extends IParameterExtractor<?>
> extends Comparable<Object> {

ModelMigratorConfig config();

/**
* Defines the migration to be performed.
* @param element Entity to be transformed.
*
* @param element Entity to be transformed.
* @param extractor Extractor that allows to handle static properties.
* @return Result of the migration that describes both outcomes: successful and failed migrations
* @throws RuntimeException in case any unexpected error occurs
*/
MigrationResult<T> migrate(T element, ExT extractor) throws RuntimeException;

@Override
default int compareTo(Object o) {
if (!(o instanceof ModelMigrator<?, ?>)) {
throw new ClassCastException("Given object is not an instance of `ModelMigrator` - "
+ "only instances of `ModelMigrator` can be compared.");
} else {
return config().compareTo(((ModelMigrator<?, ?>) o).config());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.streampipes.model.extensions.configuration.ConfigItem;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

Expand Down Expand Up @@ -190,7 +191,19 @@ public void addRuntimeProvider(IStreamPipesRuntimeProvider runtimeProvider) {
return this.migrators;
}

/**
* Add a list of migrations to the service definition.
* This inherently checks for duplicates and sorts the migrations as such that
* migrations affecting lower versions always come first.
*
* @param migrators migrators to add
*/
public void addMigrators(List<ModelMigrator<?, ?>> migrators) {
this.migrators.addAll(migrators);
for (var migratorToAdd : migrators) {
if (!this.migrators.contains(migratorToAdd)) {
this.migrators.add(migratorToAdd);
}
}
Collections.sort(this.migrators);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,29 @@
* @param toVersion Target version that the migration aims to achieve
*/
public record ModelMigratorConfig(String targetAppId, SpServiceTagPrefix modelType,
Integer fromVersion, Integer toVersion) {
int fromVersion, int toVersion) implements Comparable<Object>{


@Override
public int compareTo(Object o) {

if (o == null) {
throw new NullPointerException();
}

if (!(o instanceof ModelMigratorConfig)){
throw new ClassCastException("Given object is not an instance of `ModelMigratorConfig` - "
+ "only instances of `ModelMigratorConfig` can be compared.");
}

if (targetAppId.equals(((ModelMigratorConfig) o).targetAppId())) {

if (fromVersion != ((ModelMigratorConfig) o).fromVersion()) {
return this.toVersion() - ((ModelMigratorConfig) o).toVersion();
}
return this.fromVersion() - ((ModelMigratorConfig) o).fromVersion();
}

return 0;
}
}

0 comments on commit cd0e41d

Please sign in to comment.