Skip to content

Commit 24ba4bd

Browse files
committed
convert snapshot migration to use aggregation pipeline
1 parent 3a443bb commit 24ba4bd

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

server/api-service/lowcoder-server/src/main/java/org/lowcoder/runner/migrations/DatabaseChangelog.java

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
import java.time.Instant;
4646
import java.time.temporal.ChronoUnit;
47+
import java.util.Arrays;
4748
import java.util.List;
4849
import java.util.Set;
4950

@@ -319,35 +320,43 @@ public void addTimeSeriesSnapshotHistory(MongockTemplate mongoTemplate, CommonCo
319320

320321
// Create the time-series collection if it doesn't exist
321322
if (!mongoTemplate.collectionExists(ApplicationHistorySnapshotTS.class)) {
322-
if(mongoVersion < 5) {
323+
if (mongoVersion < 5) {
323324
mongoTemplate.createCollection(ApplicationHistorySnapshotTS.class);
324325
} else {
325326
mongoTemplate.createCollection(ApplicationHistorySnapshotTS.class, CollectionOptions.empty().timeSeries("createdAt"));
326327
}
327328
}
329+
328330
Instant thresholdDate = Instant.now().minus(commonConfig.getQuery().getAppSnapshotKeepDuration(), ChronoUnit.DAYS);
329-
List<ApplicationHistorySnapshot> snapshots = mongoTemplate.find(new Query().addCriteria(Criteria.where("createdAt").gte(thresholdDate)), ApplicationHistorySnapshot.class);
330-
snapshots.forEach(snapshot -> {
331-
ApplicationHistorySnapshotTS applicationHistorySnapshotTS = new ApplicationHistorySnapshotTS();
332-
applicationHistorySnapshotTS.setApplicationId(snapshot.getApplicationId());
333-
applicationHistorySnapshotTS.setDsl(snapshot.getDsl());
334-
applicationHistorySnapshotTS.setContext(snapshot.getContext());
335-
applicationHistorySnapshotTS.setCreatedAt(snapshot.getCreatedAt());
336-
applicationHistorySnapshotTS.setCreatedBy(snapshot.getCreatedBy());
337-
applicationHistorySnapshotTS.setModifiedBy(snapshot.getModifiedBy());
338-
applicationHistorySnapshotTS.setUpdatedAt(snapshot.getUpdatedAt());
339-
applicationHistorySnapshotTS.setId(snapshot.getId());
340-
mongoTemplate.insert(applicationHistorySnapshotTS);
341-
mongoTemplate.remove(snapshot);
342-
});
343331

344-
// Ensure indexes if needed
332+
// Use aggregation to move and transform data
333+
Document match = new Document("$match",
334+
new Document("createdAt", new Document("$gte", thresholdDate)));
335+
336+
Document project = new Document("$project", new Document()
337+
.append("applicationId", 1)
338+
.append("dsl", 1)
339+
.append("context", 1)
340+
.append("createdAt", 1)
341+
.append("createdBy", 1)
342+
.append("modifiedBy", 1)
343+
.append("updatedAt", 1)
344+
.append("id", "$_id")); // Map MongoDB's default `_id` to `id` if needed.
345+
346+
Document out = new Document("$out", "applicationHistorySnapshotTS"); // Target collection name
347+
348+
// Execute the aggregation pipeline
349+
mongoTemplate.getDb()
350+
.getCollection("applicationHistorySnapshot") // Original collection name
351+
.aggregate(Arrays.asList(match, project, out))
352+
.toCollection();
353+
345354
ensureIndexes(mongoTemplate, ApplicationHistorySnapshotTS.class,
346355
makeIndex("applicationId"),
347-
makeIndex("createdAt")
348-
);
356+
makeIndex("createdAt"));
349357
}
350358

359+
351360
private void addGidField(MongockTemplate mongoTemplate, String collectionName) {
352361
// Create a query to match all documents
353362
Query query = new Query();

0 commit comments

Comments
 (0)