Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 24 additions & 11 deletions lib/chrono_model/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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."
Expand All @@ -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!

Expand Down Expand Up @@ -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
#
Expand All @@ -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
Expand Down Expand Up @@ -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" }

Expand All @@ -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 }
Expand All @@ -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 }
Expand All @@ -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
Expand All @@ -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
Expand All @@ -260,20 +271,23 @@ 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

# Change the default on the temporal schema table.
#
def change_column_default(table_name, *)
return super unless is_chrono?(table_name)
chrono_setup!
_on_temporal_schema { super }
end

# Change the null constraint on the temporal schema table.
#
def change_column_null(table_name, *)
return super unless is_chrono?(table_name)
chrono_setup!
_on_temporal_schema { super }
end

Expand All @@ -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

Expand Down Expand Up @@ -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] == ')')
}
Expand All @@ -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.
#
Expand Down Expand Up @@ -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.
#
Expand Down