couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fdman...@apache.org
Subject svn commit: r1156360 - in /couchdb/trunk: share/www/script/test/basics.js src/couchdb/couch_server.erl
Date Wed, 10 Aug 2011 20:45:53 GMT
Author: fdmanana
Date: Wed Aug 10 20:45:53 2011
New Revision: 1156360

URL: http://svn.apache.org/viewvc?rev=1156360&view=rev
Log:
Prevent data loss on db creation request
    
1) Create and populate a database
2) Restart the server
3) Send a PUT request to create the database - the server
   will override the existing file, making all previous
   documents no longer accessible nor recoverable


Modified:
    couchdb/trunk/share/www/script/test/basics.js
    couchdb/trunk/src/couchdb/couch_server.erl

Modified: couchdb/trunk/share/www/script/test/basics.js
URL: http://svn.apache.org/viewvc/couchdb/trunk/share/www/script/test/basics.js?rev=1156360&r1=1156359&r2=1156360&view=diff
==============================================================================
--- couchdb/trunk/share/www/script/test/basics.js (original)
+++ couchdb/trunk/share/www/script/test/basics.js Wed Aug 10 20:45:53 2011
@@ -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);
 };

Modified: couchdb/trunk/src/couchdb/couch_server.erl
URL: http://svn.apache.org/viewvc/couchdb/trunk/src/couchdb/couch_server.erl?rev=1156360&r1=1156359&r2=1156360&view=diff
==============================================================================
--- couchdb/trunk/src/couchdb/couch_server.erl (original)
+++ couchdb/trunk/src/couchdb/couch_server.erl Wed Aug 10 20:45:53 2011
@@ -316,11 +316,13 @@ handle_call({open, DbName, Options}, {Fr
         {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),



Mime
View raw message