Skip to content

Fixed Posgresql index tables with public. #405

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 2 commits into from
Dec 13, 2016
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
5 changes: 3 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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
Expand All @@ -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
Expand Down
16 changes: 14 additions & 2 deletions lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,18 @@ def schema_default(klass, column)
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,
Expand Down Expand Up @@ -244,7 +256,7 @@ def get_schema_info(klass, header, options = {})
# 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 }
Expand Down Expand Up @@ -305,7 +317,7 @@ def get_index_info(klass, options={})
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
Expand Down
59 changes: 53 additions & 6 deletions spec/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
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,
Expand All @@ -20,21 +28,22 @@ def mock_connection(indexes = [], foreign_keys = [])
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,
:column_names => columns.map { |col| col.name.to_s },
:columns => columns,
:column_defaults => Hash[columns.map { |col|
[col.name, col.default]
}]
}],
:table_name_prefix => '',
}

double("An ActiveRecord class", options)
Expand Down Expand Up @@ -181,7 +190,7 @@ def mock_column(name, type, options={})
klass = mock_class(:users, :id, [
mock_column(:id, :integer),
mock_column(:foreign_thing_id, :integer),
],
], [],
[
mock_foreign_key(
'fk_rails_cf2568e89e',
Expand Down Expand Up @@ -220,7 +229,7 @@ def mock_column(name, type, options={})
klass = mock_class(:users, :id, [
mock_column(:id, :integer),
mock_column(:foreign_thing_id, :integer),
],
], [],
[
mock_foreign_key(
'fk_rails_02e851e3b7',
Expand All @@ -246,6 +255,44 @@ def mock_column(name, type, options={})
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),
Expand Down