Skip to content
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby-pg-extras.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 13 additions & 3 deletions lib/ruby_pg_extras/missing_fk_indexes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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(
{
Expand Down
22 changes: 22 additions & 0 deletions spec/missing_fk_indexes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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