Skip to content

Commit

Permalink
[3424] Fix document migration data during creation
Browse files Browse the repository at this point in the history
Bug: #3424
Signed-off-by: Michaël Charfadi <michael.charfadi@obeosoft.com>
  • Loading branch information
mcharfadi authored and sbegaudeau committed May 6, 2024
1 parent 3717bb4 commit c9c69ec
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Accordingly, `IObjectSearchService` and `IIdentityService` now handle actual `IE
- https://github.com/eclipse-sirius/sirius-web/issues/3420[#3420] [diagram] Fix an issue that prevents position reset after a rejected drag&drop.
- https://github.com/eclipse-sirius/sirius-web/issues/3422[#3422] [diagram] Fix an issue that prevents element not present on diagram to be selected on first click.
- https://github.com/eclipse-sirius/sirius-web/issues/3407[#3407] [diagram] Fix performance issue during node drag.
- https://github.com/eclipse-sirius/sirius-web/issues/3424[#3424] [emf] Fix wrong document migration data when creating a document.

=== New Features

Expand Down
8 changes: 6 additions & 2 deletions doc/adrs/144_add_support_for_emf_models_migration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ One important thing to consider is that BasicExtendedMetaData methods assume tha

The version of the last migration used on a model will be stored at the root of the JSON document on EMFJson side.

The version of a migration participant will represent when it was added to the project and will respect this structure : `ITERATION-YYYYMMDDHHMM`

For example `2024.5.0-202404221400` for a participant added on 2024-04-22 at 14h00 for the iteration 2024.5.0.

[source,java]
----
public class MigrationExtendedMetaData extends BasicExtendedMetaData {
Expand Down Expand Up @@ -65,15 +69,15 @@ public interface IMigrationParticipant {
----


This is an example of implementation where this NameFeatureChangelMigrationParticipant will be used if the model version is superior to `202404221458`.
This is an example of implementation where this NameFeatureChangelMigrationParticipant will be used if the model version is superior to `2024.5.0-202404221400`.
If the feature name was changed from "name2" to something else, we provide the new correct value.

[source,java]
----
@Service
public class NameFeatureChangelMigrationParticipant implements IMigrationParticipant {
private static final String PARTICIPANT_VERSION = "202404221458";
private static final String PARTICIPANT_VERSION = "2024.5.0-202404221400";
@Override
public String getVersion() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*******************************************************************************/
package org.eclipse.sirius.components.emf.migration;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
Expand All @@ -23,9 +24,9 @@
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.BasicExtendedMetaData;
import org.eclipse.sirius.components.emf.ResourceMetadataAdapter;
import org.eclipse.sirius.components.emf.migration.api.IMigrationParticipant;
import org.eclipse.sirius.components.emf.migration.api.MigrationData;
import org.eclipse.sirius.components.emf.ResourceMetadataAdapter;
import org.eclipse.sirius.emfjson.resource.JsonResource;

import com.google.gson.Gson;
Expand All @@ -50,6 +51,17 @@ private boolean isCandidateVersion(IMigrationParticipant migrationParticipant) {
return migrationParticipant.getVersion().compareTo(this.documentMigrationData.migrationVersion()) > 0;
}

public MigrationData getMostRecentParticipantMigrationData() {
var migrationParticipantsCandidate = migrationParticipants.stream()
.filter(this::isCandidateVersion)
.sorted(Comparator.comparing(IMigrationParticipant::getVersion))
.sorted(Collections.reverseOrder())
.map(migrationParticipant -> new MigrationData("none", migrationParticipant.getVersion()))
.findFirst();

return migrationParticipantsCandidate.orElse(this.documentMigrationData);
}

@Override
public EStructuralFeature getElement(EClass eClass, String namespace, String eStructuralFeatureName) {
EStructuralFeature structuralFeature = eClass.getEStructuralFeature(eStructuralFeatureName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
@Service
public class NodeDescriptionLabelExpressionMigrationParticipant implements IMigrationParticipant {

private static final String PARTICIPANT_VERSION = "2024.4.0";
private static final String PARTICIPANT_VERSION = "2024.5.0-202404221400";

@Override
public String getVersion() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*******************************************************************************/
package org.eclipse.sirius.web.application.studio.services;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

import org.eclipse.emf.ecore.resource.Resource;
Expand All @@ -22,6 +24,8 @@
import org.eclipse.sirius.components.domain.Entity;
import org.eclipse.sirius.components.domain.Relation;
import org.eclipse.sirius.components.emf.ResourceMetadataAdapter;
import org.eclipse.sirius.components.emf.migration.MigrationService;
import org.eclipse.sirius.components.emf.migration.api.IMigrationParticipant;
import org.eclipse.sirius.components.emf.services.JSONResourceFactory;
import org.eclipse.sirius.emfjson.resource.JsonResource;
import org.eclipse.sirius.web.application.studio.services.api.IDefaultDomainResourceProvider;
Expand All @@ -37,6 +41,12 @@ public class DefaultDomainResourceProvider implements IDefaultDomainResourceProv

private static final String DOMAIN_DOCUMENT_NAME = "DomainNewModel";

private final List<IMigrationParticipant> migrationParticipants;

public DefaultDomainResourceProvider(List<IMigrationParticipant> migrationParticipants) {
this.migrationParticipants = Objects.requireNonNull(migrationParticipants);
}

@Override
public Resource getResource(String domainName) {
Domain domain = DomainFactory.eINSTANCE.createDomain();
Expand Down Expand Up @@ -84,7 +94,11 @@ public Resource getResource(String domainName) {
this.addAttribute(entity2, "name", DataType.STRING);

JsonResource resource = new JSONResourceFactory().createResourceFromPath(UUID.randomUUID().toString());
resource.eAdapters().add(new ResourceMetadataAdapter(DOMAIN_DOCUMENT_NAME));
var resourceMetadataAdapter = new ResourceMetadataAdapter(DOMAIN_DOCUMENT_NAME);
var migrationService = new MigrationService(this.migrationParticipants);

resourceMetadataAdapter.setMigrationData(migrationService.getMostRecentParticipantMigrationData());
resource.eAdapters().add(resourceMetadataAdapter);
resource.getContents().add(domain);

return resource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
package org.eclipse.sirius.web.application.studio.services;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.sirius.components.emf.ResourceMetadataAdapter;
import org.eclipse.sirius.components.emf.migration.MigrationService;
import org.eclipse.sirius.components.emf.migration.api.IMigrationParticipant;
import org.eclipse.sirius.components.emf.services.JSONResourceFactory;
import org.eclipse.sirius.components.view.ChangeContext;
import org.eclipse.sirius.components.view.ColorPalette;
Expand Down Expand Up @@ -51,6 +55,12 @@ public class DefaultViewResourceProvider implements IDefaultViewResourceProvider

private static final String VIEW_DOCUMENT_NAME = "ViewNewModel";

private final List<IMigrationParticipant> migrationParticipants;

public DefaultViewResourceProvider(List<IMigrationParticipant> migrationParticipants) {
this.migrationParticipants = Objects.requireNonNull(migrationParticipants);
}

@Override
@SuppressWarnings("checkstyle:MultipleStringLiterals")
public Resource getResource(String domainName) {
Expand Down Expand Up @@ -118,8 +128,12 @@ public Resource getResource(String domainName) {
linkedToEdge.setStyle(edgeStyle);

JsonResource resource = new JSONResourceFactory().createResourceFromPath(UUID.randomUUID().toString());
resource.eAdapters().add(new ResourceMetadataAdapter(VIEW_DOCUMENT_NAME));
var resourceMetadataAdapter = new ResourceMetadataAdapter(VIEW_DOCUMENT_NAME);
var migrationService = new MigrationService(this.migrationParticipants);

resourceMetadataAdapter.setMigrationData(migrationService.getMostRecentParticipantMigrationData());
resource.getContents().add(view);
resource.eAdapters().add(resourceMetadataAdapter);

return resource;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.eclipse.sirius.components.domain.Domain;
import org.eclipse.sirius.components.domain.DomainFactory;
import org.eclipse.sirius.components.emf.ResourceMetadataAdapter;
import org.eclipse.sirius.components.emf.migration.MigrationService;
import org.eclipse.sirius.components.emf.migration.api.IMigrationParticipant;
import org.eclipse.sirius.components.emf.services.JSONResourceFactory;
import org.eclipse.sirius.components.emf.services.api.IEMFEditingContext;
import org.eclipse.sirius.components.view.View;
Expand All @@ -43,8 +45,11 @@ public class StudioStereotypeHandler implements IStereotypeHandler {

private final IDomainNameProvider domainNameProvider;

public StudioStereotypeHandler(IDomainNameProvider domainNameProvider) {
private final List<IMigrationParticipant> migrationParticipants;

public StudioStereotypeHandler(IDomainNameProvider domainNameProvider, List<IMigrationParticipant> migrationParticipants) {
this.domainNameProvider = Objects.requireNonNull(domainNameProvider);
this.migrationParticipants = Objects.requireNonNull(migrationParticipants);
}

@Override
Expand All @@ -70,8 +75,13 @@ public Optional<DocumentDTO> handle(IEditingContext editingContext, String stere
private Optional<DocumentDTO> createDomainDocument(IEMFEditingContext editingContext, String name) {
var documentId = UUID.randomUUID();
var resource = new JSONResourceFactory().createResourceFromPath(documentId.toString());
resource.eAdapters().add(new ResourceMetadataAdapter(name));

var resourceMetadataAdapter = new ResourceMetadataAdapter(name);
var migrationService = new MigrationService(this.migrationParticipants);

resourceMetadataAdapter.setMigrationData(migrationService.getMostRecentParticipantMigrationData());

resource.eAdapters().add(resourceMetadataAdapter);
editingContext.getDomain().getResourceSet().getResources().add(resource);

Domain domain = DomainFactory.eINSTANCE.createDomain();
Expand All @@ -85,8 +95,12 @@ private Optional<DocumentDTO> createDomainDocument(IEMFEditingContext editingCon
private Optional<DocumentDTO> createViewDocument(IEMFEditingContext editingContext, String name) {
var documentId = UUID.randomUUID();
var resource = new JSONResourceFactory().createResourceFromPath(documentId.toString());
resource.eAdapters().add(new ResourceMetadataAdapter(name));
var resourceMetadataAdapter = new ResourceMetadataAdapter(name);
var migrationService = new MigrationService(this.migrationParticipants);

resourceMetadataAdapter.setMigrationData(migrationService.getMostRecentParticipantMigrationData());

resource.eAdapters().add(resourceMetadataAdapter);
editingContext.getDomain().getResourceSet().getResources().add(resource);

View view = ViewFactory.eINSTANCE.createView();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Function;
Expand All @@ -22,6 +25,9 @@
import org.eclipse.sirius.components.core.api.IEditingContext;
import org.eclipse.sirius.components.core.api.IEditingContextSearchService;
import org.eclipse.sirius.components.emf.ResourceMetadataAdapter;
import org.eclipse.sirius.components.emf.migration.MigrationService;
import org.eclipse.sirius.components.emf.migration.api.IMigrationParticipant;
import org.eclipse.sirius.components.emf.migration.api.MigrationData;
import org.eclipse.sirius.components.graphql.api.UploadFile;
import org.eclipse.sirius.components.graphql.tests.ExecuteEditingContextFunctionInput;
import org.eclipse.sirius.components.graphql.tests.ExecuteEditingContextFunctionRunner;
Expand Down Expand Up @@ -71,6 +77,9 @@ public class MigrationParticipantTests extends AbstractIntegrationTests {
@Autowired
private UploadDocumentMutationRunner uploadDocumentMutationRunner;

@Autowired
private List<IMigrationParticipant> migrationParticipants;

@Test
@DisplayName("Given a project with an old model, NodeDescriptionLabelExpressionMigrationParticipant migrates the model correctly")
@Sql(scripts = {"/scripts/migration.sql"}, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
Expand Down Expand Up @@ -171,6 +180,16 @@ private void uploadDocument(String editingContextId, String name, String content
.map(Boolean.class::cast)
.orElse(false);

var migrationService = new MigrationService(this.migrationParticipants);
var optionalLastMigrationData = this.migrationParticipants.stream()
.sorted(Comparator.comparing(IMigrationParticipant::getVersion))
.sorted(Collections.reverseOrder())
.map(migrationParticipant -> new MigrationData(migrationParticipant.getClass().getSimpleName(), migrationParticipant.getVersion()))
.findFirst();

assertThat(optionalLastMigrationData).isPresent();
var lastMigrationData = optionalLastMigrationData.get();

Function<IEditingContext, Object> function = editingContext -> Optional.of(editingContext)
.filter(EditingContext.class::isInstance)
.map(EditingContext.class::cast)
Expand All @@ -179,8 +198,8 @@ private void uploadDocument(String editingContextId, String name, String content
.filter(adapter -> adapter instanceof ResourceMetadataAdapter)
.map(ResourceMetadataAdapter.class::cast)
.filter(resourceMetadataAdapter -> resourceMetadataAdapter.getMigrationData() != null)
.anyMatch(resourceMetadataAdapter -> resourceMetadataAdapter.getMigrationData().migrationVersion().equals("2024.4.0")
&& resourceMetadataAdapter.getMigrationData().lastMigrationPerformed().equals("NodeDescriptionLabelExpressionMigrationParticipant"))
.anyMatch(resourceMetadataAdapter -> resourceMetadataAdapter.getMigrationData().migrationVersion().equals(lastMigrationData.migrationVersion())
&& resourceMetadataAdapter.getMigrationData().lastMigrationPerformed().equals(lastMigrationData.lastMigrationPerformed()))
))
.orElse(false);

Expand Down

0 comments on commit c9c69ec

Please sign in to comment.