After working with OptionParser a bit, I settle on this updated command line interface for my couch_docs gem:
cstrom@whitefall:~/repos/couch_docs$ ./bin/couch-docs -hThe most important of these options for me is the
Usage: couch-docs push|dump [OPTIONS] couchdb_url [target_dir]
If a target_dir is not specified, the current working directory will be used.
Push options:
-R, --destructive Drop the couchdb_uri (if it exists) and create a new database
-b, --bulk [BATCH_SIZE=1000] Use bulk insert when pushing new documents
Dump options:
-d, --design Only dump design documents
-D, --data Only dump data documents
Common options:
-v, --version Show version
-h, --help Show this message
--destructive
option. It is a pain to have to drop/create databases manually when I am trying to test things out. Happily this is trivial to implement—thanks to the extreme RESTful nature of CouchDB. Specifically, creating a database is accomplished with the same PUT operation that is used to create a document. The couch_docs gem already has a facility for creating documents and for replacing existing documents as needed—the Store#put!
method.And so I do something I have yet to do in this incarnation of my chain, I write an RSpec spec:
describe CouchDocs doSimple enough. When I do a destructive database create, I tell the
it "should be able to create (or delete/create) a DB" do
Store.
should_receive(:put!).
with("couchdb_url")
CouchDocs.destructive_database_create("couchdb_url")
end
#...
end
Store
class to perform a (possibly) destructive put. Running my spec, I get a failure because I have not defined the destructive_database_create
method:cstrom@whitefall:~/repos/couch_docs$ spec ./spec/couch_docs_spec.rbAh, good to be back in the change-the-message, make it pass cycle. After getting the spec to pass, I give it a try from the command line only to find:
F..........................
1)
NoMethodError in 'CouchDocs should be able to create (or delete/create) a DB'
undefined method `destructive_database_create' for CouchDocs:Module
./spec/couch_docs_spec.rb:9:
Finished in 0.077468 seconds
27 examples, 1 failure
cstrom@whitefall:~/repos/couch_docs$ ./bin/couch-docs -R push http://localhost:5984/test ~/repos/eee-code/seed/seed2/Ah, bummer. All of my PUTs so far have been of a document to an existing database. When creating a new database, there is no document.
/home/cstrom/repos/couch_docs/lib/couch_docs.rb:63:in `put!': wrong number of arguments (1 for 2) (ArgumentError)
from /home/cstrom/repos/couch_docs/lib/couch_docs.rb:63:in `destructive_database_create'
from /home/cstrom/repos/couch_docs/lib/couch_docs/command_line.rb:26:in `run'
from /home/cstrom/repos/couch_docs/lib/couch_docs/command_line.rb:10:in `run'
from ./bin/couch-docs:8
It turns out that I can put anything I like when creating a database and CouchDB will happily ignore it for for me:
cstrom@whitefall:~/repos/couch_docs$ curl -X DELETE http://localhost:5984/testI do not see any reason to expect that CouchDB will change this behavior, so I code to it:
{"ok":true}
cstrom@whitefall:~/repos/couch_docs$ curl -X PUT http://localhost:5984/test -d '{"foo":"bar"}'
{"ok":true}
cstrom@whitefall:~/repos/couch_docs$ curl -X GET http://localhost:5984/test/_all_docs
{"total_rows":0,"offset":0,"rows":[]}
describe CouchDocs doThe
it "should be able to create (or delete/create) a DB" do
Store.
should_receive(:put!).
with("couchdb_url", anything())
CouchDocs.destructive_database_create("couchdb_url")
end
#...
end
anything
RSpec matcher means that I expect the put!
method to receive a second argument, but really do not care what it is. The implementation that makes this pass is:# Create or recreate the database located at <tt>db_uri</tt>To test this out, I install my gem and run it in a seed directory with 4 meal/recipe documents from 2002:
def self.destructive_database_create(db_uri)
Store.put!(db_uri, "")
end
cstrom@whitefall:~/tmp/seed$ lsAnd, checking the test database in Futon, I find that I do indeed have 4 documents now:
2002-08-26-grilled_chicken.json 2002-08-26.json 2002-08-26-pasta.json 2002-08-26-pesto.json
cstrom@whitefall:~/tmp/seed$ couch-docs -R push http://localhost:5984/test
That is a good stopping point for tonight. I will pick up tomorrow by being more selective in what I dump to the filesystem (i.e. only design documents, only data, or both).
Day #28