From 21e151bbd1dcf6b633c49ab8022f52f98f8cb99c Mon Sep 17 00:00:00 2001 From: chaadow Date: Mon, 5 Jan 2026 23:07:47 +0100 Subject: [PATCH] Enhance missing_fk_indexes to support ignore_list for excluding specific columns and tables --- README.md | 10 ++++++++++ lib/ruby-pg-extras.rb | 2 +- lib/ruby_pg_extras/missing_fk_indexes.rb | 16 +++++++++++++--- spec/missing_fk_indexes_spec.rb | 22 ++++++++++++++++++++++ 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 51070e5..2ab1f77 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,16 @@ RubyPgExtras.missing_fk_indexes(args: { table_name: "users" }) ``` +You can also exclude known/intentional cases using `ignore_list` (array or comma-separated string), with entries like: +- `"posts.topic_id"` (ignore a specific table+column) +- `"topic_id"` (ignore this column name for all tables) +- `"posts.*"` (ignore all columns on a table) +- `"*"` (ignore everything) + +```ruby +RubyPgExtras.missing_fk_indexes(args: { ignore_list: ["users.company_id", "posts.*"] }) +``` + `table_name` argument is optional, if omitted, the method will display missing fk indexes for all the tables. ## `missing_fk_constraints` diff --git a/lib/ruby-pg-extras.rb b/lib/ruby-pg-extras.rb index 736618b..f3a151b 100644 --- a/lib/ruby-pg-extras.rb +++ b/lib/ruby-pg-extras.rb @@ -192,7 +192,7 @@ def self.table_info(args: {}, in_format: :display_table) end def self.missing_fk_indexes(args: {}, in_format: :display_table) - RubyPgExtras::MissingFkIndexes.call(args[:table_name]) + RubyPgExtras::MissingFkIndexes.call(args[:table_name], ignore_list: args[:ignore_list]) end def self.missing_fk_constraints(args: {}, in_format: :display_table) diff --git a/lib/ruby_pg_extras/missing_fk_indexes.rb b/lib/ruby_pg_extras/missing_fk_indexes.rb index 257c441..bbfc626 100644 --- a/lib/ruby_pg_extras/missing_fk_indexes.rb +++ b/lib/ruby_pg_extras/missing_fk_indexes.rb @@ -2,11 +2,18 @@ module RubyPgExtras class MissingFkIndexes - def self.call(table_name) - new.call(table_name) + # ignore_list: array (or comma-separated string) of entries like: + # - "posts.topic_id" (ignore a specific table+column) + # - "topic_id" (ignore this column name for all tables) + # - "posts.*" (ignore all columns on a table) + # - "*" (ignore everything) + def self.call(table_name, ignore_list: nil) + new.call(table_name, ignore_list: ignore_list) end - def call(table_name) + def call(table_name, ignore_list: nil) + ignore_list_matcher = IgnoreList.new(ignore_list) + indexes_info = query_module.indexes(in_format: :hash) foreign_keys = query_module.foreign_keys(in_format: :hash) @@ -23,6 +30,9 @@ def call(table_name) table_fks.each do |fk| column_name = fk.fetch("column_name") + # Skip columns explicitly excluded via ignore list. + next if ignore_list_matcher.ignored?(table: table, column_name: column_name) + if index_info.none? { |row| row.fetch("columns").split(",").first == column_name } agg.push( { diff --git a/spec/missing_fk_indexes_spec.rb b/spec/missing_fk_indexes_spec.rb index 462b068..8ced421 100644 --- a/spec/missing_fk_indexes_spec.rb +++ b/spec/missing_fk_indexes_spec.rb @@ -18,4 +18,26 @@ { table: "posts", column_name: "topic_id" }, ]) end + + it "supports ignoring a specific table+column via args" do + result = RubyPgExtras.missing_fk_indexes( + args: { ignore_list: ["posts.topic_id"] }, + in_format: :hash + ) + + expect(result).to eq([ + { table: "users", column_name: "company_id" }, + ]) + end + + it "supports ignoring a column name globally via args" do + result = RubyPgExtras.missing_fk_indexes( + args: { ignore_list: ["company_id"] }, + in_format: :hash + ) + + expect(result).to eq([ + { table: "posts", column_name: "topic_id" }, + ]) + end end