Skip to content

Commit 352fdc8

Browse files
committed
Fix issues with import/export for branches
1 parent 93a4989 commit 352fdc8

File tree

8 files changed

+62
-30
lines changed

8 files changed

+62
-30
lines changed

app/src/main/java/io/apicurio/registry/storage/impl/sql/AbstractSqlRegistryStorage.java

+42-20
Original file line numberDiff line numberDiff line change
@@ -2279,15 +2279,17 @@ public void exportData(Function<Entity, Void> handler) throws RegistryStorageExc
22792279

22802280
// Export all branches
22812281
/////////////////////////////////
2282-
// TODO add the list of versions to the BranchEntity when exporting
22832282
handles.withHandle(handle -> {
22842283
Stream<BranchEntity> stream = handle.createQuery(sqlStatements.exportBranches())
22852284
.setFetchSize(50)
22862285
.map(BranchEntityMapper.instance)
22872286
.stream();
22882287
// Process and then close the stream.
22892288
try (stream) {
2290-
stream.forEach(handler::apply);
2289+
stream.forEach(branch -> {
2290+
branch.versions = getBranchVersionNumbersRaw(handle, branch.toGA(), branch.toBranchId());
2291+
handler.apply(branch);
2292+
});
22912293
}
22922294
return null;
22932295
});
@@ -3426,6 +3428,15 @@ public BranchMetaDataDto getBranchMetaData(GA ga, BranchId branchId) {
34263428
});
34273429
}
34283430

