Migration for createdAt and updatedAt field for userCollection if not present (#5249)

This commit is contained in:
Abhijeet 2021-06-19 18:03:23 +05:30 committed by GitHub
parent e6adc27b7e
commit 598a48cea7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2562,5 +2562,27 @@ public class DatabaseChangelog {
// }
// }
@ChangeSet(order = "074", id = "ensure-user-created-and-updated-at-fields", author = "")
public void ensureUserCreatedAndUpdatedAt(MongoTemplate mongoTemplate) {
final List<User> missingCreatedAt = mongoTemplate.find(
query(where("createdAt").exists(false)),
User.class
);
for (User user : missingCreatedAt) {
user.setCreatedAt(Instant.parse("2019-01-07T00:00:00.00Z"));
mongoTemplate.save(user);
}
final List<User> missingUpdatedAt = mongoTemplate.find(
query(where("updatedAt").exists(false)),
User.class
);
for (User user : missingUpdatedAt) {
user.setUpdatedAt(Instant.now());
mongoTemplate.save(user);
}
}
}