Commit cce4bb1d by Guillermo Guerrero Ibarra Committed by Cuong Tran

Fixed Posgresql index tables with `public`. (#405)

* Fixed Posgresql index tables with `public`. Added indexes test. Added test not crash getting indexes. * Updated rake and rubocop.
parent bc18c3c3
source 'https://rubygems.org'
gem 'rake', '>= 10.4.2', require: false
gem 'rake', require: false
gem 'activerecord', '>= 4.2.5', require: false
group :development do
......@@ -16,9 +16,10 @@ group :development, :test do
gem 'guard-rspec', require: false
gem 'terminal-notifier-guard', require: false
gem 'simplecov', require: false
gem 'rubocop', '~> 0.37.2', require: false unless RUBY_VERSION =~ /^1.8/
gem 'rubocop', '~> 0.39.0', require: false unless RUBY_VERSION =~ /^1.8/
gem 'coveralls'
gem 'codeclimate-test-reporter'
gem 'ruby_dep', '1.3.1'
platforms :mri, :mingw do
gem 'pry', require: false
......
......@@ -181,6 +181,18 @@ module AnnotateModels
quote(klass.column_defaults[column.name])
end
def retrieve_indexes_from_table(klass)
table_name = klass.table_name
return [] unless table_name
indexes = klass.connection.indexes(table_name)
return indexes if indexes.any? || !klass.table_name_prefix
# Try to search the table without prefix
table_name.to_s.slice!(klass.table_name_prefix)
klass.connection.indexes(table_name)
end
# Use the column information in an ActiveRecord class
# to create a comment block containing a line for
# each column. The line contains the column name,
......@@ -244,7 +256,7 @@ module AnnotateModels
# Check if the column has indices and print "indexed" if true
# If the index includes another column, print it too.
if options[:simple_indexes] && klass.table_exists?# Check out if this column is indexed
indices = klass.connection.indexes(klass.table_name)
indices = retrieve_indexes_from_table(klass)
if indices = indices.select { |ind| ind.columns.include? col.name }
indices.sort_by(&:name).each do |ind|
ind = ind.columns.reject! { |i| i == col.name }
......@@ -305,7 +317,7 @@ module AnnotateModels
index_info = "#\n# Indexes\n#\n"
end
indexes = klass.connection.indexes(klass.table_name)
indexes = retrieve_indexes_from_table(klass)
return '' if indexes.empty?
max_size = indexes.collect{|index| index.name.size}.max + 1
......
......@@ -5,6 +5,14 @@ require 'annotate/active_record_patch'
require 'active_support/core_ext/string'
describe AnnotateModels do
def mock_index(name, columns = [], unique = false)
double("IndexKeyDefinition",
:name => name,
:columns => columns,
:unique => unique
)
end
def mock_foreign_key(name, from_column, to_table, to_column = 'id', constraints = {})
double("ForeignKeyDefinition",
:name => name,
......@@ -20,13 +28,13 @@ describe AnnotateModels do
double("Conn",
:indexes => indexes,
:foreign_keys => foreign_keys,
:supports_foreign_keys? => true,
:supports_foreign_keys? => true
)
end
def mock_class(table_name, primary_key, columns, foreign_keys = [])
def mock_class(table_name, primary_key, columns, indexes = [], foreign_keys = [])
options = {
:connection => mock_connection([], foreign_keys),
:connection => mock_connection(indexes, foreign_keys),
:table_exists? => true,
:table_name => table_name,
:primary_key => primary_key,
......@@ -34,7 +42,8 @@ describe AnnotateModels do
:columns => columns,
:column_defaults => Hash[columns.map { |col|
[col.name, col.default]
}]
}],
:table_name_prefix => '',
}
double("An ActiveRecord class", options)
......@@ -181,7 +190,7 @@ EOS
klass = mock_class(:users, :id, [
mock_column(:id, :integer),
mock_column(:foreign_thing_id, :integer),
],
], [],
[
mock_foreign_key(
'fk_rails_cf2568e89e',
......@@ -220,7 +229,7 @@ EOS
klass = mock_class(:users, :id, [
mock_column(:id, :integer),
mock_column(:foreign_thing_id, :integer),
],
], [],
[
mock_foreign_key(
'fk_rails_02e851e3b7',
......@@ -246,6 +255,44 @@ EOS
EOS
end
it "should get 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'])])
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
# foreign_thing_id :integer not null
#
# Indexes
#
# index_rails_02e851e3b7 (id)
# index_rails_02e851e3b8 (foreign_thing_id)
#
EOS
end
it "should not crash getting indexes keys" do
klass = mock_class(:users, :id, [
mock_column(:id, :integer),
mock_column(:foreign_thing_id, :integer),
], [])
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
# foreign_thing_id :integer not null
#
EOS
end
it "should get schema info as RDoc" do
klass = mock_class(:users, :id, [
mock_column(:id, :integer),
......
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