From e4c3a4ba80cd990a9c7507e0fa9eb2f6b60caf6c Mon Sep 17 00:00:00 2001 From: epdejager Date: Fri, 20 Feb 2015 15:12:46 +0200 Subject: [PATCH 1/4] apartment causes connections to open all the time, causing chronosetup to be called like 50 times in one page load, resulting in well slow load times. Moved chrono_setup to table edit statments in migrations for now --- lib/active_record/connection_adapters/chronomodel_adapter.rb | 2 +- lib/chrono_model/adapter.rb | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) 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..5450aa77 100644 --- a/lib/chrono_model/adapter.rb +++ b/lib/chrono_model/adapter.rb @@ -32,6 +32,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 +55,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 +121,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 +133,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 +190,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" } From 99d2db9feb98c6646d4289a30e23d1564065bbf2 Mon Sep 17 00:00:00 2001 From: epdejager Date: Tue, 24 Feb 2015 16:47:24 +0200 Subject: [PATCH 2/4] made sure schema migrations only happen during migrations and on every new connection as combined with the constant connection switching Apartment does this was slowing apps using Apartment down --- .../chronomodel_adapter.rb | 1 + lib/chrono_model/adapter.rb | 22 ++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/active_record/connection_adapters/chronomodel_adapter.rb b/lib/active_record/connection_adapters/chronomodel_adapter.rb index cce72dde..78d96c6f 100644 --- a/lib/active_record/connection_adapters/chronomodel_adapter.rb +++ b/lib/active_record/connection_adapters/chronomodel_adapter.rb @@ -28,6 +28,7 @@ def chronomodel_connection(config) # :nodoc: "Currently, only PostgreSQL >= 9.3 is supported." end + adapter.chrono_setup_type_map #adapter.chrono_setup! return adapter diff --git a/lib/chrono_model/adapter.rb b/lib/chrono_model/adapter.rb index 5450aa77..066b9f2a 100644 --- a/lib/chrono_model/adapter.rb +++ b/lib/chrono_model/adapter.rb @@ -203,6 +203,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 } @@ -219,6 +220,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 } @@ -231,6 +233,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 @@ -246,6 +249,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 @@ -263,6 +267,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 @@ -270,6 +275,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 @@ -277,6 +283,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 @@ -286,6 +293,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 @@ -508,11 +516,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. # @@ -546,12 +558,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. # From f7107b03e1fb0a3026928004a56babe3243f53e6 Mon Sep 17 00:00:00 2001 From: epdejager Date: Wed, 11 Mar 2015 14:20:53 +0200 Subject: [PATCH 3/4] setup the typemap on init so tsrange mapping is available to the connection right at the start. Otherwise specs run in rspecs end up using the old range mapping for some reason --- lib/chrono_model/adapter.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/chrono_model/adapter.rb b/lib/chrono_model/adapter.rb index 066b9f2a..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. @@ -497,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] == ')') } From c39563d7714d2efaf20a36b8f854347200a3d6c1 Mon Sep 17 00:00:00 2001 From: epdejager Date: Wed, 11 Mar 2015 14:21:12 +0200 Subject: [PATCH 4/4] moved setup type map to init of adapter --- lib/active_record/connection_adapters/chronomodel_adapter.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/active_record/connection_adapters/chronomodel_adapter.rb b/lib/active_record/connection_adapters/chronomodel_adapter.rb index 78d96c6f..cce72dde 100644 --- a/lib/active_record/connection_adapters/chronomodel_adapter.rb +++ b/lib/active_record/connection_adapters/chronomodel_adapter.rb @@ -28,7 +28,6 @@ def chronomodel_connection(config) # :nodoc: "Currently, only PostgreSQL >= 9.3 is supported." end - adapter.chrono_setup_type_map #adapter.chrono_setup! return adapter