Skip to content

Commit 5803fba

Browse files
sashabelozerovctran
authored andcommitted
Annotate ordered indexes (#479)
1 parent 8e75c91 commit 5803fba

File tree

2 files changed

+71
-9
lines changed

2 files changed

+71
-9
lines changed

lib/annotate/annotate_models.rb

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,15 +337,33 @@ def get_index_info(klass, options = {})
337337
max_size = indexes.collect{|index| index.name.size}.max + 1
338338
indexes.sort_by(&:name).each do |index|
339339
index_info << if options[:format_markdown]
340-
sprintf("# * `%s`%s:\n# * **`%s`**\n", index.name, index.unique ? " (_unique_)" : "", Array(index.columns).join("`**\n# * **`"))
340+
final_index_string_in_markdown(index)
341341
else
342-
sprintf("# %-#{max_size}.#{max_size}s %s %s", index.name, "(#{Array(index.columns).join(",")})", index.unique ? "UNIQUE" : "").rstrip + "\n"
342+
final_index_string(index, max_size)
343343
end
344344
end
345345

346346
index_info
347347
end
348348

349+
def index_columns_info(index)
350+
Array(index.columns).map do |col|
351+
if index.try(:orders) && index.orders[col.to_s]
352+
"#{col} #{index.orders[col.to_s].upcase}"
353+
else
354+
col.to_s
355+
end
356+
end
357+
end
358+
359+
def final_index_string_in_markdown(index)
360+
sprintf("# * `%s`%s:\n# * **`%s`**\n", index.name, index.unique ? " (_unique_)" : "", index_columns_info(index).join("`**\n# * **`"))
361+
end
362+
363+
def final_index_string(index, max_size)
364+
sprintf("# %-#{max_size}.#{max_size}s %s %s", index.name, "(#{index_columns_info(index).join(',')})", index.unique ? "UNIQUE" : "").rstrip + "\n"
365+
end
366+
349367
def hide_limit?(col_type, options)
350368
excludes =
351369
if options[:hide_limit_column_types].blank?

spec/annotate/annotate_models_spec.rb

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
require 'active_support/core_ext/string'
66

77
describe AnnotateModels do
8-
def mock_index(name, columns = [], unique = false)
8+
def mock_index(name, columns = [], orders = {}, unique = false)
99
double('IndexKeyDefinition',
1010
name: name,
1111
columns: columns,
12-
unique: unique)
12+
unique: unique,
13+
orders: orders)
1314
end
1415

1516
def mock_foreign_key(name, from_column, to_table, to_column = 'id', constraints = {})
@@ -320,14 +321,52 @@ def mock_column(name, type, options = {})
320321
EOS
321322
end
322323

324+
it 'should get ordered indexes keys' do
325+
klass = mock_class(:users,
326+
:id,
327+
[
328+
mock_column("id", :integer),
329+
mock_column("firstname", :string),
330+
mock_column("surname", :string),
331+
mock_column("value", :string)
332+
],
333+
[
334+
mock_index('index_rails_02e851e3b7', ['id']),
335+
mock_index('index_rails_02e851e3b8',
336+
%w(firstname surname value),
337+
'surname' => :asc, 'value' => :desc)
338+
])
339+
expect(AnnotateModels.get_schema_info(klass, 'Schema Info', show_indexes: true)).to eql(<<-EOS)
340+
# Schema Info
341+
#
342+
# Table name: users
343+
#
344+
# id :integer not null, primary key
345+
# firstname :string not null
346+
# surname :string not null
347+
# value :string not null
348+
#
349+
# Indexes
350+
#
351+
# index_rails_02e851e3b7 (id)
352+
# index_rails_02e851e3b8 (firstname,surname ASC,value DESC)
353+
#
354+
EOS
355+
end
356+
323357
it 'should get simple indexes keys' do
324358
klass = mock_class(:users,
325359
:id,
326360
[
327361
mock_column(:id, :integer),
328362
mock_column(:foreign_thing_id, :integer)
329-
], [mock_index('index_rails_02e851e3b7', ['id']),
330-
mock_index('index_rails_02e851e3b8', ['foreign_thing_id'])])
363+
],
364+
[
365+
mock_index('index_rails_02e851e3b7', ['id']),
366+
mock_index('index_rails_02e851e3b8',
367+
['foreign_thing_id'],
368+
'foreign_thing_id' => :desc)
369+
])
331370
expect(AnnotateModels.get_schema_info(klass, 'Schema Info', simple_indexes: true)).to eql(<<-EOS)
332371
# Schema Info
333372
#
@@ -460,8 +499,13 @@ def mock_column(name, type, options = {})
460499
[
461500
mock_column(:id, :integer),
462501
mock_column(:name, :string, limit: 50)
463-
], [mock_index('index_rails_02e851e3b7', ['id']),
464-
mock_index('index_rails_02e851e3b8', ['foreign_thing_id'])])
502+
],
503+
[
504+
mock_index('index_rails_02e851e3b7', ['id']),
505+
mock_index('index_rails_02e851e3b8',
506+
['foreign_thing_id'],
507+
'foreign_thing_id' => :desc)
508+
])
465509
expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, show_indexes: true)).to eql(<<-EOS)
466510
# #{AnnotateModels::PREFIX}
467511
#
@@ -479,7 +523,7 @@ def mock_column(name, type, options = {})
479523
# * `index_rails_02e851e3b7`:
480524
# * **`id`**
481525
# * `index_rails_02e851e3b8`:
482-
# * **`foreign_thing_id`**
526+
# * **`foreign_thing_id DESC`**
483527
#
484528
EOS
485529
end

0 commit comments

Comments
 (0)