Skip to content
Open
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
11 changes: 8 additions & 3 deletions lib/lol_dba.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def self.validate_and_sort_indexes(indexes_required)
keys_to_add = foreign_keys.uniq - existing_indexes
missing_indexes[table_name] = keys_to_add unless keys_to_add.empty?
else
warning_messages << "BUG: table '#{table_name.to_s}' does not exist, please report this bug.\n "
warning_messages << "BUG: table '#{table_name.to_s}' does not exist, please report this bug.\n"
end
rescue Exception => e
puts "ERROR: #{e}"
Expand Down Expand Up @@ -112,8 +112,13 @@ def self.check_for_indexes(migration_format = false)
index_name = foreign_key.to_s
end
when :has_and_belongs_to_many
table_name = reflection_options.options[:join_table]
table_name ||= [class_name.table_name, reflection_name.to_s].sort.join('_')
# in older rails versions, join_table is not present
if reflection_options.respond_to?(:join_table)
table_name = reflection_options.join_table
else
table_name ||= reflection_options.options[:join_table]
table_name ||= [class_name.table_name, reflection_options.table_name].sort.join('_')
end
association_foreign_key = reflection_options.options[:association_foreign_key] ||= "#{reflection_name.to_s.singularize}_id"

foreign_key = get_through_foreign_key(class_name, reflection_options)
Expand Down
16 changes: 12 additions & 4 deletions spec/associations_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
before :all do
lol_dba = LolDba.check_for_indexes
@relationship_indexes = lol_dba[0]
@warning_messages = lol_dba[1]
@warning_messages = lol_dba[1].split("\n")
end

it "find relationship indexes" do
Expand Down Expand Up @@ -62,9 +62,11 @@
end

it "have warnings(non-existent table) on test data" do
expect(@warning_messages).not_to be_empty
expect(@warning_messages).to match(/\'wrongs\'/)
expect(@warning_messages).to match(/\'addresses_wrongs\'/)
expected_warnings = [
"BUG: table 'wrongs' does not exist, please report this bug.",
"BUG: table 'addresses_wrongs' does not exist, please report this bug."
]
expect(@warning_messages).to match_array(expected_warnings)
end

it "find indexes for STI" do
Expand All @@ -75,6 +77,12 @@
expect(@relationship_indexes["freelancers"]).to include(["id", "worker_type"])
end

it "finds the right join table for HABTM for an STI subclass" do
expect(@relationship_indexes).not_to have_key('companies_worker_users')
expect(@relationship_indexes).to have_key('companies_users')
expect(@relationship_indexes["companies_users"]).to include(["company_id", "user_id"])
end

it "find indexes, than use custom class name option in association" do
expect(@relationship_indexes["employers_freelancers"]).to be_nil
expect(@relationship_indexes["companies_freelancers"]).to include(["company_id", "freelancer_id"])
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/app/models/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ class Company < ActiveRecord::Base
has_many :users

has_and_belongs_to_many :freelancers
has_and_belongs_to_many :worker_users, join_table: 'companies_users', association_foreign_key: 'user_id'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing the join_table: option here actually undermines the original intention of this PR, which was to make sure that the right join table is detected for Rails >= 4 without having this option set.
However, It should work now, even if it is not explicitly tested.

end
5 changes: 5 additions & 0 deletions spec/fixtures/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
t.column "company_id", :integer
end

create_table "companies_users", :id => false, :force => true do |t|
t.column "company_id", :integer
t.column "user_id", :integer
end

create_table "gifts", :primary_key => "custom_primary_key", :force => true do |t|
t.column "name", :string
t.column "price", :integer
Expand Down