From 545fbbf31162fa5d63e0fad15490a89dc83abf16 Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Thu, 22 Feb 2024 17:03:32 +0100 Subject: [PATCH] [WIP] Remove TS Range override - TODO: Reasons - TODO: Warn about the potential breaking change - TODO: Bump major version Close #267 --- .rubocop_todo.yml | 1 - lib/chrono_model/adapter.rb | 2 - lib/chrono_model/adapter/tsrange.rb | 72 ------------------- .../time_machine/history_model.rb | 4 ++ 4 files changed, 4 insertions(+), 75 deletions(-) delete mode 100644 lib/chrono_model/adapter/tsrange.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index e23a03b..c98c4a6 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -40,7 +40,6 @@ Metrics/PerceivedComplexity: Naming/MethodParameterName: Exclude: - 'lib/chrono_model/adapter/ddl.rb' - - 'lib/chrono_model/adapter/tsrange.rb' - 'lib/chrono_model/time_machine/time_query.rb' # This cop supports unsafe autocorrection (--autocorrect-all). diff --git a/lib/chrono_model/adapter.rb b/lib/chrono_model/adapter.rb index 8d8aca1..259fe91 100644 --- a/lib/chrono_model/adapter.rb +++ b/lib/chrono_model/adapter.rb @@ -7,7 +7,6 @@ require 'chrono_model/adapter/ddl' require 'chrono_model/adapter/indexes' -require 'chrono_model/adapter/tsrange' require 'chrono_model/adapter/upgrade' module ChronoModel @@ -19,7 +18,6 @@ class Adapter < ActiveRecord::ConnectionAdapters::PostgreSQLAdapter include ChronoModel::Adapter::Migrations include ChronoModel::Adapter::DDL include ChronoModel::Adapter::Indexes - include ChronoModel::Adapter::TSRange include ChronoModel::Adapter::Upgrade # The schema holding current data diff --git a/lib/chrono_model/adapter/tsrange.rb b/lib/chrono_model/adapter/tsrange.rb deleted file mode 100644 index e005e7f..0000000 --- a/lib/chrono_model/adapter/tsrange.rb +++ /dev/null @@ -1,72 +0,0 @@ -# frozen_string_literal: true - -module ChronoModel - class Adapter < ActiveRecord::ConnectionAdapters::PostgreSQLAdapter - module TSRange - # HACK: Redefine tsrange parsing support, as it is broken currently. - # - # This self-made API is here because currently AR4 does not support - # open-ended ranges. The reasons are poor support in Ruby: - # - # https://bugs.ruby-lang.org/issues/6864 - # - # and an instable interface in Active Record: - # - # https://github.com/rails/rails/issues/13793 - # https://github.com/rails/rails/issues/14010 - # - # so, for now, we are implementing our own. - # - class Type < ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Range - OID = 3908 - - def cast_value(value) - return if value == 'empty' - return value if value.is_a?(::Array) - - extracted = extract_bounds(value) - - from = Conversions.string_to_utc_time extracted[:from] - to = Conversions.string_to_utc_time extracted[:to] - - [from, to] - end - - def extract_bounds(value) - from, to = value[1..-2].split(',') - - from_bound = - if value[1] == ',' || from == '-infinity' - nil - else - from[1..-2] - end - - to_bound = - if value[-2] == ',' || to == 'infinity' - nil - else - to[1..-2] - end - - { - from: from_bound, - to: to_bound - } - end - end - - def initialize_type_map(m = type_map) - super.tap do - typ = ChronoModel::Adapter::TSRange::Type - oid = typ::OID - - ar_type = type_map.fetch(oid) - cm_type = typ.new(ar_type.subtype, ar_type.type) - - type_map.register_type oid, cm_type - end - end - end - end -end diff --git a/lib/chrono_model/time_machine/history_model.rb b/lib/chrono_model/time_machine/history_model.rb index b76b2ba..dc8449e 100644 --- a/lib/chrono_model/time_machine/history_model.rb +++ b/lib/chrono_model/time_machine/history_model.rb @@ -211,10 +211,14 @@ def record # :nodoc: def valid_from validity.first + rescue RangeError + nil end def valid_to validity.last + rescue RangeError + nil end alias as_of_time valid_to