Skip to content

Commit

Permalink
Add context functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
echrist committed Oct 25, 2013
1 parent ea7e75e commit c78b617
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 9 deletions.
23 changes: 15 additions & 8 deletions src/main/java/org/mongeez/ChangeSetExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<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);
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/org/mongeez/Mongeez.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChangeSet> changeSets = getChangeSets();
new ChangeSetExecutor(mongo, dbName, auth).execute(changeSets);
new ChangeSetExecutor(mongo, dbName, context, auth).execute(changeSets);
}

private List<ChangeSet> getChangeSets() {
Expand All @@ -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());
Expand Down Expand Up @@ -89,4 +93,8 @@ public void setChangeSetFileProvider(ChangeSetFileProvider changeSetFileProvider
this.changeSetFileProvider = changeSetFileProvider;
}

public void setContext(String context) {
this.context = context;
}

}
38 changes: 38 additions & 0 deletions src/main/java/org/mongeez/commands/ChangeSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class ChangeSet {
private String author;
private String file;
private String resourcePath;
private String contextsStr;
private ArrayList<String> contexts;

private boolean failOnError = true;
private boolean runAlways;
Expand Down Expand Up @@ -81,4 +83,40 @@ 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);
}
}
}

if (contexts.size() > 0 && (context == null || !contexts.contains(context.toLowerCase().trim()))) {
return false;
}
return true;
}
}
41 changes: 41 additions & 0 deletions src/test/java/org/mongeez/MongeezTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
40 changes: 40 additions & 0 deletions src/test/resources/changeset_with_contexts.xml
Original file line number Diff line number Diff line change
@@ -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>
3 changes: 3 additions & 0 deletions src/test/resources/mongeez_contexts.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<changeFiles>
<file path="changeset_with_contexts.xml"/>
</changeFiles>

0 comments on commit c78b617

Please sign in to comment.