Permalink
Browse files

add failOnError flag

  • Loading branch information...
1 parent e96fad6 commit eb2bd058c75797294a30f7bcfea7efa7118cfc26 @jozic jozic committed May 16, 2013
@@ -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);
}
@@ -21,6 +21,7 @@
private String file;
private String resourcePath;
+ private boolean failOnError = true;
private boolean runAlways;
private List<Script> commands = new ArrayList<Script>();
@@ -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;
}
@@ -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;
@@ -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");
@@ -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>
@@ -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>
@@ -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>
@@ -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.