Skip to content

Commit

Permalink
Move index migration methods to concerns
Browse files Browse the repository at this point in the history
The signature of `add_index` and `remove_index` is different among
Rails 6.1 and older Rails versions and the old code had too mani `if`s
and bugs.

This approach conditionally requires implementations basing on AR
version
  • Loading branch information
tagliala committed Dec 1, 2021
1 parent e28b73d commit 1030c27
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 77 deletions.
7 changes: 7 additions & 0 deletions lib/chrono_model/adapter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
require 'active_record/connection_adapters/postgresql_adapter'

require 'chrono_model/adapter/migrations'

if ActiveRecord::VERSION::STRING >= '6.1'
require 'chrono_model/adapter/migration_modules/stable'
else
require 'chrono_model/adapter/migration_modules/legacy'
end

require 'chrono_model/adapter/ddl'
require 'chrono_model/adapter/indexes'
require 'chrono_model/adapter/tsrange'
Expand Down
39 changes: 39 additions & 0 deletions lib/chrono_model/adapter/migration_modules/legacy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module ChronoModel
class Adapter
module MigrationModules
module Legacy
# If adding an index to a temporal table, add it to the one in the
# temporal schema and to the history one. If the `:unique` option is
# present, it is removed from the index created in the history table.
#
def add_index(table_name, column_name, options = {})
return super unless is_chrono?(table_name)

transaction do
on_temporal_schema { super }

# Uniqueness constraints do not make sense in the history table
options = options.dup.tap { |o| o.delete(:unique) } if options[:unique].present?

on_history_schema { super table_name, column_name, options }
end
end

# If removing an index from a temporal table, remove it both from the
# temporal and the history schemas.
#
def remove_index(table_name, options = {})
return super unless is_chrono?(table_name)

transaction do
on_temporal_schema { super }

on_history_schema { super }
end
end
end
end
end
end

ChronoModel::Adapter::Migrations.include ChronoModel::Adapter::MigrationModules::Legacy
39 changes: 39 additions & 0 deletions lib/chrono_model/adapter/migration_modules/stable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module ChronoModel
class Adapter
module MigrationModules
module Ar61
# If adding an index to a temporal table, add it to the one in the
# temporal schema and to the history one. If the `:unique` option is
# present, it is removed from the index created in the history table.
#
def add_index(table_name, column_name, **options)
return super unless is_chrono?(table_name)

transaction do
on_temporal_schema { super }

# Uniqueness constraints do not make sense in the history table
options = options.dup.tap { |o| o.delete(:unique) } if options[:unique].present?

on_history_schema { super table_name, column_name, **options }
end
end

# If removing an index from a temporal table, remove it both from the
# temporal and the history schemas.
#
def remove_index(table_name, column_name = nil, **options)
return super unless is_chrono?(table_name)

transaction do
on_temporal_schema { super }

on_history_schema { super }
end
end
end
end
end
end

ChronoModel::Adapter::Migrations.include ChronoModel::Adapter::MigrationModules::Ar61
77 changes: 0 additions & 77 deletions lib/chrono_model/adapter/migrations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,83 +101,6 @@ def drop_table(table_name, *)
chrono_drop_trigger_functions_for(table_name)
end

# If adding an index to a temporal table, add it to the one in the
# temporal schema and to the history one. If the `:unique` option is
# present, it is removed from the index created in the history table.
#
def add_index(table_name, column_name, options = {})
unless is_chrono?(table_name)
return super if RUBY_VERSION < '3.0.0'
return super(table_name, column_name, **options)
end

transaction do
on_temporal_schema do
if RUBY_VERSION < '3.0.0'
super
else
super(table_name, column_name, **options)
end
end

# Uniqueness constraints do not make sense in the history table
options = options.dup.tap {|o| o.delete(:unique)} if options[:unique].present?

on_history_schema do
if RUBY_VERSION < '3.0.0'
super table_name, column_name, options
else
super table_name, column_name, **options
end
end
end
end

# If removing an index from a temporal table, remove it both from the
# temporal and the history schemas.
#
def remove_index(table_name, options = {})
unless is_chrono?(table_name)
if RUBY_VERSION < '3.0.0'
return super
else
if ActiveRecord::VERSION::STRING < '6.1'
return super(table_name, options)
else
# nil refers to column name
return super(table_name, nil, **options)
end
end
end

transaction do
on_temporal_schema do
if RUBY_VERSION < '3.0.0'
super
else
if ActiveRecord::VERSION::STRING < '6.1'
super(table_name, options)
else
# nil refers to column name
super(table_name, nil, **options)
end
end
end
on_history_schema do
if RUBY_VERSION < '3.0.0'
super
else
if ActiveRecord::VERSION::STRING < '6.1'
super(table_name, options)
else
# nil refers to column name
super(table_name, nil, **options)
end
end
end
end
end

# If adding a column to a temporal table, creates it in the table in
# the temporal schema and updates the triggers.
#
Expand Down

0 comments on commit 1030c27

Please sign in to comment.