On Wed, Aug 10, 2011 at 3:45 PM, <[email protected]> wrote: > 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), > > >
I can't reproduce this bug with curl, is there something I'm missing? $ curl -XPUT -H "Content-Type: application/json" http://127.0.0.1:5984/foo {"ok":true} $ curl -XPUT -H "Content-Type: application/json" http://127.0.0.1:5984/foo/bar -d '{"hi": true}' {"ok":true,"id":"bar","rev":"1-e7e9148335bb3e3d0f04263e57edef01"} $ curl -XPUT -H "Content-Type: application/json" http://127.0.0.1:5984/foo/bar2 -d '{"hi": true}' {"ok":true,"id":"bar2","rev":"1-e7e9148335bb3e3d0f04263e57edef01"} $ curl -XPUT -H "Content-Type: application/json" http://127.0.0.1:5984/foo/bar23 -d '{"hi": true}' {"ok":true,"id":"bar23","rev":"1-e7e9148335bb3e3d0f04263e57edef01"} $ curl -XPOST -H "Content-Type: application/json" http://127.0.0.1:5984/foo/_ensure_full_commit {"ok":true,"instance_start_time":"1313013146583992"} $ curl -XPOST -H "Content-Type: application/json" http://127.0.0.1:5984/_restart curl: (52) Empty reply from server $ curl -XPUT -H "Content-Type: application/json" http://127.0.0.1:5984/foo {"error":"file_exists","reason":"The database could not be created, the file already exists."}
