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