Unverified Commit a28fef34 by Jim Jowdy Committed by GitHub

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.
parent 13b532d9
......@@ -124,7 +124,11 @@ module AnnotateModels
# Try to search the table without prefix
table_name_without_prefix = table_name.to_s.sub(klass.table_name_prefix, '')
klass.connection.indexes(table_name_without_prefix)
if klass.connection.table_exists?(table_name_without_prefix)
klass.connection.indexes(table_name_without_prefix)
else
[]
end
end
# Use the column information in an ActiveRecord class
......
......@@ -53,7 +53,8 @@ describe AnnotateModels do
foreign_keys: foreign_keys,
check_constraints: check_constraints,
supports_foreign_keys?: true,
supports_check_constraints?: true)
supports_check_constraints?: true,
table_exists?: true)
end
# rubocop:disable Metrics/ParameterLists
......@@ -538,7 +539,7 @@ describe AnnotateModels do
end
end
context 'when one of indexes includes orderd index key' do
context 'when one of indexes includes ordered index key' do
let :columns do
[
mock_column("id", :integer),
......@@ -694,6 +695,24 @@ describe AnnotateModels do
it 'returns schema info without index information' do
is_expected.to eq expected_result
end
# rubocop:disable RSpec/NestedGroups
context 'when the unprefixed table name does not exist' do
let :klass do
mock_class(:users, primary_key, columns, indexes, foreign_keys).tap do |mock_klass|
allow(mock_klass).to receive(:table_name_prefix).and_return('my_prefix_')
allow(mock_klass.connection).to receive(:table_exists?).with('users').and_return(false)
allow(mock_klass.connection).to receive(:indexes).with('users').and_raise('error fetching indexes on nonexistent table')
end
end
it 'returns schema info without index information' do
is_expected.to eq expected_result
expect(klass).to have_received(:table_name_prefix).at_least(:once)
expect(klass.connection).to have_received(:table_exists?).with('users')
end
end
# rubocop:enable RSpec/NestedGroups
end
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment