diff --git a/src/main/java/org/mongeez/ChangeSetExecutor.java b/src/main/java/org/mongeez/ChangeSetExecutor.java index 3dab217..5381a66 100644 --- a/src/main/java/org/mongeez/ChangeSetExecutor.java +++ b/src/main/java/org/mongeez/ChangeSetExecutor.java @@ -27,22 +27,29 @@ public class ChangeSetExecutor { private final Logger logger = LoggerFactory.getLogger(ChangeSetExecutor.class); private MongeezDao dao = null; + private String context = null; - public ChangeSetExecutor(Mongo mongo, String dbName) { - this(mongo, dbName, null); + public ChangeSetExecutor(Mongo mongo, String dbName, String context) { + this(mongo, dbName, context, null); } - public ChangeSetExecutor(Mongo mongo, String dbName, MongoAuth auth) { + public ChangeSetExecutor(Mongo mongo, String dbName, String context, MongoAuth auth) { dao = new MongeezDao(mongo, dbName, auth); + this.context = context; } public void execute(List changeSets) { for (ChangeSet changeSet : changeSets) { - if (changeSet.isRunAlways() || !dao.wasExecuted(changeSet)) { - execute(changeSet); - logger.info("ChangeSet " + changeSet.getChangeId() + " has been executed"); - } else { - logger.info("ChangeSet already executed: " + changeSet.getChangeId()); + if (changeSet.canBeAppliedInContext(context)) { + if (changeSet.isRunAlways() || !dao.wasExecuted(changeSet)) { + execute(changeSet); + logger.info("ChangeSet " + changeSet.getChangeId() + " has been executed"); + } else { + logger.info("ChangeSet already executed: " + changeSet.getChangeId()); + } + } + else { + logger.info("Not executing Changeset {} it cannot run in the context {}", changeSet.getChangeId(), context); } } } diff --git a/src/main/java/org/mongeez/Mongeez.java b/src/main/java/org/mongeez/Mongeez.java index 42c2ef4..5c74a00 100644 --- a/src/main/java/org/mongeez/Mongeez.java +++ b/src/main/java/org/mongeez/Mongeez.java @@ -34,10 +34,11 @@ public class Mongeez { private String dbName; private MongoAuth auth = null; private ChangeSetFileProvider changeSetFileProvider; + private String context = null; public void process() { List changeSets = getChangeSets(); - new ChangeSetExecutor(mongo, dbName, auth).execute(changeSets); + new ChangeSetExecutor(mongo, dbName, context, auth).execute(changeSets); } private List getChangeSets() { @@ -58,6 +59,9 @@ private void logChangeSets(List changeSets) { logger.trace("Changeset"); logger.trace("id: " + changeSet.getChangeId()); logger.trace("author: " + changeSet.getAuthor()); + if (! "".equals(changeSet.getContexts())) { + logger.trace("contexts: {}", changeSet.getContexts()); + } for (Script command : changeSet.getCommands()) { logger.trace("script"); logger.trace(command.getBody()); @@ -89,4 +93,8 @@ public void setChangeSetFileProvider(ChangeSetFileProvider changeSetFileProvider this.changeSetFileProvider = changeSetFileProvider; } + public void setContext(String context) { + this.context = context; + } + } diff --git a/src/main/java/org/mongeez/commands/ChangeSet.java b/src/main/java/org/mongeez/commands/ChangeSet.java index cb863aa..1f16f30 100644 --- a/src/main/java/org/mongeez/commands/ChangeSet.java +++ b/src/main/java/org/mongeez/commands/ChangeSet.java @@ -20,6 +20,8 @@ public class ChangeSet { private String author; private String file; private String resourcePath; + private String contextsStr; + private ArrayList contexts; private boolean failOnError = true; private boolean runAlways; @@ -81,4 +83,40 @@ public void add(Script command) { public List + + + + + + + + + + + + + + diff --git a/src/test/resources/mongeez_contexts.xml b/src/test/resources/mongeez_contexts.xml new file mode 100644 index 0000000..7156880 --- /dev/null +++ b/src/test/resources/mongeez_contexts.xml @@ -0,0 +1,3 @@ + + +