Skip to content

Fix retrieve_indexes_from_table when indexes is empty and base table does not exist #849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ def retrieve_indexes_from_table(klass)

# 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
Expand Down
23 changes: 21 additions & 2 deletions spec/lib/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def mock_connection(indexes = [], foreign_keys = [], check_constraints = [])
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
Expand Down Expand Up @@ -538,7 +539,7 @@ def mock_column(name, type, options = {})
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),
Expand Down Expand Up @@ -694,6 +695,24 @@ def mock_column(name, type, options = {})
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

Expand Down