Skip to content

Commit faca2a4

Browse files
authored
Enhance missing_fk_indexes to support ignore_list
1 parent 977e405 commit faca2a4

4 files changed

Lines changed: 46 additions & 4 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,16 @@ RubyPgExtras.missing_fk_indexes(args: { table_name: "users" })
136136

137137
```
138138

139+
You can also exclude known/intentional cases using `ignore_list` (array or comma-separated string), with entries like:
140+
- `"posts.topic_id"` (ignore a specific table+column)
141+
- `"topic_id"` (ignore this column name for all tables)
142+
- `"posts.*"` (ignore all columns on a table)
143+
- `"*"` (ignore everything)
144+
145+
```ruby
146+
RubyPgExtras.missing_fk_indexes(args: { ignore_list: ["users.company_id", "posts.*"] })
147+
```
148+
139149
`table_name` argument is optional, if omitted, the method will display missing fk indexes for all the tables.
140150

141151
## `missing_fk_constraints`

lib/ruby-pg-extras.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def self.table_info(args: {}, in_format: :display_table)
192192
end
193193

194194
def self.missing_fk_indexes(args: {}, in_format: :display_table)
195-
RubyPgExtras::MissingFkIndexes.call(args[:table_name])
195+
RubyPgExtras::MissingFkIndexes.call(args[:table_name], ignore_list: args[:ignore_list])
196196
end
197197

198198
def self.missing_fk_constraints(args: {}, in_format: :display_table)

lib/ruby_pg_extras/missing_fk_indexes.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22

33
module RubyPgExtras
44
class MissingFkIndexes
5-
def self.call(table_name)
6-
new.call(table_name)
5+
# ignore_list: array (or comma-separated string) of entries like:
6+
# - "posts.topic_id" (ignore a specific table+column)
7+
# - "topic_id" (ignore this column name for all tables)
8+
# - "posts.*" (ignore all columns on a table)
9+
# - "*" (ignore everything)
10+
def self.call(table_name, ignore_list: nil)
11+
new.call(table_name, ignore_list: ignore_list)
712
end
813

9-
def call(table_name)
14+
def call(table_name, ignore_list: nil)
15+
ignore_list_matcher = IgnoreList.new(ignore_list)
16+
1017
indexes_info = query_module.indexes(in_format: :hash)
1118
foreign_keys = query_module.foreign_keys(in_format: :hash)
1219

@@ -23,6 +30,9 @@ def call(table_name)
2330
table_fks.each do |fk|
2431
column_name = fk.fetch("column_name")
2532

33+
# Skip columns explicitly excluded via ignore list.
34+
next if ignore_list_matcher.ignored?(table: table, column_name: column_name)
35+
2636
if index_info.none? { |row| row.fetch("columns").split(",").first == column_name }
2737
agg.push(
2838
{

spec/missing_fk_indexes_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,26 @@
1818
{ table: "posts", column_name: "topic_id" },
1919
])
2020
end
21+
22+
it "supports ignoring a specific table+column via args" do
23+
result = RubyPgExtras.missing_fk_indexes(
24+
args: { ignore_list: ["posts.topic_id"] },
25+
in_format: :hash
26+
)
27+
28+
expect(result).to eq([
29+
{ table: "users", column_name: "company_id" },
30+
])
31+
end
32+
33+
it "supports ignoring a column name globally via args" do
34+
result = RubyPgExtras.missing_fk_indexes(
35+
args: { ignore_list: ["company_id"] },
36+
in_format: :hash
37+
)
38+
39+
expect(result).to eq([
40+
{ table: "posts", column_name: "topic_id" },
41+
])
42+
end
2143
end

0 commit comments

Comments
 (0)