Author: fdmanana Date: Wed Aug 10 20:48:17 2011 New Revision: 1156362 URL: http://svn.apache.org/viewvc?rev=1156362&view=rev Log: Merged revision 1156360 from trunk
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/branches/1.0.x/share/www/script/test/basics.js couchdb/branches/1.0.x/src/couchdb/couch_server.erl Modified: couchdb/branches/1.0.x/share/www/script/test/basics.js URL: http://svn.apache.org/viewvc/couchdb/branches/1.0.x/share/www/script/test/basics.js?rev=1156362&r1=1156361&r2=1156362&view=diff ============================================================================== --- couchdb/branches/1.0.x/share/www/script/test/basics.js (original) +++ couchdb/branches/1.0.x/share/www/script/test/basics.js Wed Aug 10 20:48:17 2011 @@ -248,4 +248,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/branches/1.0.x/src/couchdb/couch_server.erl URL: http://svn.apache.org/viewvc/couchdb/branches/1.0.x/src/couchdb/couch_server.erl?rev=1156362&r1=1156361&r2=1156362&view=diff ============================================================================== --- couchdb/branches/1.0.x/src/couchdb/couch_server.erl (original) +++ couchdb/branches/1.0.x/src/couchdb/couch_server.erl Wed Aug 10 20:48:17 2011 @@ -308,11 +308,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),