3431+
protected List<String> getBranchVersionNumbersRaw(Handle handle, GA ga, BranchId branchId) {
3432+
return handle.createQuery(sqlStatements.selectBranchVersionNumbers())
3433+
.bind(0, ga.getRawGroupId())
3434+
.bind(1, ga.getRawArtifactId())
3435+
.bind(2, branchId.getRawBranchId())
3436+
.map(StringMapper.instance)
3437+
.list();
3438+
}
3439+
34293440
@Override
34303441
public VersionSearchResultsDto getBranchVersions(GA ga, BranchId branchId, int offset, int limit) {
34313442
return handles.withHandleNoException(handle -> {
@@ -3755,24 +3766,35 @@ public void deleteBranch(GA ga, BranchId branchId) {
37553766
@Override
37563767
@Transactional
37573768
public void importBranch(BranchEntity entity) {
3758-
// var gav = entity.toGAV();
3759-
// var branchId = entity.toBranchId();
3760-
// handles.withHandleNoException(handle -> {
3761-
// try {
3762-
// handle.createUpdate(sqlStatements.importBranch())
3763-
// .bind(0, gav.getRawGroupId())
3764-
// .bind(1, gav.getRawArtifactId())
3765-
// .bind(2, branchId.getRawBranchId())
3766-
// .bind(3, entity.branchOrder)
3767-
// .bind(4, gav.getRawVersionId())
3768-
// .execute();
3769-
// } catch (Exception ex) {
3770-
// if (sqlStatements.isForeignKeyViolation(ex)) {
3771-
// throw new VersionNotFoundException(gav, ex);
3772-
// }
3773-
// throw ex;
3774-
// }
3775-
// });
3769+
var ga = entity.toGA();
3770+
var branchId = entity.toBranchId();
3771+
handles.withHandleNoException(handle -> {
3772+
try {
3773+
handle.createUpdate(sqlStatements.insertBranch())
3774+
.bind(0, ga.getRawGroupId())
3775+
.bind(1, ga.getRawArtifactId())
3776+
.bind(2, branchId.getRawBranchId())
3777+
.bind(3, entity.description)
3778+
.bind(4, entity.userDefined)
3779+
.bind(5, entity.owner)
3780+
.bind(6, entity.createdOn)
3781+
.bind(7, entity.modifiedBy)
3782+
.bind(8, entity.modifiedOn)
3783+
.execute();
3784+
} catch (Exception ex) {
3785+
if (sqlStatements.isForeignKeyViolation(ex)) {
3786+
throw new ArtifactNotFoundException(ga.getRawGroupIdWithDefaultString(), ga.getRawArtifactId());
3787+
}
3788+
throw ex;
3789+
}
3790+
3791+
// Append each of the versions onto the branch
3792+
if (entity.versions != null) {
3793+
entity.versions.forEach(version -> {
3794+
appendVersionToBranchRaw(handle, ga, branchId, new VersionId(version));
3795+
});
3796+
}
3797+
});
37763798
}
37773799

37783800
@Override

app/src/main/java/io/apicurio/registry/storage/impl/sql/CommonSqlStatements.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ public String exportGroups() {
729729

730730
@Override
731731
public String exportBranches() {
732-
return "SELECT * FROM branches b";
732+
return "SELECT * FROM branches";
733733
}
734734

735735

@@ -1048,6 +1048,11 @@ public String selectBranch() {
10481048
return "SELECT b.* FROM branches b WHERE b.groupId = ? AND b.artifactId = ? AND b.branchId = ?";
10491049
}
10501050

1051+
@Override
1052+
public String selectBranchVersionNumbers() {
1053+
return "SELECT bv.version FROM branch_versions bv WHERE bv.groupId = ? AND bv.artifactId = ? AND bv.branchId = ?";
1054+
}
1055+
10511056
@Override
10521057
public String selectBranchTip() {
10531058
return "SELECT bv.groupId, bv.artifactId, bv.version FROM branch_versions bv " +
@@ -1098,8 +1103,4 @@ public String deleteAllBranches() {
10981103
return "DELETE FROM branches";
10991104
}
11001105

1101-
@Override
1102-
public String importBranch() {
1103-
return insertBranch();
1104-
}
11051106
}

app/src/main/java/io/apicurio/registry/storage/impl/sql/SqlStatements.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,6 @@ public interface SqlStatements {
481481

482482
public String importArtifactVersion();
483483

484-
public String importBranch();
485-
486484
public String selectMaxContentId();
487485

488486
public String selectMaxGlobalId();
@@ -574,6 +572,8 @@ public interface SqlStatements {
574572

575573
public String selectBranch();
576574

575+
public String selectBranchVersionNumbers();
576+
577577
public String selectBranchTip();
578578

579579
public String selectBranchTipNotDisabled();

app/src/main/java/io/apicurio/registry/storage/impl/sql/mappers/BranchEntityMapper.java

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public BranchEntity map(ResultSet rs) throws SQLException {
2323
.artifactId(rs.getString("artifactId"))
2424
.branchId(rs.getString("branchId"))
2525
.description(rs.getString("description"))
26+
.userDefined(rs.getBoolean("userDefined"))
2627
.owner(rs.getString("owner"))
2728
.createdOn(rs.getTimestamp("createdOn").getTime())
2829
.modifiedBy(rs.getString("modifiedBy"))

app/src/test/java/io/apicurio/registry/noprofile/rest/v3/BranchesTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,6 @@ public void testGetMostRecentVersionFromBranch() throws Exception {
544544
Assertions.assertThrows(Exception.class, () -> {
545545
clientV3.groups().byGroupId(groupId).artifacts().byArtifactId(artifactId).versions().byVersionExpression("branch=invalid").get();
546546
});
547-
548547
}
549548

550549
private static AddVersionToBranch addVersion(String version) {

integration-tests/src/test/java/io/apicurio/tests/migration/GenerateCanonicalHashImportIT.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void testGeneratingCanonicalHashOnImport() throws Exception {
6868
The only way is to generate canonical hash and then search artifact by it. But that needs apicurio-registry-app module as dependency.
6969
*/
7070

71-
var registryContent = client.groups().byGroupId(groupId).artifacts().byArtifactId(artifactId).versions().byVersionExpression("branch=latest").content().get();
71+
var registryContent = client.groups().byGroupId(groupId).artifacts().byArtifactId(artifactId).versions().byVersionExpression("1").content().get();
7272
assertNotNull(registryContent);
7373
assertEquals(content, IoUtil.toString(registryContent));
7474
}

utils/importexport/src/main/java/io/apicurio/registry/utils/impexp/BranchEntity.java

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.apicurio.registry.utils.impexp;
22

3+
import io.apicurio.registry.model.BranchId;
4+
import io.apicurio.registry.model.GA;
35
import io.quarkus.runtime.annotations.RegisterForReflection;
46
import lombok.AllArgsConstructor;
57
import lombok.Builder;
@@ -21,12 +23,20 @@ public class BranchEntity extends Entity {
2123
public String artifactId;
2224
public String branchId;
2325
public String description;
26+
public boolean userDefined;
2427
public String owner;
2528
public long createdOn;
2629
public String modifiedBy;
2730
public long modifiedOn;
2831
public List<String> versions;
2932

33+
public GA toGA() {
34+
return new GA(groupId, artifactId);
35+
}
36+
37+
public BranchId toBranchId() {
38+
return new BranchId(branchId);
39+
}
3040

3141
@Override
3242
public EntityType getEntityType() {

utils/importexport/src/main/java/io/apicurio/registry/utils/impexp/EntityWriter.java

-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ private ZipEntry createZipEntry(EntityType type, String groupId, String artifact
123123
path = String.format("groups/%s/artifacts/%s/rules/%s.%s.%s", groupOrDefault(groupId), artifactId, fileName, type.name(), fileExt);
124124
break;
125125
case ArtifactVersion:
126-
case Branch:
127126
path = String.format("groups/%s/artifacts/%s/versions/%s.%s.%s", groupOrDefault(groupId), artifactId, fileName, type.name(), fileExt);
128127
break;
129128
case Content:

0 commit comments

Comments
 (0)