Skip to content

Commit e7b01bd

Browse files
committed
Fixed Posgresql index tables with public.
Added indexes test. Added test not crash getting indexes.
1 parent 417d10b commit e7b01bd

File tree

3 files changed

+68
-8
lines changed

3 files changed

+68
-8
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ group :development, :test do
1919
gem 'rubocop', '~> 0.37.2', require: false unless RUBY_VERSION =~ /^1.8/
2020
gem 'coveralls'
2121
gem 'codeclimate-test-reporter'
22+
gem 'ruby_dep', '1.3.1'
2223

2324
platforms :mri, :mingw do
2425
gem 'pry', require: false

lib/annotate/annotate_models.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,18 @@ def schema_default(klass, column)
181181
quote(klass.column_defaults[column.name])
182182
end
183183

184+
def retrieve_indexes_from_table(klass)
185+
table_name = klass.table_name
186+
return [] unless table_name
187+
188+
indexes = klass.connection.indexes(table_name)
189+
return indexes if indexes.any? || !klass.table_name_prefix
190+
191+
# Try to search the table without prefix
192+
table_name.to_s.slice!(klass.table_name_prefix)
193+
klass.connection.indexes(table_name)
194+
end
195+
184196
# Use the column information in an ActiveRecord class
185197
# to create a comment block containing a line for
186198
# each column. The line contains the column name,
@@ -244,7 +256,7 @@ def get_schema_info(klass, header, options = {})
244256
# Check if the column has indices and print "indexed" if true
245257
# If the index includes another column, print it too.
246258
if options[:simple_indexes] && klass.table_exists?# Check out if this column is indexed
247-
indices = klass.connection.indexes(klass.table_name)
259+
indices = retrieve_indexes_from_table(klass)
248260
if indices = indices.select { |ind| ind.columns.include? col.name }
249261
indices.sort_by(&:name).each do |ind|
250262
ind = ind.columns.reject! { |i| i == col.name }
@@ -305,7 +317,7 @@ def get_index_info(klass, options={})
305317
index_info = "#\n# Indexes\n#\n"
306318
end
307319

308-
indexes = klass.connection.indexes(klass.table_name)
320+
indexes = retrieve_indexes_from_table(klass)
309321
return '' if indexes.empty?
310322

311323
max_size = indexes.collect{|index| index.name.size}.max + 1

spec/annotate/annotate_models_spec.rb

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
require 'active_support/core_ext/string'
66

77
describe AnnotateModels do
8+
def mock_index(name, columns = [], unique = false)
9+
double("IndexKeyDefinition",
10+
:name => name,
11+
:columns => columns,
12+
:unique => unique
13+
)
14+
end
15+
816
def mock_foreign_key(name, from_column, to_table, to_column = 'id', constraints = {})
917
double("ForeignKeyDefinition",
1018
:name => name,
@@ -20,21 +28,22 @@ def mock_connection(indexes = [], foreign_keys = [])
2028
double("Conn",
2129
:indexes => indexes,
2230
:foreign_keys => foreign_keys,
23-
:supports_foreign_keys? => true,
31+
:supports_foreign_keys? => true
2432
)
2533
end
2634

27-
def mock_class(table_name, primary_key, columns, foreign_keys = [])
35+
def mock_class(table_name, primary_key, columns, indexes = [], foreign_keys = [])
2836
options = {
29-
:connection => mock_connection([], foreign_keys),
37+
:connection => mock_connection(indexes, foreign_keys),
3038
:table_exists? => true,
3139
:table_name => table_name,
3240
:primary_key => primary_key,
3341
:column_names => columns.map { |col| col.name.to_s },
3442
:columns => columns,
3543
:column_defaults => Hash[columns.map { |col|
3644
[col.name, col.default]
37-
}]
45+
}],
46+
:table_name_prefix => '',
3847
}
3948

4049
double("An ActiveRecord class", options)
@@ -181,7 +190,7 @@ def mock_column(name, type, options={})
181190
klass = mock_class(:users, :id, [
182191
mock_column(:id, :integer),
183192
mock_column(:foreign_thing_id, :integer),
184-
],
193+
], [],
185194
[
186195
mock_foreign_key(
187196
'fk_rails_cf2568e89e',
@@ -220,7 +229,7 @@ def mock_column(name, type, options={})
220229
klass = mock_class(:users, :id, [
221230
mock_column(:id, :integer),
222231
mock_column(:foreign_thing_id, :integer),
223-
],
232+
], [],
224233
[
225234
mock_foreign_key(
226235
'fk_rails_02e851e3b7',
@@ -246,6 +255,44 @@ def mock_column(name, type, options={})
246255
EOS
247256
end
248257

258+
it "should get indexes keys" do
259+
klass = mock_class(:users, :id, [
260+
mock_column(:id, :integer),
261+
mock_column(:foreign_thing_id, :integer),
262+
], [mock_index('index_rails_02e851e3b7', ['id']),
263+
mock_index('index_rails_02e851e3b8', ['foreign_thing_id'])])
264+
expect(AnnotateModels.get_schema_info(klass, "Schema Info", :show_indexes => true)).to eql(<<-EOS)
265+
# Schema Info
266+
#
267+
# Table name: users
268+
#
269+
# id :integer not null, primary key
270+
# foreign_thing_id :integer not null
271+
#
272+
# Indexes
273+
#
274+
# index_rails_02e851e3b7 (id)
275+
# index_rails_02e851e3b8 (foreign_thing_id)
276+
#
277+
EOS
278+
end
279+
280+
it "should not crash getting indexes keys" do
281+
klass = mock_class(:users, :id, [
282+
mock_column(:id, :integer),
283+
mock_column(:foreign_thing_id, :integer),
284+
], [])
285+
expect(AnnotateModels.get_schema_info(klass, "Schema Info", :show_indexes => true)).to eql(<<-EOS)
286+
# Schema Info
287+
#
288+
# Table name: users
289+
#
290+
# id :integer not null, primary key
291+
# foreign_thing_id :integer not null
292+
#
293+
EOS
294+
end
295+
249296
it "should get schema info as RDoc" do
250297
klass = mock_class(:users, :id, [
251298
mock_column(:id, :integer),

0 commit comments

Comments
 (0)