Skip to content

Commit f3ba652

Browse files
author
Thomasr
committed
add migration to fill folder->createdAt
1 parent a322899 commit f3ba652

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.data.mongodb.core.query.Query;
3737
import org.springframework.data.mongodb.core.query.Update;
3838

39+
import java.time.Instant;
3940
import java.util.Set;
4041

4142
import static org.lowcoder.domain.util.QueryDslUtils.fieldName;
@@ -243,6 +244,30 @@ public void processDocument(Document document) {
243244
});
244245
}
245246

247+
@ChangeSet(order = "024", id = "fill-create-at", author = "")
248+
public void fillCreateAt(MongockTemplate mongoTemplate) {
249+
// Create a query to match all documents
250+
Query query = new Query();
251+
252+
// Use a DocumentCallbackHandler to iterate through each document
253+
mongoTemplate.executeQuery(query, "folder", new DocumentCallbackHandler() {
254+
@Override
255+
public void processDocument(Document document) {
256+
Object object = document.get("createdAt");
257+
if(object != null) return;
258+
// Create an update object to add the 'gid' field
259+
Update update = new Update();
260+
update.set("createdAt", Instant.now());
261+
262+
// Create a query to match the current document by its _id
263+
Query idQuery = new Query(Criteria.where("_id").is(document.getObjectId("_id")));
264+
265+
// Update the document with the new 'gid' field
266+
mongoTemplate.updateFirst(idQuery, update, "folder");
267+
}
268+
});
269+
}
270+
246271
private void addGidField(MongockTemplate mongoTemplate, String collectionName) {
247272
// Create a query to match all documents
248273
Query query = new Query();

0 commit comments

Comments
 (0)