Skip to content

Commit

Permalink
Don't choke when change_table temporal: true on a table with computed…
Browse files Browse the repository at this point in the history
… indexes (fixes #99)
  • Loading branch information
vjt committed May 12, 2020
1 parent 0706798 commit 90b1870
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/chrono_model/adapter/indexes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,14 @@ def chrono_copy_indexes_to_history(table_name)
next if history_indexes.include?(index.name)

on_history_schema do
# index.columns is an Array for plain indexes,
# while it is a String for computed indexes.
#
columns = Array.wrap(index.columns).join(', ')

execute %[
CREATE INDEX #{index.name} ON #{table_name}
USING #{index.using} ( #{index.columns.join(', ')} )
USING #{index.using} ( #{columns} )
], 'Copy index from temporal to history'
end
end
Expand Down
21 changes: 21 additions & 0 deletions spec/chrono_model/adapter/migrations_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'spec_helper'
require 'support/adapter/structure'

# For the structure of these tables, please see spec/support/adabters/structure.rb.
#
shared_examples_for 'temporal table' do
it { expect(adapter.is_chrono?(subject)).to be(true) }

Expand Down Expand Up @@ -94,12 +96,18 @@
before :all do
adapter.add_index table, :foo
adapter.add_index table, :bar, :unique => true
adapter.add_index table, '(lower(baz))'
adapter.add_index table, '(lower(baz) || upper(baz))'

adapter.change_table table, :temporal => true
end

it_should_behave_like 'temporal table'

let(:temporal_indexes) do
adapter.indexes(table)
end

let(:history_indexes) do
adapter.on_schema(ChronoModel::Adapter::HISTORY_SCHEMA) do
adapter.indexes(table)
Expand All @@ -113,6 +121,19 @@
it "copies unique index to history without uniqueness constraint" do
expect(history_indexes.find {|i| i.columns == ['bar'] && i.unique == false}).to be_present
end

it 'copies also computed indexes' do
indexes = %W(
index_#{table}_on_bar
index_#{table}_on_foo
index_#{table}_on_lower_baz
index_#{table}_on_lower_baz_upper_baz
)

expect(temporal_indexes.map(&:name).sort).to eq(indexes)

expect(indexes - history_indexes.map(&:name).sort).to be_empty
end
end

with_plain_table do
Expand Down

0 comments on commit 90b1870

Please sign in to comment.