jchris-couchrest 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/couchrest.rb CHANGED
@@ -28,7 +28,7 @@ require 'couchrest/monkeypatches'
28
28
 
29
29
  # = CouchDB, close to the metal
30
30
  module CouchRest
31
- VERSION = '0.2.1' unless self.const_defined?("VERSION")
31
+ VERSION = '0.2.2' unless self.const_defined?("VERSION")
32
32
 
33
33
  autoload :Server, 'couchrest/core/server'
34
34
  autoload :Database, 'couchrest/core/database'
@@ -40,6 +40,7 @@ module CouchRest
40
40
  autoload :Pager, 'couchrest/helper/pager'
41
41
  autoload :FileManager, 'couchrest/helper/file_manager'
42
42
  autoload :Streamer, 'couchrest/helper/streamer'
43
+ autoload :Upgrade, 'couchrest/helper/upgrade'
43
44
 
44
45
  autoload :ExtendedDocument, 'couchrest/more/extended_document'
45
46
  autoload :CastedModel, 'couchrest/more/casted_model'
@@ -61,12 +61,15 @@ module CouchRest
61
61
  # paramaters as described in http://wiki.apache.org/couchdb/HttpViewApi
62
62
  def view(name, params = {}, &block)
63
63
  keys = params.delete(:keys)
64
- url = CouchRest.paramify_url "#{@uri}/_view/#{name}", params
64
+ name = name.split('/') # I think this will always be length == 2, but maybe not...
65
+ dname = name.shift
66
+ vname = name.join('/')
67
+ url = CouchRest.paramify_url "#{@uri}/_design/#{dname}/_view/#{vname}", params
65
68
  if keys
66
69
  CouchRest.post(url, {:keys => keys})
67
70
  else
68
71
  if block_given?
69
- @streamer.view(name, params, &block)
72
+ @streamer.view("_design/#{dname}/_view/#{vname}", params, &block)
70
73
  else
71
74
  CouchRest.get url
72
75
  end
@@ -74,13 +77,15 @@ module CouchRest
74
77
  end
75
78
 
76
79
  # GET a document from CouchDB, by id. Returns a Ruby Hash.
77
- def get(id)
80
+ def get(id, params = {})
78
81
  slug = escape_docid(id)
79
- hash = CouchRest.get("#{@uri}/#{slug}")
80
- doc = if /^_design/ =~ hash["_id"]
81
- Design.new(hash)
82
+ url = CouchRest.paramify_url("#{@uri}/#{slug}", params)
83
+ result = CouchRest.get(url)
84
+ return result unless result.is_a?(Hash)
85
+ doc = if /^_design/ =~ result["_id"]
86
+ Design.new(result)
82
87
  else
83
- Document.new(hash)
88
+ Document.new(result)
84
89
  end
85
90
  doc.database = self
86
91
  doc
@@ -0,0 +1,51 @@
1
+ module CouchRest
2
+ class Upgrade
3
+ attr_accessor :olddb, :newdb, :dbname
4
+ def initialize dbname, old_couch, new_couch
5
+ @dbname = dbname
6
+ @olddb = old_couch.database dbname
7
+ @newdb = new_couch.database!(dbname)
8
+ @bulk_docs = []
9
+ end
10
+ def clone!
11
+ puts "#{dbname} - #{olddb.info['doc_count']} docs"
12
+ streamer = CouchRest::Streamer.new(olddb)
13
+ streamer.view("_all_docs_by_seq") do |row|
14
+ load_row_docs(row) if row
15
+ maybe_flush_bulks
16
+ end
17
+ flush_bulks!
18
+ end
19
+
20
+ private
21
+
22
+ def maybe_flush_bulks
23
+ flush_bulks! if (@bulk_docs.length > 99)
24
+ end
25
+
26
+ def flush_bulks!
27
+ url = CouchRest.paramify_url "#{@newdb.uri}/_bulk_docs", {:all_or_nothing => true}
28
+ puts "posting #{@bulk_docs.length} bulk docs to #{url}"
29
+ begin
30
+ CouchRest.post url, {:docs => @bulk_docs}
31
+ @bulk_docs = []
32
+ rescue Exception => e
33
+ puts e.response
34
+ raise e
35
+ end
36
+ end
37
+
38
+ def load_row_docs(row)
39
+ results = @olddb.get(row["id"], {:open_revs => "all", :attachments => true})
40
+ results.select{|r|r["ok"]}.each do |r|
41
+ doc = r["ok"]
42
+ if /^_/.match(doc["_id"]) && !/^_design/.match(doc["_id"])
43
+ puts "invalid docid #{doc["_id"]} -- trimming"
44
+ doc["_id"] = doc["_id"].sub('_','')
45
+ end
46
+ doc.delete('_rev')
47
+ @bulk_docs << doc
48
+ end
49
+ end
50
+ end
51
+ end
@@ -70,6 +70,7 @@ describe CouchRest::CastedModel do
70
70
 
