Skip to content

Refactor/Fix missing foreign key index detection + missing FK constraints heuristics and update tests#34

Merged
pawurb merged 4 commits intopawurb:mainfrom
chaadow:fix_missing_fk_indexes
Dec 16, 2025
Merged

Refactor/Fix missing foreign key index detection + missing FK constraints heuristics and update tests#34
pawurb merged 4 commits intopawurb:mainfrom
chaadow:fix_missing_fk_indexes

Conversation

@chaadow
Copy link
Contributor

@chaadow chaadow commented Dec 14, 2025

Missing FK indices Algorithm

  • Simplify the logic for detecting missing foreign key indexes by directly using foreign key information.
  • Update tests to reflect changes in foreign key and index detection

==> I went from 67 missing foreign keys ( with false positives ) to over 200 in my production application 💀. I checked manually and indeed, it turns out I have a LOT of missing indices. Which for me indicates that the DetectFkColumn was a bit weird ... EDIT: indeed the root cause was that cases such as user_account_id were returning user instead of user_account!

missing Fk constraints check heuristics algorithm

Enhance missing foreign key detection (missing_fk_constraints) logic and update tests

  • Refactor the logic to identify missing foreign key constraints by considering existing constraints and polymorphic associations
  • Add tests to ensure columns with existing foreign key constraints and polymorphic associations are correctly excluded from results.

Add ignore list to missing_fk_constraint check

  • Introduce a new IgnoreList class which can be later be used in other checks if needed. As input it takes an ignore list, then can determine if a column in a table is ignored based on the ignore list.

Enhance and Fix foreign key column detection logic

  • Refactor FkDetectColumn class to improve detection of foreign key columns, accommodating underscored prefixes and mixed-case names, such as user_account_id
  • Improve the pluralization edge cases

related to pawurb/rails-pg-extras#58

* Simplify the logic for detecting missing foreign key indexes by directly using foreign key information.
* Update tests to reflect changes in foreign key and index detection, including new expected results for users and posts tables.
* Modify the database schema in tests to include new foreign key relationships and columns for accurate testing.
…c and update tests

* Refactor the logic to identify missing foreign key constraints by considering existing constraints and polymorphic associations.
* Add tests to ensure columns with existing foreign key constraints and polymorphic associations are correctly excluded from results.
* Update the test database schema to include new tables for comprehensive testing.
@chaadow chaadow changed the title Refactor missing foreign key index detection and update tests Refactor/Fix missing foreign key index detection + missing FK constraints heuristics and update tests Dec 15, 2025
@chaadow
Copy link
Contributor Author

chaadow commented Dec 15, 2025

I'm going to push later the ignore list feature as well

and if I got time/succeed, I will have to improve the DetectFkColumn algorithm. i suspect we can better improve the pluralize part

…ive tests

* Refactor the `call` method to improve detection of foreign key columns, accommodating underscored prefixes and mixed-case names.
* Update the `pluralize` method to normalize table names before applying pluralization rules.
* Add new test cases to validate detection for various scenarios, including uncountable nouns and irregular plurals.
@chaadow
Copy link
Contributor Author

chaadow commented Dec 16, 2025

@pawurb I found the root cause for DetectFkColumn. I knew something was wrong when I went from 67 missing foreign keys to over 200 ( even with the algorithm change to directly check foreign keys from the database)

the main isuse was this line: table_name = column_name.split("_").first which unfortunately did not handle column names suchs user_account_id and was returning user instead of user_account

I've tested the fix on my production app, and with it I was able to detect 5 real missing FK constraints!

@chaadow
Copy link
Contributor Author

chaadow commented Dec 16, 2025

@pawurb I believe I'm done with the improvements and fixes, I don't have anything else for now. You can review / cut a release whenever you can!

@pawurb pawurb merged commit d1c6f6b into pawurb:main Dec 16, 2025
6 checks passed
@pawurb
Copy link
Owner

pawurb commented Dec 16, 2025

@chaadow thanks a lot! I'll proceed with releasing this and related rails-pg-extras changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants