couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rnew...@apache.org
Subject [1/2] fabric commit: updated refs/heads/master to 03c927b
Date Tue, 22 Sep 2015 12:56:36 GMT
Repository: couchdb-fabric
Updated Branches:
  refs/heads/master cad0918d8 -> 03c927ba4


Check if a DB exists prior to creating

COUCHDB-2819


Project: http://git-wip-us.apache.org/repos/asf/couchdb-fabric/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-fabric/commit/61fbb94b
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-fabric/tree/61fbb94b
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-fabric/diff/61fbb94b

Branch: refs/heads/master
Commit: 61fbb94b9f7f59c34483c092ecc39bd3a63155e2
Parents: e46ab8f
Author: Sam Smith <sam.smith@cloudant.com>
Authored: Mon Sep 21 16:05:16 2015 +0100
Committer: Sam Smith <sam.smith@cloudant.com>
Committed: Tue Sep 22 13:49:22 2015 +0100

----------------------------------------------------------------------
 src/fabric_db_create.erl | 51 +++++++++++++++++++++++++++++++++++++------
 1 file changed, 44 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-fabric/blob/61fbb94b/src/fabric_db_create.erl
----------------------------------------------------------------------
diff --git a/src/fabric_db_create.erl b/src/fabric_db_create.erl
index ffbf78a..dc1c0cc 100644
--- a/src/fabric_db_create.erl
+++ b/src/fabric_db_create.erl
@@ -23,14 +23,19 @@
 go(DbName, Options) ->
     case validate_dbname(DbName, Options) of
     ok ->
-        {Shards, Doc} = generate_shard_map(DbName, Options),
-        case {create_shard_files(Shards), create_shard_db_doc(Doc)} of
-        {ok, {ok, Status}} ->
-            Status;
-        {file_exists, {ok, _}} ->
+        case db_exists(DbName) of
+        true ->
             {error, file_exists};
-        {_, Error} ->
-            Error
+        false ->
+            {Shards, Doc} = generate_shard_map(DbName, Options),
+            case {create_shard_files(Shards), create_shard_db_doc(Doc)} of
+            {ok, {ok, Status}} ->
+                Status;
+            {file_exists, {ok, _}} ->
+                {error, file_exists};
+            {_, Error} ->
+                Error
+            end
         end;
     Error ->
         Error
@@ -155,3 +160,35 @@ make_document([#shard{dbname=DbName}|_] = Shards, Suffix) ->
         {<<"by_range">>, {[{K,lists:sort(V)} || {K,V} <- ByRangeOut]}}
     ]}}.
 
+db_exists(DbName) -> is_list(catch mem3:shards(DbName)).
+
+-ifdef(TEST).
+-include_lib("eunit/include/eunit.hrl").
+
+db_exists_for_existing_db_test() ->
+    start_meck_(),
+    Mock = fun(DbName) when is_binary(DbName) ->
+        [#shard{dbname = DbName, range = [0,100]}]
+    end,
+    ok = meck:expect(mem3, shards, Mock),
+    ?assertEqual(db_exists(<<"foobar">>), true),
+    ?assertEqual(meck:validate(mem3), true),
+    stop_meck_().
+
+db_exists_for_missing_db_test() ->
+    start_meck_(),
+    Mock = fun(DbName) ->
+        erlang:error(database_does_not_exist, DbName)
+    end,
+    ok = meck:expect(mem3, shards, Mock),
+    ?assertEqual(db_exists(<<"foobar">>), false),
+    ?assertEqual(meck:validate(mem3), false),
+    stop_meck_().
+
+start_meck_() ->
+    ok = meck:new(mem3).
+
+stop_meck_() ->
+    ok = meck:unload(mem3).
+
+-endif.


Mime
View raw message