summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--share/www/script/test/basics.js19
-rw-r--r--src/couchdb/couch_server.erl12
2 files changed, 26 insertions, 5 deletions
diff --git a/share/www/script/test/basics.js b/share/www/script/test/basics.js
index 30c27c11..5dcf9fa9 100644
--- a/share/www/script/test/basics.js
+++ b/share/www/script/test/basics.js
@@ -246,4 +246,23 @@ couchTests.basics = function(debug) {
result = JSON.parse(xhr.responseText);
TEquals("bad_request", result.error);
TEquals("You tried to DELETE a database with a ?=rev parameter. Did you mean to DELETE a document instead?", result.reason);
+
+ // On restart, a request for creating a database that already exists can
+ // not override the existing database file
+ db = new CouchDB("test_suite_foobar");
+ db.deleteDb();
+ xhr = CouchDB.request("PUT", "/" + db.name);
+ TEquals(201, xhr.status);
+
+ TEquals(true, db.save({"_id": "doc1"}).ok);
+ TEquals(true, db.ensureFullCommit().ok);
+
+ TEquals(1, db.info().doc_count);
+
+ restartServer();
+
+ xhr = CouchDB.request("PUT", "/" + db.name);
+ TEquals(412, xhr.status);
+
+ TEquals(1, db.info().doc_count);
};
diff --git a/src/couchdb/couch_server.erl b/src/couchdb/couch_server.erl
index 7870d69e..b9503d2f 100644
--- a/src/couchdb/couch_server.erl
+++ b/src/couchdb/couch_server.erl
@@ -314,11 +314,13 @@ handle_call({open, DbName, Options}, {FromPid,_}=From, Server) ->
{reply, couch_db:open_ref_counted(MainPid, FromPid), Server}
end;
handle_call({create, DbName, Options}, From, Server) ->
- case ets:lookup(couch_dbs_by_name, DbName) of
- [] ->
- open_db(DbName, Server, [create | Options], From);
- [_AlreadyRunningDb] ->
- {reply, file_exists, Server}
+ FileName = get_full_filename(Server, ?b2l(DbName)),
+ case file:open(FileName, [read]) of
+ {ok, Fd} ->
+ ok = file:close(Fd),
+ {reply, file_exists, Server};
+ Error ->
+ open_db(DbName, Server, [create | Options], From)
end;
handle_call({delete, DbName, _Options}, _From, Server) ->
DbNameList = binary_to_list(DbName),