-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move index migration methods to concerns
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
Showing
4 changed files
with
85 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters