From 9c349b9b18456bc75486defb87b205c38da4a05d Mon Sep 17 00:00:00 2001 From: ILYA Khlopotov Date: Tue, 17 May 2016 16:44:13 -0700 Subject: [PATCH] Support create_if_missing option in couch_db:open --- src/couch_server.erl | 4 ++++ test/couch_db_tests.erl | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/couch_server.erl b/src/couch_server.erl index b9a50772..2634bbff 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 diff --git a/test/couch_db_tests.erl b/test/couch_db_tests.erl index f614102b..c57a0d49 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) ->