package cc.fascinated.bat.migrations.changelogs; import com.mongodb.client.FindIterable; import io.mongock.api.annotations.ChangeUnit; import io.mongock.api.annotations.Execution; import io.mongock.api.annotations.RollbackExecution; import org.bson.Document; import org.springframework.data.mongodb.core.MongoTemplate; /** * @author Fascinated (fascinated7) */ @ChangeUnit(id="birthday-changelog", order = "001", author = "fascinated7") public class BirthdayProfileChangelog { private final MongoTemplate mongoTemplate; public BirthdayProfileChangelog(MongoTemplate mongoTemplate) { this.mongoTemplate = mongoTemplate; } @Execution public void changeSet() { FindIterable guilds = mongoTemplate.getCollection("guilds").find(); guilds.forEach(guild -> { Document profiles = guild.get("profiles", Document.class); if (profiles == null) { return; } Document birthdayProfile = profiles.get("birthday", Document.class); if (birthdayProfile == null) { return; } birthdayProfile.remove("birthdays"); profiles.put("birthday", birthdayProfile); guild.put("profiles", profiles); mongoTemplate.getCollection("guilds").replaceOne(new Document("_id", guild.get("_id")), guild); }); } @RollbackExecution public void rollback() { // DO NOTHING } }