How to: complex logic in migration script

176 views
Skip to first unread message

Jeremy Townson

unread,
Jul 9, 2015, 5:46:39 PM7/9/15
to mongee...@googlegroups.com
I have a migration task involving the generation of thumbnail images from existing images in a mongo collection.

This seems beyond the capabilities of mongo javascript so a changeset js file presumably will not work.

What approach would others suggest?

It would be easy to write a Java class to do the thumbnail generation. Is there a way of hooking a java call into a mongeez changeset?

Jeremy Townson

unread,
Aug 1, 2015, 8:49:03 AM8/1/15
to Mongeez Users
Here is an approach that combines the normal mongeez changesets with supplementary ones, written in java and works well.

From mongo 3 onwards, map reduce does not work from javascript migration files, so the java approach may be useful in a wider range of migration scenarios.

public class DatabaseMaintenance {

private List<SupplementaryChangeSet> supplementaryChangeSets;

@Autowired
public DatabaseMaintenance(SupplementaryChangeSets supplementaryChangeSets) {
this.supplementaryChangeSets = supplementaryChangeSets.get();
}

public void perform(DB db) {

runMongeezChangesets(db);

runSupplementaryChangesets(db);
}

// Run conventional js or xml migration scripts
private void runMongeezChangesets(DB db) {
Mongeez mongeez = new Mongeez();
mongeez.setMongo(db.getMongo());
mongeez.setDbName(db.getName());
mongeez.setFile(new ClassPathResource("db-maintenance/mongeez.xml"));
mongeez.process();
}

// Run additional changesets that do not fit into js or xml form
private void runSupplementaryChangesets(DB db) {

MongeezDao dao = new MongeezDao(db.getMongo(), db.getName());

for (SupplementaryChangeSet changeSet : supplementaryChangeSets) {

if ( ! dao.wasExecuted(changeSet) && changeSet.perform(db)) {
dao.logChangeSet(changeSet);
}
}
}
}


public abstract class SupplementaryChangeSet extends ChangeSet {
public abstract boolean perform(DB db);
}

public class
SupplementaryChangeSets {

@Resource
private ThumbnailGenerationChangeSet thumbnailGenerationChangeSet;

public List<SupplementaryChangeSet> get() {
return asList(thumbnailGenerationChangeSet);
}
}

@Component
public class ThumbnailGenerationChangeSet extends SupplementaryChangeSet {

@Override
public boolean perform(DB db) {

// ... do stuff in java ...

}
}

Michael Kasch

unread,
Apr 10, 2016, 5:59:53 PM4/10/16
to Mongeez Users
if it is stll intersting to you you should also have a look at: 
https://github.com/mongobee/mongobee they seem to have this as their design concept
Reply all
Reply to author
Forward
0 new messages