Deleting data based on CouchDB model in Rails

I want to set up a rake task to populate my CouchDB with hardware data and get rid of the data in the database first. A good example for the whole procedure using MySQL can be found here . These examples contain the following part for deleting model data in MySQL database:

# the list of models (pluralized) you want to import, in order
models = ['Cities','Neighborhoods','Shops','Reviews']

# truncate existing tables
models.reverse.each { |model| ActiveRecord::Base.connection.execute("truncate table #{model.underscore}") }

      

What would be the equivalent for CouchDB? Should I take a completely different approach since this concept, created for relational databases, cannot be applied to document oriented databases?

Thank you very much in advance!

+2


source to share


2 answers


There are no tables and schemas, so nothing needs to be trimmed. Instead, you should delete documents. But it is not possible to do things like "remove from" so that you can fetch documents and then remove them using the HTTP Bulk Document API .

I guess this is necessary for a development environment, so there shouldn't be a lot of documents and this solution will be acceptable.



Also, you can get first documents, bulk delete, and then get another group of documents, etc. Repeat for each model. Hope it helps.

+1


source


I think the easiest way to get rid of all documents in your database is to simply drop the database and then re-create it. Does your problem solve? You probably want to have an offline copy of the project documents so you can check them back after re-creating.

I don't know how one can simply delete documents from the CouchDB database.



Ah - so that you only want to delete documents belonging to one specific model? In this case, I think you will have to iterate over the view (either a specific view that iterates over documents belonging to that model, or _all_docs) and delete one document at a time.

0


source







All Articles