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

fix(import): Fix an issue where migrating from v2 to v3 failed to create the "latest" branch #5674

Merged
merged 4 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -2,6 +2,9 @@

import io.apicurio.registry.content.ContentHandle;
import io.apicurio.registry.content.TypedContent;
import io.apicurio.registry.model.BranchId;
import io.apicurio.registry.model.GA;
import io.apicurio.registry.model.VersionId;
import io.apicurio.registry.storage.RegistryStorage;
import io.apicurio.registry.storage.dto.ArtifactReferenceDto;
import io.apicurio.registry.storage.dto.ArtifactVersionMetaDataDto;
Expand All @@ -23,6 +26,7 @@
import io.apicurio.registry.utils.impexp.v2.GlobalRuleEntity;
import io.apicurio.registry.utils.impexp.v2.GroupEntity;
import io.apicurio.registry.utils.impexp.v3.ArtifactEntity;
import io.apicurio.registry.utils.impexp.v3.BranchEntity;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;

Expand Down Expand Up @@ -124,10 +128,17 @@ public void importArtifactVersion(ArtifactVersionEntity entity) {
.modifiedBy(entity.createdBy).modifiedOn(entity.createdOn).name(entity.name)
.owner(entity.createdBy).build();
storage.importArtifact(artifactEntity);

// Also create the "latest" branch.
BranchEntity branchEntity = BranchEntity.builder().groupId(entity.groupId)
.artifactId(entity.artifactId).branchId("latest").createdOn(entity.createdOn)
.owner(entity.createdBy).modifiedOn(entity.createdOn).modifiedBy(entity.createdBy)
.build();
storage.importBranch(branchEntity);
}

// If this version is the latest, update the artifact metadata with its metadata
if (entity.isLatest) {
// If this version is the latest, update the artifact metadata with its metadata
EditableArtifactMetaDataDto editableArtifactMetaDataDto = EditableArtifactMetaDataDto
.builder().name(newEntity.name).owner(newEntity.owner)
.description(newEntity.description).labels(newEntity.labels).build();
Expand All @@ -139,6 +150,14 @@ public void importArtifactVersion(ArtifactVersionEntity entity) {
storage.importArtifactVersion(newEntity);
log.debug("Artifact version imported successfully: {}", entity);
globalIdMapping.put(oldGlobalId, entity.globalId);

// Append this version to the "latest" branch
String entityVersion = entity.version;
if (entityVersion == null) {
entityVersion = String.valueOf(entity.versionId);
}
storage.appendVersionToBranch(new GA(entity.groupId, entity.artifactId), new BranchId("latest"),
new VersionId(entityVersion));
} catch (VersionAlreadyExistsException ex) {
if (ex.getGlobalId() != null) {
log.warn("Duplicate globalId {} detected, skipping import of artifact version: {}",
Expand Down
46 changes: 46 additions & 0 deletions app/src/test/java/io/apicurio/registry/DataUpgradeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import io.apicurio.registry.model.GroupId;
import io.apicurio.registry.rest.client.models.ArtifactReference;
import io.apicurio.registry.rest.client.models.ArtifactSearchResults;
import io.apicurio.registry.rest.client.models.BranchMetaData;
import io.apicurio.registry.rest.client.models.HandleReferencesType;
import io.apicurio.registry.rest.client.models.ProblemDetails;
import io.apicurio.registry.rest.client.models.VersionMetaData;
import io.apicurio.registry.storage.RegistryStorage;
import io.apicurio.registry.types.Current;
import io.apicurio.registry.utils.IoUtil;
Expand Down Expand Up @@ -152,4 +156,46 @@ public void testCheckJsonWithReferences() throws Exception {
* HandleReferencesType.DEREFERENCE; }));
*/
}

@Test
public void testLatestBranch() {
try {
ArtifactSearchResults results = clientV3.search().artifacts().get();
results.getArtifacts().forEach(artifact -> {
String groupId = "default";
if (artifact.getGroupId() != null) {
groupId = artifact.getGroupId();
}
BranchMetaData branchMetaData = clientV3.groups().byGroupId(groupId).artifacts()
.byArtifactId(artifact.getArtifactId()).branches().byBranchId("latest").get();
Assertions.assertNotNull(branchMetaData);
Assertions.assertEquals(artifact.getGroupId(), branchMetaData.getGroupId());
Assertions.assertEquals(artifact.getArtifactId(), branchMetaData.getArtifactId());
Assertions.assertEquals("latest", branchMetaData.getBranchId());

VersionMetaData versionMetaData = clientV3.groups().byGroupId(groupId).artifacts()
.byArtifactId(artifact.getArtifactId()).versions()
.byVersionExpression("branch=latest").get();
Assertions.assertNotNull(versionMetaData);
Assertions.assertEquals(artifact.getGroupId(), versionMetaData.getGroupId());
Assertions.assertEquals(artifact.getArtifactId(), versionMetaData.getArtifactId());
});

// Make sure the latest version of "MixAvroExample/Farewell" is version "2"
VersionMetaData versionMetaData = clientV3.groups().byGroupId("MixAvroExample").artifacts()
.byArtifactId("Farewell").versions().byVersionExpression("branch=latest").get();
Assertions.assertNotNull(versionMetaData);
Assertions.assertEquals("2", versionMetaData.getVersion());

// Make sure the latest version of "default/city" is version "2"
versionMetaData = clientV3.groups().byGroupId("default").artifacts().byArtifactId("city")
.versions().byVersionExpression("branch=latest").get();
Assertions.assertNotNull(versionMetaData);
Assertions.assertEquals("2", versionMetaData.getVersion());
} catch (ProblemDetails e) {
System.err.println("ERROR: " + e.getDetail());
throw e;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,25 @@ private void createEntityIndex() {
}
}

if (manifest == null) {
throw new RuntimeException("No manifest found");
}
try {
readManifest(manifest);
} catch (IOException e) {
throw new RuntimeException("Invalid manifest: ", e);
}

// Make sure we sort the versions when upgrading from v2 to v3 - this is
// so that the "latest" branch contains the versions in the correct order.
if (majorVersion == 2) {
versions.sort((v1, v2) -> {
long v1GlobalId = getArtifactVersionOrder(v1);
long v2GlobalId = getArtifactVersionOrder(v2);
return (int) (v1GlobalId - v2GlobalId);
});
}

entities = new LinkedList<>();
if (manifest != null) {
entities.add(manifest);
Expand Down Expand Up @@ -269,4 +288,14 @@ private <T> T readEntry(EntityInfo entry, Class<T> theClass) throws IOException
return entity;
}

private long getArtifactVersionOrder(EntityInfo artifactVersionEntityInfo) {
try {
Entity entity = readArtifactVersion(artifactVersionEntityInfo);
return ((io.apicurio.registry.utils.impexp.v2.ArtifactVersionEntity) entity).globalId;
} catch (IOException e) {
// Nothing we can do here. :(
return 0;
}
}

}
Loading