Skip to content

Commit 9d42e1c

Browse files
dragonpooludomikula
authored andcommitted
check mongodb version
1 parent a05e43e commit 9d42e1c

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,29 @@ public void addGidIndexesUnique(MongockTemplate mongoTemplate) {
301301
ensureIndexes(mongoTemplate, LibraryQuery.class, makeIndex("gid").unique());
302302
}
303303

304+
private int getMongoDBVersion(MongockTemplate mongoTemplate) {
305+
Document buildInfo = mongoTemplate.executeCommand(new Document("buildInfo", 1));
306+
String versionString = buildInfo.getString("version");
307+
if(versionString == null) return -1;
308+
String[] versionParts = versionString.split("\\.");
309+
int majorVersion = Integer.parseInt(versionParts[0]);
310+
return majorVersion;
311+
}
312+
304313
@ChangeSet(order = "026", id = "add-time-series-snapshot-history", author = "")
305314
public void addTimeSeriesSnapshotHistory(MongockTemplate mongoTemplate, CommonConfig commonConfig) {
315+
int mongoVersion = getMongoDBVersion(mongoTemplate);
316+
if (mongoVersion < 5) {
317+
log.warn("MongoDB version is below 5. Time-series collections are not supported. Upgrade the MongoDB version.");
318+
}
319+
306320
// Create the time-series collection if it doesn't exist
307321
if (!mongoTemplate.collectionExists(ApplicationHistorySnapshotTS.class)) {
308-
mongoTemplate.createCollection(ApplicationHistorySnapshotTS.class, CollectionOptions.empty().timeSeries("createdAt"));
322+
if(mongoVersion < 5) {
323+
mongoTemplate.createCollection(ApplicationHistorySnapshotTS.class, CollectionOptions.empty().timeSeries("createdAt"));
324+
} else {
325+
mongoTemplate.createCollection(ApplicationHistorySnapshotTS.class);
326+
}
309327
}
310328
Instant thresholdDate = Instant.now().minus(commonConfig.getQuery().getAppSnapshotKeepDuration(), ChronoUnit.DAYS);
311329
List<ApplicationHistorySnapshot> snapshots = mongoTemplate.find(new Query().addCriteria(Criteria.where("createdAt").gte(thresholdDate)), ApplicationHistorySnapshot.class);

0 commit comments

Comments
 (0)