Skip to content

Commit a28fef3

Browse files
authored
Fix retrieve_indexes_from_table when indexes is empty and base table does not exist. (#849)
Some tables may have a table_name_prefix but no indexes. Previous versions of the code would strip the prefix and look for indexes on the resulting table which likely would not exist. This causes DB errors, at least in MySQL. So now check if the new table exists first before trying to show its indexes.
1 parent 13b532d commit a28fef3

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

lib/annotate/annotate_models.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ def retrieve_indexes_from_table(klass)
124124

125125
# Try to search the table without prefix
126126
table_name_without_prefix = table_name.to_s.sub(klass.table_name_prefix, '')
127-
klass.connection.indexes(table_name_without_prefix)
127+
if klass.connection.table_exists?(table_name_without_prefix)
128+
klass.connection.indexes(table_name_without_prefix)
129+
else
130+
[]
131+
end
128132
end
129133

130134
# Use the column information in an ActiveRecord class

spec/lib/annotate/annotate_models_spec.rb

+21-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ def mock_connection(indexes = [], foreign_keys = [], check_constraints = [])
5353
foreign_keys: foreign_keys,
5454
check_constraints: check_constraints,
5555
supports_foreign_keys?: true,
56-
supports_check_constraints?: true)
56+
supports_check_constraints?: true,
57+
table_exists?: true)
5758
end
5859

5960
# rubocop:disable Metrics/ParameterLists
@@ -538,7 +539,7 @@ def mock_column(name, type, options = {})
538539
end
539540
end
540541

541-
context 'when one of indexes includes orderd index key' do
542+
context 'when one of indexes includes ordered index key' do
542543
let :columns do
543544
[
544545
mock_column("id", :integer),
@@ -694,6 +695,24 @@ def mock_column(name, type, options = {})
694695
it 'returns schema info without index information' do
695696
is_expected.to eq expected_result
696697
end
698+
699+
# rubocop:disable RSpec/NestedGroups
700+
context 'when the unprefixed table name does not exist' do
701+
let :klass do
702+
mock_class(:users, primary_key, columns, indexes, foreign_keys).tap do |mock_klass|
703+
allow(mock_klass).to receive(:table_name_prefix).and_return('my_prefix_')
704+
allow(mock_klass.connection).to receive(:table_exists?).with('users').and_return(false)
705+
allow(mock_klass.connection).to receive(:indexes).with('users').and_raise('error fetching indexes on nonexistent table')
706+
end
707+
end
708+
709+
it 'returns schema info without index information' do
710+
is_expected.to eq expected_result
711+
expect(klass).to have_received(:table_name_prefix).at_least(:once)
712+
expect(klass.connection).to have_received(:table_exists?).with('users')
713+
end
714+
end
715+
# rubocop:enable RSpec/NestedGroups
697716
end
698717
end
699718

0 commit comments

Comments
 (0)