Class: Artery::ActiveRecord::ModelInfo

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/artery/active_record/model_info.rb

Class Method Summary collapse

Class Method Details

.acquire_lock!(model_name) ⇒ Object

Deprecated.

No longer used. Publishing is handled by Publisher.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/artery/active_record/model_info.rb', line 12

def self.acquire_lock!(model_name)
  lock_row = lock('FOR UPDATE').find_by(model: model_name)
  return lock_row if lock_row

  begin
    create!(model: model_name, latest_index: Artery.message_class.latest_index(model_name))
  rescue ::ActiveRecord::RecordNotUnique
    # concurrent insert — fine
  end

  lock('FOR UPDATE').find_by!(model: model_name)
end

.ensure_initialized!(model_name) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/artery/active_record/model_info.rb', line 25

def self.ensure_initialized!(model_name)
  row = find_or_create_by!(model: model_name) do |r|
    r.latest_index = Artery.message_class.latest_index(model_name)
  end

  row.update!(last_published_id: row.latest_index) if row.last_published_id.zero? && row.latest_index.positive?

  row
end

.last_published_id_for(model_name) ⇒ Object



35
36
37
# File 'lib/artery/active_record/model_info.rb', line 35

def self.last_published_id_for(model_name)
  where(model: model_name).limit(1).pluck(:last_published_id).first.to_i
end