couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From iil...@apache.org
Subject [1/2] couch commit: updated refs/heads/master to 514a447
Date Wed, 18 May 2016 14:47:03 GMT
Repository: couchdb-couch
Updated Branches:
  refs/heads/master 23f188096 -> 514a44775


Support create_if_missing option in couch_db:open


Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch/commit/9c349b9b
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch/tree/9c349b9b
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch/diff/9c349b9b

Branch: refs/heads/master
Commit: 9c349b9b18456bc75486defb87b205c38da4a05d
Parents: 23f1880
Author: ILYA Khlopotov <iilyak@ca.ibm.com>
Authored: Tue May 17 16:44:13 2016 -0700
Committer: ILYA Khlopotov <iilyak@ca.ibm.com>
Committed: Tue May 17 16:44:13 2016 -0700

----------------------------------------------------------------------
 src/couch_server.erl    |  4 ++++
 test/couch_db_tests.erl | 19 +++++++++++++++++++
 2 files changed, 23 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/9c349b9b/src/couch_server.erl
----------------------------------------------------------------------
diff --git a/src/couch_server.erl b/src/couch_server.erl
index b9a5077..2634bbf 100644
--- a/src/couch_server.erl
+++ b/src/couch_server.erl
@@ -81,10 +81,14 @@ open(DbName, Options0) ->
         {ok, Db#db{user_ctx=Ctx, fd_monitor=erlang:monitor(process,Fd)}};
     _ ->
         Timeout = couch_util:get_value(timeout, Options, infinity),
+        Create = couch_util:get_value(create_if_missing, Options, false),
         case gen_server:call(couch_server, {open, DbName, Options}, Timeout) of
         {ok, #db{fd=Fd} = Db} ->
             update_lru(DbName, Options),
             {ok, Db#db{user_ctx=Ctx, fd_monitor=erlang:monitor(process,Fd)}};
+        {not_found, no_db_file} when Create ->
+            couch_log:warning("creating missing database: ~s", [DbName]),
+            couch_server:create(DbName, Options);
         Error ->
             Error
         end

http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/9c349b9b/test/couch_db_tests.erl
----------------------------------------------------------------------
diff --git a/test/couch_db_tests.erl b/test/couch_db_tests.erl
index f614102..c57a0d4 100644
--- a/test/couch_db_tests.erl
+++ b/test/couch_db_tests.erl
@@ -39,6 +39,18 @@ create_delete_db_test_()->
         }
     }.
 
+open_db_test_()->
+    {
+        "Database open tests",
+        {
+            setup,
+            fun setup/0, fun test_util:stop_couch/1,
+            fun(_) ->
+                [should_create_db_if_missing()]
+            end
+        }
+    }.
+
 
 should_create_db() ->
     DbName = ?tempdb(),
@@ -98,6 +110,13 @@ should_create_delete_database_continuously() ->
                            ?_assert(loop(DbName, N))}}
      || N <- [10, 100]].
 
+should_create_db_if_missing() ->
+    DbName = ?tempdb(),
+    {ok, Db} = couch_db:open(DbName, [{create_if_missing, true}]),
+    ok = couch_db:close(Db),
+    {ok, AllDbs} = couch_server:all_databases(),
+    ?_assert(lists:member(DbName, AllDbs)).
+
 loop(_, 0) ->
     true;
 loop(DbName, N) ->


Mime
View raw message