fix(counter): use couchrest_database, proper begin/rescue instead of inline rescue nil
- Order.database returns CouchPotato::Database, not CouchRest::Database - Fix: use .couchrest_database for raw CouchRest access - Replace fragile 'rescue nil' inline with explicit begin/rescue - Simplify YAML.safe_load (remove unnecessary permitted_classes)
This commit is contained in:
+11
-14
@@ -33,17 +33,17 @@ module Mozo
|
|||||||
def self.provision!
|
def self.provision!
|
||||||
require 'couchrest'
|
require 'couchrest'
|
||||||
|
|
||||||
db = Order.database
|
# CouchPotato::Database wraps CouchRest — get the raw DB for direct view queries
|
||||||
|
db = Order.database.couchrest_database
|
||||||
design = '_design/order_counter'
|
design = '_design/order_counter'
|
||||||
view = 'by_supplier_id_and_state'
|
view = 'by_supplier_id_and_state'
|
||||||
|
|
||||||
# Ensure the design doc exists
|
# Ensure the design doc exists (auto-create if missing)
|
||||||
unless db.get(design) rescue nil
|
begin
|
||||||
|
db.get(design)
|
||||||
|
rescue RestClient::ResourceNotFound, CouchRest::NotFound
|
||||||
puts "[Counter.provision!] Creating design doc #{design}"
|
puts "[Counter.provision!] Creating design doc #{design}"
|
||||||
doc = YAML.safe_load(
|
doc = YAML.safe_load(File.read(Rails.root.join('drb_counter/couchdb_design.yml')))
|
||||||
File.read(Rails.root.join('drb_counter/couchdb_design.yml')),
|
|
||||||
permitted_classes: [Symbol]
|
|
||||||
)
|
|
||||||
doc['_id'] = design
|
doc['_id'] = design
|
||||||
doc['language'] ||= 'javascript'
|
doc['language'] ||= 'javascript'
|
||||||
db.save_doc(doc)
|
db.save_doc(doc)
|
||||||
@@ -59,14 +59,11 @@ module Mozo
|
|||||||
supplier_id, state = row['key']
|
supplier_id, state = row['key']
|
||||||
count = row['value']
|
count = row['value']
|
||||||
|
|
||||||
case state
|
key = case state
|
||||||
when 'placed'
|
when 'placed' then "supplier_counter:#{supplier_id}:orders_placed"
|
||||||
key = "supplier_counter:#{supplier_id}:orders_placed"
|
when 'active' then "supplier_counter:#{supplier_id}:orders_in_process"
|
||||||
when 'active'
|
|
||||||
key = "supplier_counter:#{supplier_id}:orders_in_process"
|
|
||||||
else
|
|
||||||
next
|
|
||||||
end
|
end
|
||||||
|
next unless key
|
||||||
|
|
||||||
set(key, count)
|
set(key, count)
|
||||||
puts " #{key} = #{count}"
|
puts " #{key} = #{count}"
|
||||||
|
|||||||
Reference in New Issue
Block a user