Commit 5803fbaa by Alexander Belozerov Committed by Cuong Tran

Annotate ordered indexes (#479)

parent 8e75c911
...@@ -337,15 +337,33 @@ module AnnotateModels ...@@ -337,15 +337,33 @@ module AnnotateModels
max_size = indexes.collect{|index| index.name.size}.max + 1 max_size = indexes.collect{|index| index.name.size}.max + 1
indexes.sort_by(&:name).each do |index| indexes.sort_by(&:name).each do |index|
index_info << if options[:format_markdown] index_info << if options[:format_markdown]
sprintf("# * `%s`%s:\n# * **`%s`**\n", index.name, index.unique ? " (_unique_)" : "", Array(index.columns).join("`**\n# * **`")) final_index_string_in_markdown(index)
else else
sprintf("# %-#{max_size}.#{max_size}s %s %s", index.name, "(#{Array(index.columns).join(",")})", index.unique ? "UNIQUE" : "").rstrip + "\n" final_index_string(index, max_size)
end end
end end
index_info index_info
end end
def index_columns_info(index)
Array(index.columns).map do |col|
if index.try(:orders) && index.orders[col.to_s]
"#{col} #{index.orders[col.to_s].upcase}"
else
col.to_s
end
end
end
def final_index_string_in_markdown(index)
sprintf("# * `%s`%s:\n# * **`%s`**\n", index.name, index.unique ? " (_unique_)" : "", index_columns_info(index).join("`**\n# * **`"))
end
def final_index_string(index, max_size)
sprintf("# %-#{max_size}.#{max_size}s %s %s", index.name, "(#{index_columns_info(index).join(',')})", index.unique ? "UNIQUE" : "").rstrip + "\n"
end
def hide_limit?(col_type, options) def hide_limit?(col_type, options)
excludes = excludes =
if options[:hide_limit_column_types].blank? if options[:hide_limit_column_types].blank?
......
...@@ -5,11 +5,12 @@ require 'annotate/active_record_patch' ...@@ -5,11 +5,12 @@ require 'annotate/active_record_patch'
require 'active_support/core_ext/string' require 'active_support/core_ext/string'
describe AnnotateModels do describe AnnotateModels do
def mock_index(name, columns = [], unique = false) def mock_index(name, columns = [], orders = {}, unique = false)
double('IndexKeyDefinition', double('IndexKeyDefinition',
name: name, name: name,
columns: columns, columns: columns,
unique: unique) unique: unique,
orders: orders)
end end
def mock_foreign_key(name, from_column, to_table, to_column = 'id', constraints = {}) def mock_foreign_key(name, from_column, to_table, to_column = 'id', constraints = {})
...@@ -320,14 +321,52 @@ EOS ...@@ -320,14 +321,52 @@ EOS
EOS EOS
end end
it 'should get ordered indexes keys' do
klass = mock_class(:users,
:id,
[
mock_column("id", :integer),
mock_column("firstname", :string),
mock_column("surname", :string),
mock_column("value", :string)
],
[
mock_index('index_rails_02e851e3b7', ['id']),
mock_index('index_rails_02e851e3b8',
%w(firstname surname value),
'surname' => :asc, 'value' => :desc)
])
expect(AnnotateModels.get_schema_info(klass, 'Schema Info', show_indexes: true)).to eql(<<-EOS)
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# firstname :string not null
# surname :string not null
# value :string not null
#
# Indexes
#
# index_rails_02e851e3b7 (id)
# index_rails_02e851e3b8 (firstname,surname ASC,value DESC)
#
EOS
end
it 'should get simple indexes keys' do it 'should get simple indexes keys' do
klass = mock_class(:users, klass = mock_class(:users,
:id, :id,
[ [
mock_column(:id, :integer), mock_column(:id, :integer),
mock_column(:foreign_thing_id, :integer) mock_column(:foreign_thing_id, :integer)
], [mock_index('index_rails_02e851e3b7', ['id']), ],
mock_index('index_rails_02e851e3b8', ['foreign_thing_id'])]) [
mock_index('index_rails_02e851e3b7', ['id']),
mock_index('index_rails_02e851e3b8',
['foreign_thing_id'],
'foreign_thing_id' => :desc)
])
expect(AnnotateModels.get_schema_info(klass, 'Schema Info', simple_indexes: true)).to eql(<<-EOS) expect(AnnotateModels.get_schema_info(klass, 'Schema Info', simple_indexes: true)).to eql(<<-EOS)
# Schema Info # Schema Info
# #
...@@ -460,8 +499,13 @@ EOS ...@@ -460,8 +499,13 @@ EOS
[ [
mock_column(:id, :integer), mock_column(:id, :integer),
mock_column(:name, :string, limit: 50) mock_column(:name, :string, limit: 50)
], [mock_index('index_rails_02e851e3b7', ['id']), ],
mock_index('index_rails_02e851e3b8', ['foreign_thing_id'])]) [
mock_index('index_rails_02e851e3b7', ['id']),
mock_index('index_rails_02e851e3b8',
['foreign_thing_id'],
'foreign_thing_id' => :desc)
])
expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, show_indexes: true)).to eql(<<-EOS) expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, show_indexes: true)).to eql(<<-EOS)
# #{AnnotateModels::PREFIX} # #{AnnotateModels::PREFIX}
# #
...@@ -479,7 +523,7 @@ EOS ...@@ -479,7 +523,7 @@ EOS
# * `index_rails_02e851e3b7`: # * `index_rails_02e851e3b7`:
# * **`id`** # * **`id`**
# * `index_rails_02e851e3b8`: # * `index_rails_02e851e3b8`:
# * **`foreign_thing_id`** # * **`foreign_thing_id DESC`**
# #
EOS EOS
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