71
71
  describe "saved document with casted models" do
72
72
  before(:each) do
73
+ reset_test_db!
73
74
  @obj = DummyModel.new(:casted_attribute => {:name => 'whatever'})
74
75
  @obj.save.should be_true
75
76
  @obj = DummyModel.get(@obj.id)
@@ -94,4 +95,4 @@ describe CouchRest::CastedModel do
94
95
 
95
96
  end
96
97
 
97
- end
98
+ end
@@ -4,6 +4,7 @@ describe "ExtendedDocument attachments" do
4
4
 
5
5
  describe "#has_attachment?" do
6
6
  before(:each) do
7
+ reset_test_db!
7
8
  @obj = Basic.new
8
9
  @obj.save.should == true
9
10
  @file = File.open(FIXTURE_PATH + '/attachments/test.html')
@@ -126,4 +127,4 @@ describe "ExtendedDocument attachments" do
126
127
  @obj.attachment_url(@attachment_name).should == "#{Basic.database}/#{@obj.id}/#{@attachment_name}"
127
128
  end
128
129
  end
129
- end
130
+ end
@@ -190,6 +190,7 @@ describe "ExtendedDocument views" do
190
190
 
191
191
  describe "with a lot of designs left around" do
192
192
  before(:each) do
193
+ reset_test_db!
193
194
  Article.by_date
194
195
  Article.view_by :field
195
196
  Article.by_field
@@ -203,4 +204,4 @@ describe "ExtendedDocument views" do
203
204
  ddocs["rows"].length.should == 1
204
205
  end
205
206
  end
206
- end
207
+ end
@@ -8,6 +8,7 @@ require File.join(FIXTURE_PATH, 'more', 'event')
8
8
  describe "ExtendedDocument properties" do
9
9
 
10
10
  before(:each) do
11
+ reset_test_db!
11
12
  @card = Card.new(:first_name => "matt")
12
13
  end
13
14
 
@@ -126,4 +127,4 @@ describe "ExtendedDocument properties" do
126
127
  end
127
128
  end
128
129
 
129
- end
130
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jchris-couchrest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - J. Chris Anderson
@@ -89,6 +89,7 @@ files:
89
89
  - lib/couchrest/helper
90
90
  - lib/couchrest/helper/pager.rb
91
91
  - lib/couchrest/helper/streamer.rb
92
+ - lib/couchrest/helper/upgrade.rb
92
93
  - lib/couchrest/mixins
93
94
  - lib/couchrest/mixins/attachments.rb
94
95
  - lib/couchrest/mixins/callbacks.rb
@@ -149,26 +150,6 @@ files:
149
150
  - spec/fixtures/attachments/couchdb.png
150
151
  - spec/fixtures/attachments/README
151
152
  - spec/fixtures/attachments/test.html
152
- - spec/fixtures/couchapp
153
- - spec/fixtures/couchapp/_attachments
154
- - spec/fixtures/couchapp/_attachments/index.html
155
- - spec/fixtures/couchapp/doc.json
156
- - spec/fixtures/couchapp/foo
157
- - spec/fixtures/couchapp/foo/bar.txt
158
- - spec/fixtures/couchapp/foo/test.json
159
- - spec/fixtures/couchapp/test.json
160
- - spec/fixtures/couchapp/views
161
- - spec/fixtures/couchapp/views/example-map.js
162
- - spec/fixtures/couchapp/views/example-reduce.js
163
- - spec/fixtures/couchapp-test
164
- - spec/fixtures/couchapp-test/my-app
165
- - spec/fixtures/couchapp-test/my-app/_attachments
166
- - spec/fixtures/couchapp-test/my-app/_attachments/index.html
167
- - spec/fixtures/couchapp-test/my-app/foo
168
- - spec/fixtures/couchapp-test/my-app/foo/bar.txt
169
- - spec/fixtures/couchapp-test/my-app/views
170
- - spec/fixtures/couchapp-test/my-app/views/example-map.js
171
- - spec/fixtures/couchapp-test/my-app/views/example-reduce.js
172
153
  - spec/fixtures/more
173
154
  - spec/fixtures/more/article.rb
174
155
  - spec/fixtures/more/card.rb