Sha256: 005c562425250089b3be91f5d3c177b1458b131fa84bdff3f4a7913c5d6e4791

Contents?: true

Size: 784 Bytes

Versions: 4

Compression:

Stored size: 784 Bytes

Contents

module ChronoModel

  module Utilities
    # 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
    #
    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
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
chrono_model-1.2.2 lib/chrono_model/utilities.rb
chrono_model-1.2.1 lib/chrono_model/utilities.rb
chrono_model-1.2.0 lib/chrono_model/utilities.rb
chrono_model-1.1.0 lib/chrono_model/utilities.rb