Skip to content

Commit

Permalink
add failOnError flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugene Platonov committed May 16, 2013
1 parent e96fad6 commit eb2bd05
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/main/java/org/mongeez/ChangeSetExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,16 @@ public void execute(List<ChangeSet> changeSets) {
}

private void execute(ChangeSet changeSet) {
for (Script command : changeSet.getCommands()) {
command.run(dao);
try {
for (Script command : changeSet.getCommands()) {
command.run(dao);
}
} catch (RuntimeException e) {
if (changeSet.isFailOnError()) {
throw e;
} else {
logger.warn("ChangeSet " + changeSet.getChangeId() + " has failed, but failOnError is set to false", e.getMessage());
}
}
dao.logChangeSet(changeSet);
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/mongeez/commands/ChangeSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class ChangeSet {
private String file;
private String resourcePath;

private boolean failOnError = true;
private boolean runAlways;

private List<Script> commands = new ArrayList<Script>();
Expand Down Expand Up @@ -49,6 +50,14 @@ public void setRunAlways(boolean runAlways) {
this.runAlways = runAlways;
}

public boolean isFailOnError() {
return failOnError;
}

public void setFailOnError(boolean failOnError) {
this.failOnError = failOnError;
}

public String getFile() {
return file;
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/org/mongeez/MongeezTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static org.testng.Assert.assertEquals;

import com.mongodb.DB;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import org.springframework.core.io.ClassPathResource;
import org.testng.annotations.BeforeMethod;
Expand Down Expand Up @@ -60,6 +61,22 @@ public void testRunTwice() throws Exception {
testMongeez();
}

@Test(groups = "dao")
public void testFailOnError_False() throws Exception {
assertEquals(db.getCollection("mongeez").count(), 0);

Mongeez mongeez = create("mongeez_fail.xml");
mongeez.process();

assertEquals(db.getCollection("mongeez").count(), 2);
}

@Test(groups = "dao", expectedExceptions = com.mongodb.CommandFailureException.class)
public void testFailOnError_True() throws Exception {
Mongeez mongeez = create("mongeez_fail_fail.xml");
mongeez.process();
}

@Test(groups = "dao")
public void testNoFiles() throws Exception {
Mongeez mongeez = create("mongeez_empty.xml");
Expand Down
21 changes: 21 additions & 0 deletions src/test/resources/failed_changeset.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!--
~ Copyright 2011 SecondMarket Labs, LLC.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
~ on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and limitations under the License.
-->

<mongoChangeLog>

<changeSet changeId="i-will-fail" author="jozic" failOnError="true">
<script>
db.NonExisting.fail({miserably})
</script>
</changeSet>

</mongoChangeLog>
21 changes: 21 additions & 0 deletions src/test/resources/failing_changeset.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!--
~ Copyright 2011 SecondMarket Labs, LLC.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
~ on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and limitations under the License.
-->

<mongoChangeLog>

<changeSet changeId="i-will-fail" author="jozic" failOnError="false">
<script>
db.NonExisting.fail({miserably})
</script>
</changeSet>

</mongoChangeLog>
15 changes: 15 additions & 0 deletions src/test/resources/mongeez_fail.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!--
~ Copyright 2011 SecondMarket Labs, LLC.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
~ on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and limitations under the License.
-->

<changeFiles>
<file path="failing_changeset.xml"/>
</changeFiles>
15 changes: 15 additions & 0 deletions src/test/resources/mongeez_fail_fail.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!--
~ Copyright 2011 SecondMarket Labs, LLC.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
~ on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and limitations under the License.
-->

<changeFiles>
<file path="failed_changeset.xml"/>
</changeFiles>

0 comments on commit eb2bd05

Please sign in to comment.