Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add context support #33

Merged
merged 2 commits into from

2 participants

@exell-christopher

Written so that existing changesets will run as is, and ones with contexts specified will only run if Mongeez has the correct context set.

src/main/java/org/mongeez/commands/ChangeSet.java
((20 lines not shown))
+ public boolean canBeAppliedInContext(String context) {
+ if (contextsStr == null) {
+ return true;
+ }
+
+ if (contexts == null) {
+ contexts = new ArrayList<String>();
+ for (String requiredContext : contextsStr.split(",")) {
+ String cleanedContext = requiredContext.toLowerCase().trim();
+ if (cleanedContext.length() > 0) {
+ contexts.add(cleanedContext);
+ }
+ }
+ }
+
+ if (contexts.size() > 0 && (context == null || !contexts.contains(context.toLowerCase().trim()))) {
@jozic Collaborator
jozic added a note

what about
return contexts.isEmpty() || (context != null && contexts.contains(context.toLowerCase().trim()))
?

I don't see any issue with that. I'll put it in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jozic
Collaborator

lgtm

@jozic jozic merged commit 8df46bd into from
@exell-christopher

Thanks. Do you have a timeline for the next release to the maven repos?

@jozic
Collaborator

@epishkin could you please help with new release?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 25, 2013
  1. Add context functionality

    echrist authored
Commits on Oct 29, 2013
  1. Clean up return logic

    echrist authored
This page is out of date. Refresh to see the latest.
View
23 src/main/java/org/mongeez/ChangeSetExecutor.java
@@ -27,22 +27,29 @@
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<ChangeSet> 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);
}
}
}
View
10 src/main/java/org/mongeez/Mongeez.java
@@ -34,10 +34,11 @@
private String dbName;
private MongoAuth auth = null;
private ChangeSetFileProvider changeSetFileProvider;
+ private String context = null;
public void process() {
List<ChangeSet> changeSets = getChangeSets();
- new ChangeSetExecutor(mongo, dbName, auth).execute(changeSets);
+ new ChangeSetExecutor(mongo, dbName, context, auth).execute(changeSets);
}
private List<ChangeSet> getChangeSets() {
@@ -58,6 +59,9 @@ private void logChangeSets(List<ChangeSet> 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;
+ }
+
}
View
34 src/main/java/org/mongeez/commands/ChangeSet.java
@@ -20,6 +20,8 @@
private String author;
private String file;
private String resourcePath;
+ private String contextsStr;
+ private ArrayList<String> contexts;
private boolean failOnError = true;
private boolean runAlways;
@@ -81,4 +83,36 @@ public void add(Script command) {
public List<Script> getCommands() {
return commands;
}
+
+ public String getContexts()
+ {
+ if (contextsStr == null)
+ {
+ contextsStr = "";
+ }
+
+ return contextsStr;
+ }
+
+ public void setContexts(String contextsStr) {
+ this.contextsStr = contextsStr;
+ contexts = null;
+ }
+
+ public boolean canBeAppliedInContext(String context) {
+ if (contextsStr == null) {
+ return true;
+ }
+
+ if (contexts == null) {
+ contexts = new ArrayList<String>();
+ for (String requiredContext : contextsStr.split(",")) {
+ String cleanedContext = requiredContext.toLowerCase().trim();
+ if (cleanedContext.length() > 0) {
+ contexts.add(cleanedContext);
+ }
+ }
+ }
+ return contexts.isEmpty() || (context != null && contexts.contains(context.toLowerCase().trim()));
+ }
}
View
41 src/test/java/org/mongeez/MongeezTest.java
@@ -103,4 +103,45 @@ public void testNoFailureOnNoChangeFilesBlock() throws Exception {
mongeez.process();
assertEquals(db.getCollection("mongeez").count(), 1);
}
+
+ @Test(groups = "dao")
+ public void testChangesWContextContextNotSet() throws Exception {
+ assertEquals(db.getCollection("mongeez").count(), 0);
+
+ Mongeez mongeez = create("mongeez_contexts.xml");
+ mongeez.process();
+ assertEquals(db.getCollection("mongeez").count(), 2);
+ assertEquals(db.getCollection("car").count(), 2);
+ assertEquals(db.getCollection("user").count(), 0);
+ assertEquals(db.getCollection("organization").count(), 0);
+ assertEquals(db.getCollection("house").count(), 0);
+ }
+
+ @Test(groups = "dao")
+ public void testChangesWContextContextSetToUsers() throws Exception {
+ assertEquals(db.getCollection("mongeez").count(), 0);
+
+ Mongeez mongeez = create("mongeez_contexts.xml");
+ mongeez.setContext("users");
+ mongeez.process();
+ assertEquals(db.getCollection("mongeez").count(), 4);
+ assertEquals(db.getCollection("car").count(), 2);
+ assertEquals(db.getCollection("user").count(), 2);
+ assertEquals(db.getCollection("organization").count(), 0);
+ assertEquals(db.getCollection("house").count(), 2);
+ }
+
+ @Test(groups = "dao")
+ public void testChangesWContextContextSetToOrganizations() throws Exception {
+ assertEquals(db.getCollection("mongeez").count(), 0);
+
+ Mongeez mongeez = create("mongeez_contexts.xml");
+ mongeez.setContext("organizations");
+ mongeez.process();
+ assertEquals(db.getCollection("mongeez").count(), 4);
+ assertEquals(db.getCollection("car").count(), 2);
+ assertEquals(db.getCollection("user").count(), 0);
+ assertEquals(db.getCollection("organization").count(), 2);
+ assertEquals(db.getCollection("house").count(), 2);
+ }
}
View
40 src/test/resources/changeset_with_contexts.xml
@@ -0,0 +1,40 @@
+<mongoChangeLog>
+ <changeSet changeId="ChangeSet-1" author="mlysaght" contexts="organizations">
+ <script>
+ db.organization.insert({
+ "Organization" : "10Gen",
+ "Location" : "NYC",
+ DateFounded : {"Year" : 2008, "Month" : 01, "day" :01}
+ });
+ db.organization.insert({
+ "Organization" : "SecondMarket",
+ "Location" : "NYC",
+ DateFounded : {"Year" : 2004, "Month" : 05, "day" :04}
+ });
+ </script>
+ </changeSet>
+ <changeSet changeId="ChangeSet-2" author="mlysaght" contexts="users">
+ <script>
+ db.user.insert({ "Name" : "Michael Lysaght"});
+ </script>
+ <script>
+ db.user.insert({ "Name" : "Oleksii Iepishkin"});
+ </script>
+ </changeSet>
+ <changeSet changeId="ChangeSet-3" author="exell">
+ <script>
+ db.car.insert({ "Type" : "Porsche"});
+ </script>
+ <script>
+ db.car.insert({ "Type" : "Lamborghini"});
+ </script>
+ </changeSet>
+ <changeSet changeId="ChangeSet-4" author="exell" contexts="users, organizations">
+ <script>
+ db.house.insert({ "Type" : "Bungalow"});
+ </script>
+ <script>
+ db.house.insert({ "Type" : "Split-Level"});
+ </script>
+ </changeSet>
+</mongoChangeLog>
View
3  src/test/resources/mongeez_contexts.xml
@@ -0,0 +1,3 @@
+<changeFiles>
+ <file path="changeset_with_contexts.xml"/>
+</changeFiles>
Something went wrong with that request. Please try again.