summaryrefslogtreecommitdiff
path: root/lib/tapicero
diff options
context:
space:
mode:
Diffstat (limited to 'lib/tapicero')
-rw-r--r--lib/tapicero/user_database.rb53
-rw-r--r--lib/tapicero/user_event_handler.rb44
2 files changed, 74 insertions, 23 deletions
diff --git a/lib/tapicero/user_database.rb b/lib/tapicero/user_database.rb
index 3cfdd70..26d013d 100644
--- a/lib/tapicero/user_database.rb
+++ b/lib/tapicero/user_database.rb
@@ -4,16 +4,8 @@ require 'json'
module Tapicero
class UserDatabase
- def initialize(host, name)
- @host = host
- @name = name
- end
-
- def prepare(config)
- create
- secure(config.options[:security])
- add_design_docs
- Tapicero.logger.info "Prepared storage " + name
+ def initialize(user_id)
+ @db = couch.database(config.options[:db_prefix] + user_id)
end
def create
@@ -22,7 +14,8 @@ module Tapicero
end
end
- def secure(security)
+ def secure(security = nil)
+ security ||= config.options[:security]
# let's not overwrite if we have a security doc already
return if secured? && !Tapicero::FLAGS.include?('--overwrite-security')
retry_request_once "Writing security to" do
@@ -42,9 +35,9 @@ module Tapicero
end
def upload_design_doc(file)
- url = design_url(file.basename('.json'))
- CouchRest.put url, JSON.parse(file.read)
- rescue RestClient::Conflict
+ old = CouchRest.get design_url(file.basename('.json'))
+ rescue RestClient::ResourceNotFound
+ CouchRest.put design_url(file.basename('.json')), JSON.parse(file.read)
end
@@ -54,28 +47,33 @@ module Tapicero
end
end
+ def name
+ db.name
+ end
+
protected
def create_db
- CouchRest.new(host).create_db(name)
- rescue RestClient::PreconditionFailed # database already existed
+ db.info # test if db exists
+ rescue RestClient::ResourceNotFound
+ couch.create_db(db.name)
end
def delete_db
- db = CouchRest.new(host).database(name)
db.delete! if db
rescue RestClient::ResourceNotFound # no database found
end
def retry_request_once(action)
second_try ||= false
- Tapicero.logger.debug "#{action} #{name}"
+ Tapicero.logger.debug "#{action} #{db.name}"
yield
rescue RestClient::Exception => exc
+ raise exc if Tapicero::RERAISE
if second_try
- log_error "#{action} #{name} failed twice due to:", exc
+ log_error "#{action} #{db.name} failed twice due to:", exc
else
- log_error "#{action} #{name} failed due to:", exc
+ log_error "#{action} #{db.name} failed due to:", exc
sleep 5
second_try = true
retry
@@ -95,13 +93,22 @@ module Tapicero
end
def security_url
- "#{host}/#{name}/_security"
+ db.root + "/_security"
end
def design_url(doc_name)
- "#{host}/#{name}/_design/#{doc_name}"
+ db.root + "/_design/#{doc_name}"
+ end
+
+ attr_reader :db
+
+ def couch
+ @couch ||= CouchRest.new(config.couch_host)
+ end
+
+ def config
+ Tapicero.config
end
- attr_reader :host, :name
end
end
diff --git a/lib/tapicero/user_event_handler.rb b/lib/tapicero/user_event_handler.rb
new file mode 100644
index 0000000..38cf8f8
--- /dev/null
+++ b/lib/tapicero/user_event_handler.rb
@@ -0,0 +1,44 @@
+require 'tapicero/user_database'
+module Tapicero
+ class UserEventHandler
+ def initialize(users)
+ users.created do |hash|
+ prepare_db(hash['id'])
+ end
+
+ # Sometimes changes log starts with rev 2. So the
+ # detection of is currently not working properly
+ # Working around this until a new version of
+ # couchrest changes takes this into account.
+ users.updated do |hash|
+ prepare_db(hash['id'])
+ end
+
+ users.deleted do |hash|
+ destroy_db(hash['id'])
+ end
+ end
+
+ protected
+
+ def prepare_db(id)
+ db = user_database(id)
+ db.create
+ db.secure
+ db.add_design_docs
+ logger.info "Prepared storage " + db.name
+ end
+
+ def destroy_db(id)
+ user_database(id).destroy
+ end
+
+ def logger
+ Tapicero.logger
+ end
+
+ def user_database(id)
+ UserDatabase.new(id)
+ end
+ end
+end