Module: ChronoModel::Utilities

Defined in:
lib/chrono_model/utilities.rb

Instance Method Summary collapse

Instance Method Details

#amend_period!(hid, from, to) ⇒ Object

Amends the given history item setting a different period. Useful when migrating from legacy systems.

To use it, extend AR::Base with ChronoModel::Utilities

ActiveRecord::Base.instance_eval do
  extend ChronoModel::Utilities
end


14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/chrono_model/utilities.rb', line 14

def amend_period!(hid, from, to)
  unless [from, to].any? { |ts| ts.respond_to?(:zone) && ts.zone == 'UTC' }
    raise 'Can amend history only with UTC timestamps'
  end

  connection.execute %[
    UPDATE #{quoted_table_name}
       SET "validity" = tsrange(#{connection.quote(from)}, #{connection.quote(to)}),
           "recorded_at" = #{connection.quote(from)}
     WHERE "hid" = #{hid.to_i}
  ]
end