Last active
December 18, 2017 14:06
-
-
Save tamboer/ea6735e9ceed12250157cc7ad94d3299 to your computer and use it in GitHub Desktop.
Mongobee migration configuration properties and changelog example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| map: | |
| migration: | |
| enabled: true | |
| */ | |
| import org.springframework.boot.context.properties.ConfigurationProperties; | |
| @ConfigurationProperties("map.migration") | |
| public class MigrationProperties { | |
| private boolean enabled = false; | |
| public boolean isEnabled() { | |
| return enabled; | |
| } | |
| public void setEnabled(boolean enabled) { | |
| this.enabled = enabled; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.github.mongobee.Mongobee; | |
| import com.mongodb.MongoClient; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.boot.autoconfigure.mongo.MongoProperties; | |
| import org.springframework.boot.context.properties.EnableConfigurationProperties; | |
| import org.springframework.context.annotation.Bean; | |
| import org.springframework.context.annotation.Configuration; | |
| import org.springframework.core.env.Environment; | |
| @Configuration | |
| @EnableConfigurationProperties({MongoProperties.class, MigrationProperties.class}) | |
| public class MigrationConfiguration { | |
| @Autowired | |
| private MongoProperties mongoProperties; | |
| @Autowired | |
| private MigrationProperties migrationProperties; | |
| @Autowired | |
| private MongoClient mongo; | |
| @Bean | |
| @Autowired | |
| public Mongobee mongobee(final Environment environment) { | |
| final Mongobee runner = new Mongobee(mongo); | |
| runner.setDbName(mongoProperties.getMongoClientDatabase()); | |
| runner.setSpringEnvironment(environment); | |
| runner.setChangeLogsScanPackage("com.tvh.assetmanagement.migration.changelog"); | |
| runner.setEnabled(migrationProperties.isEnabled()); | |
| return runner; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.tvh.assetmanagement.migration.changelog; | |
| import com.github.mongobee.changeset.ChangeLog; | |
| import com.github.mongobee.changeset.ChangeSet; | |
| import com.mongodb.client.MongoDatabase; | |
| import com.mongodb.client.model.Filters; | |
| import com.tvh.assetmanagement.migration.DatabaseCollections; | |
| import org.bson.Document; | |
| import org.bson.conversions.Bson; | |
| @ChangeLog(order = "004") | |
| public class PickupHandinRenameChangeLog { | |
| private static final String PICKUP_DATE_TIME_FIELD = "pickupDateTime"; | |
| private static final String HANDIN_DATE_TIME_FIELD = "handinDateTime"; | |
| private static final String START_DATE_TIME_FIELD = "startDateTime"; | |
| private static final String END_DATE_TIME_FIELD = "endDateTime"; | |
| @ChangeSet(author = "xxxxxxxxx", order = "004-001", id="004-001-rename-booking-date-time-fields") | |
| public void renameBookingDateTimeFields(final MongoDatabase db){ | |
| db.getCollection(DatabaseCollections.BOOKINGS) | |
| .updateMany(Filters.and(withOldFields(), withoutNewFields()), renameFields()); | |
| } | |
| private static Bson renameFields() { | |
| final Document rename = new Document(); | |
| rename.put(PICKUP_DATE_TIME_FIELD, START_DATE_TIME_FIELD); | |
| rename.put(HANDIN_DATE_TIME_FIELD, END_DATE_TIME_FIELD); | |
| return new Document("$rename", rename); | |
| } | |
| private static Bson withOldFields() { | |
| return Filters.and( | |
| Filters.exists(PICKUP_DATE_TIME_FIELD, true), | |
| Filters.exists(HANDIN_DATE_TIME_FIELD, true)); | |
| } | |
| private static Bson withoutNewFields() { | |
| return Filters.and( | |
| Filters.exists(START_DATE_TIME_FIELD, false), | |
| Filters.exists(END_DATE_TIME_FIELD, false)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment