couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tonysu...@apache.org
Subject couchdb-mango git commit: Return success and failure information
Date Thu, 02 Apr 2015 21:40:23 GMT
Repository: couchdb-mango
Updated Branches:
  refs/heads/2651-delete-bulk-docs 8ce9bf4ba -> 985c952aa


Return success and failure information

Rather than just return "indexes deleted", we return a list of successful deletes
and failed deletes along with the ids. For a single delete, we still throw the same
errors as before to be backwards compatible with any applications expecting an error.

Fixes COUCHDB-2651


Project: http://git-wip-us.apache.org/repos/asf/couchdb-mango/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-mango/commit/985c952a
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-mango/tree/985c952a
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-mango/diff/985c952a

Branch: refs/heads/2651-delete-bulk-docs
Commit: 985c952aa2274664107472675fe37eea105085b3
Parents: 8ce9bf4
Author: Tony Sun <tony.sun@cloudant.com>
Authored: Thu Apr 2 14:40:21 2015 -0700
Committer: Tony Sun <tony.sun@cloudant.com>
Committed: Thu Apr 2 14:40:21 2015 -0700

----------------------------------------------------------------------
 src/mango_httpd.erl        | 31 +++++++++++++++++++------------
 src/mango_idx.erl          |  8 ++++----
 test/01-index-crud-test.py |  6 +++++-
 test/mango.py              |  2 +-
 4 files changed, 29 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/985c952a/src/mango_httpd.erl
----------------------------------------------------------------------
diff --git a/src/mango_httpd.erl b/src/mango_httpd.erl
index 3ea5520..671ffd2 100644
--- a/src/mango_httpd.erl
+++ b/src/mango_httpd.erl
@@ -89,17 +89,18 @@ handle_index_req(#httpd{method='POST', path_parts=[_, <<"_index">>,
     Idxs = mango_idx:list(Db),
     DDocs = get_bulk_delete_ddocs(Opts),
     DelOpts = get_idx_w_opts(Opts),
-    lists:foreach(fun(DDocId0) ->
+    {Success, Fail} = lists:foldl(fun(DDocId0, {Success0, Fail0}) ->
         DDocId = convert_to_design_id(DDocId0),
-        Filt = fun(Idx) ->
-            IsDDoc = mango_idx:ddoc(Idx) == DDocId,
-            JsonType = mango_idx:type(Idx) == <<"json">>,
-            TextType = mango_idx:name(Idx) == <<"text">>,
-            IsDDoc andalso (JsonType orelse TextType)
-        end,
-        mango_idx:delete(Filt, Db, Idxs, DelOpts)
-    end, DDocs),
-    chttpd:send_json(Req, {[{result, <<"Indexes Deleted">>}]});
+        Filt = fun(Idx) -> mango_idx:ddoc(Idx) == DDocId end,
+        Id = {<<"id">>, DDocId},
+        case mango_idx:delete(Filt, Db, Idxs, DelOpts) of
+            {ok, true} ->
+                {[{[Id, {<<"ok">>, true}]} | Success0], Fail0};
+            {error, Error} ->
+                {Success0, [{[Id, {<<"error">>, Error}]} | Fail0]}
+        end
+    end, {[], []}, DDocs),
+    chttpd:send_json(Req, {[{<<"success">>, Success}, {<<"fail">>,
Fail}]});
 
 handle_index_req(#httpd{method='DELETE',
         path_parts=[A, B, <<"_design">>, DDocId0, Type, Name]}=Req, Db) ->
@@ -117,8 +118,14 @@ handle_index_req(#httpd{method='DELETE',
         IsName = mango_idx:name(Idx) == Name,
         IsDDoc andalso IsType andalso IsName
     end,
-    Result = mango_idx:delete(Filt, Db, Idxs, DelOpts),
-    chttpd:send_json(Req, {[{ok, Result}]});
+    case mango_idx:delete(Filt, Db, Idxs, DelOpts) of
+        {ok, true} ->
+            chttpd:send_json(Req, {[{ok, true}]});
+        {error, not_found} ->
+            throw({not_found, missing});
+        {error, Error} ->
+            ?MANGO_ERROR({error_saving_ddoc, Error})
+    end;
 
 handle_index_req(Req, _Db) ->
     chttpd:send_method_not_allowed(Req, "GET,POST,DELETE").

http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/985c952a/src/mango_idx.erl
----------------------------------------------------------------------
diff --git a/src/mango_idx.erl b/src/mango_idx.erl
index 31b29f2..bf81679 100644
--- a/src/mango_idx.erl
+++ b/src/mango_idx.erl
@@ -148,12 +148,12 @@ delete(Filt, Db, Indexes, DelOpts) ->
             end,
             case mango_crud:insert(Db, FinalDDoc, DelOpts) of
                 {ok, _} ->
-                    true;
-                _ ->
-                    ?MANGO_ERROR(error_saving_ddoc)
+                    {ok, true};
+                Error ->
+                    {error, Error}
             end;
         [] ->
-            throw({not_found, missing})
+            {error, not_found}
     end.
 
 

http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/985c952a/test/01-index-crud-test.py
----------------------------------------------------------------------
diff --git a/test/01-index-crud-test.py b/test/01-index-crud-test.py
index adc0ecb..a44376c 100644
--- a/test/01-index-crud-test.py
+++ b/test/01-index-crud-test.py
@@ -169,8 +169,12 @@ class IndexCrudTests(mango.DbPerClass):
             if idx["ddoc"] is not None:
                 docids.append(idx["ddoc"])
 
+        docids.append("_design/this_is_not_an_index_name")
+
         ret = self.db.bulk_delete(docids)
-        assert ret is True
+
+        assert ret["fail"][0]["id"] == "_design/this_is_not_an_index_name"
+        assert len(ret["success"]) == 3
 
         for idx in self.db.list_indexes():
             assert idx["type"] != "json"

http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/985c952a/test/mango.py
----------------------------------------------------------------------
diff --git a/test/mango.py b/test/mango.py
index d1da9d7..b39c916 100644
--- a/test/mango.py
+++ b/test/mango.py
@@ -142,7 +142,7 @@ class Database(object):
         }
         body = json.dumps(body)
         r = self.sess.post(self.path("_index/_bulk_delete"), data=body)
-        return r.json()["result"] == "Indexes Deleted"
+        return r.json()
 
     def find(self, selector, limit=25, skip=0, sort=None, fields=None,
                 r=1, conflicts=False, use_index=None, explain=False,


Mime
View raw message