Sha256: eb5eebca0d950ccbf595824adfaf1a9b04703fe5768097422063315b9e5ff515

Contents?: true

Size: 815 Bytes

Versions: 2

Compression:

Stored size: 815 Bytes

Contents

# frozen_string_literal: true

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

2 entries across 2 versions & 1 rubygems

Version Path
chrono_model-3.0.1 lib/chrono_model/utilities.rb
chrono_model-2.0.0 lib/chrono_model/utilities.rb