Skip to content

Annotate ordered indexes #479

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
Jul 2, 2017
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
22 changes: 20 additions & 2 deletions lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -337,15 +337,33 @@ def get_index_info(klass, options = {})
max_size = indexes.collect{|index| index.name.size}.max + 1
indexes.sort_by(&:name).each do |index|
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
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

index_info
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)
excludes =
if options[:hide_limit_column_types].blank?
Expand Down
58 changes: 51 additions & 7 deletions spec/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
require 'active_support/core_ext/string'

describe AnnotateModels do
def mock_index(name, columns = [], unique = false)
def mock_index(name, columns = [], orders = {}, unique = false)
double('IndexKeyDefinition',
name: name,
columns: columns,
unique: unique)
unique: unique,
orders: orders)
end

def mock_foreign_key(name, from_column, to_table, to_column = 'id', constraints = {})
Expand Down Expand Up @@ -320,14 +321,52 @@ def mock_column(name, type, options = {})
EOS
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
klass = mock_class(:users,
:id,
[
mock_column(: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)
# Schema Info
#
Expand Down Expand Up @@ -460,8 +499,13 @@ def mock_column(name, type, options = {})
[
mock_column(:id, :integer),
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)
# #{AnnotateModels::PREFIX}
#
Expand All @@ -479,7 +523,7 @@ def mock_column(name, type, options = {})
# * `index_rails_02e851e3b7`:
# * **`id`**
# * `index_rails_02e851e3b8`:
# * **`foreign_thing_id`**
# * **`foreign_thing_id DESC`**
#
EOS
end
Expand Down