diff --git a/lib/active_record/connection_adapters/chronomodel_adapter.rb b/lib/active_record/connection_adapters/chronomodel_adapter.rb index 6694b1a6..cce72dde 100644 --- a/lib/active_record/connection_adapters/chronomodel_adapter.rb +++ b/lib/active_record/connection_adapters/chronomodel_adapter.rb @@ -28,7 +28,7 @@ def chronomodel_connection(config) # :nodoc: "Currently, only PostgreSQL >= 9.3 is supported." end - adapter.chrono_setup! + #adapter.chrono_setup! return adapter end diff --git a/lib/chrono_model/adapter.rb b/lib/chrono_model/adapter.rb index 4d6e6762..2f63cfb6 100644 --- a/lib/chrono_model/adapter.rb +++ b/lib/chrono_model/adapter.rb @@ -18,6 +18,10 @@ class Adapter < ActiveRecord::ConnectionAdapters::PostgreSQLAdapter # This is the data type used for the SCD2 validity RANGE_TYPE = 'tsrange' + def initialize(*args) + chrono_setup_type_map + super + end # Returns true whether the connection adapter supports our # implementation of temporal tables. Currently, Chronomodel # is supported starting with PostgreSQL 9.3. @@ -32,6 +36,7 @@ def chrono_supported? def create_table(table_name, options = {}) # No temporal features requested, skip return super unless options[:temporal] + chrono_setup! if options[:id] == false logger.warn "WARNING - Temporal Temporal tables require a primary key." @@ -54,6 +59,7 @@ def create_table(table_name, options = {}) # def rename_table(name, new_name) return super unless is_chrono?(name) + chrono_setup! clear_cache! @@ -119,6 +125,7 @@ def change_table(table_name, options = {}, &block) block ||= proc { } if options[:temporal] == true + chrono_setup! if !is_chrono?(table_name) # Add temporal features to this table # @@ -130,7 +137,6 @@ def change_table(table_name, options = {}, &block) _on_history_schema { chrono_create_history_for(table_name) } chrono_create_view_for(table_name, options) copy_indexes_to_history_for(table_name) - TableCache.add! table_name # Optionally copy the plain table data, setting up history @@ -188,6 +194,7 @@ def change_table(table_name, options = {}, &block) # def drop_table(table_name, *) return super unless is_chrono?(table_name) + chrono_setup! _on_temporal_schema { execute "DROP TABLE #{table_name} CASCADE" } @@ -200,6 +207,7 @@ def drop_table(table_name, *) # def add_index(table_name, column_name, options = {}) return super unless is_chrono?(table_name) + chrono_setup! transaction do _on_temporal_schema { super } @@ -216,6 +224,7 @@ def add_index(table_name, column_name, options = {}) # def remove_index(table_name, *) return super unless is_chrono?(table_name) + chrono_setup! transaction do _on_temporal_schema { super } @@ -228,6 +237,7 @@ def remove_index(table_name, *) # def add_column(table_name, *) return super unless is_chrono?(table_name) + chrono_setup! transaction do # Add the column to the temporal table @@ -243,6 +253,7 @@ def add_column(table_name, *) # def rename_column(table_name, *) return super unless is_chrono?(table_name) + chrono_setup! # Rename the column in the temporal table and in the view transaction do @@ -260,6 +271,7 @@ def rename_column(table_name, *) # def change_column(table_name, *) return super unless is_chrono?(table_name) + chrono_setup! chrono_alter(table_name) { super } end @@ -267,6 +279,7 @@ def change_column(table_name, *) # def change_column_default(table_name, *) return super unless is_chrono?(table_name) + chrono_setup! _on_temporal_schema { super } end @@ -274,6 +287,7 @@ def change_column_default(table_name, *) # def change_column_null(table_name, *) return super unless is_chrono?(table_name) + chrono_setup! _on_temporal_schema { super } end @@ -283,6 +297,7 @@ def change_column_null(table_name, *) # def remove_column(table_name, *) return super unless is_chrono?(table_name) + chrono_setup! chrono_alter(table_name) { super } end @@ -486,8 +501,8 @@ class TSRange < OID::Type def extract_bounds(value) from, to = value[1..-2].split(',') { - from: (value[1] == ',' || from == '-infinity') ? nil : from[1..-2], - to: (value[-2] == ',' || to == 'infinity') ? nil : to[1..-2], + from: (value[1] == ',' || from == '-infinity' || from.nil?) ? nil : from[1..-2], + to: (value[-2] == ',' || to == 'infinity' || to.nil?) ? nil : to[1..-2], #exclude_start: (value[0] == '('), #exclude_end: (value[-1] == ')') } @@ -505,11 +520,15 @@ def type_cast(value) def chrono_setup! chrono_create_schemas - chrono_setup_type_map - chrono_upgrade_structure! end + # Adds the above TSRange class to the PG Adapter OID::TYPE_MAP + # + def chrono_setup_type_map + OID.register_type 'tsrange', TSRange.new + end + # Copy the indexes from the temporal table to the history table if the indexes # are not already created with the same name. # @@ -543,12 +562,6 @@ def chrono_create_schemas end end - # Adds the above TSRange class to the PG Adapter OID::TYPE_MAP - # - def chrono_setup_type_map - OID.register_type 'tsrange', TSRange.new - end - # Upgrades existing structure for each table, if required. # TODO: allow upgrades from pre-0.6 structure with box() and stuff. #