Skip to content

Commit

Permalink
Fix ignore_columns in missing_unique_indexes
Browse files Browse the repository at this point in the history
This commit fixes a problem that made it impossible to ignore the right column
when working with a has_one association. The root cause was looking at the model
containing the has_one relationship instead of the one being referred to, which
is the model whose table contains the actual foreign key.
  • Loading branch information
fatkodima authored Dec 4, 2023
1 parent d322a6e commit b013fc0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def has_ones_without_indexes # rubocop:disable Naming/PredicateName
else
[has_one.foreign_key.to_s]
end
next if ignored?("#{model.name}(#{columns.join(',')})", ignore_columns)
next if ignored?("#{has_one.klass.name}(#{columns.join(',')})", ignore_columns)

table_name = has_one.klass.table_name
next if unique_index?(table_name, columns)
Expand Down
19 changes: 19 additions & 0 deletions test/active_record_doctor/detectors/missing_unique_indexes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,25 @@ def test_config_ignore_columns
refute_problems
end

def test_config_ignore_columns_for_has_one
Context.create_table(:users).define_model do
has_one :account, class_name: "Context::Account"
end

Context.create_table(:accounts) do |t|
t.integer :user_id
end.define_model

config_file(<<-CONFIG)
ActiveRecordDoctor.configure do |config|
config.detector :missing_unique_indexes,
ignore_columns: ["Context::Account(user_id)"]
end
CONFIG

refute_problems
end

class DummyValidator < ActiveModel::Validator
def validate(record)
end
Expand Down

0 comments on commit b013fc0

Please sign in to comment.