lib/dcmgr/models/base_new.rb in wakame-vdc-agents-10.12.0 vs lib/dcmgr/models/base_new.rb in wakame-vdc-agents-11.06.0

- old
+ new

@@ -2,13 +2,10 @@ require 'sequel/model' module Dcmgr::Models - class InvalidUUIDError < StandardError; end - class UUIDPrefixDuplication < StandardError; end - # Sequal::Model plugin to inject the Taggable feature to the model # class. # # Taggable model supports the features below: # - Taggable.uuid_prefix to both set and get uuid_prefix for the model. @@ -185,10 +182,15 @@ return p_uuid.sub(regex, '') end raise InvalidUUIDError, "Invalid uuid or unsupported uuid: #{p_uuid} in #{self}" end + # Checks the general uuid syntax + def check_trimmed_uuid_format(uuid) + uuid.match(/^[a-z0-9 ]*$/) && uuid.length <= 8 + end + # Checks the uuid syntax if it is for the Taggable class. def check_uuid_format(uuid) uuid =~ /^#{self.uuid_prefix}-/ end end @@ -395,15 +397,22 @@ end class BaseNew < Sequel::Model LOCK_TABLES_KEY='__locked_tables' + + def self.default_row_lock_mode=(mode) + raise ArgumentError unless [nil, :share, :update].member?(mode) + @default_row_lock_mode = mode + end - def self.lock! + def self.lock!(mode=nil) + raise ArgumentError unless [nil, :share, :update].member?(mode) + mode ||= @default_row_lock_mode locktbls = Thread.current[LOCK_TABLES_KEY] if locktbls - locktbls[self.db.uri.to_s + @dataset.first_source_alias.to_s]=1 + locktbls[self.db.uri.to_s + @dataset.first_source_alias.to_s]=mode end end def self.unlock! locktbls = Thread.current[LOCK_TABLES_KEY] @@ -412,12 +421,13 @@ end end def self.dataset locktbls = Thread.current[LOCK_TABLES_KEY] - if locktbls && locktbls[self.db.uri.to_s + @dataset.first_source_alias.to_s] - @dataset.opts = @dataset.opts.merge({:lock=>:update}) + if locktbls && (mode = locktbls[self.db.uri.to_s + @dataset.first_source_alias.to_s]) + # lock mode: :share or :update + @dataset.opts = @dataset.opts.merge({:lock=>mode}) else @dataset.opts = @dataset.opts.merge({:lock=>nil}) end @dataset end @@ -437,9 +447,13 @@ end end s end + # Returns true if this Model has time stamps + def with_timestamps? + self.columns.include?(:created_at) && self.columns.include?(:updated_at) + end # Callback when the initial data is setup to the database. def self.install_data install_data_hooks.each{|h| h.call } end