Skip to content

Exclude partial indexes when determining if a foreign key is indexed in specs

Tiger Watson requested to merge exclude-partial-indexes-from-schema-spec into master

What does this MR do and why?

In spec/db/schema_spec.rb we assert that every foreign key column must have an index. However, we don't consider that the index may be a partial index and only cover a small subset of the rows in the table. This can lead to a false positive, where the spec passes but the column does not have index coverage, and cause problems such as #366615 (closed).

In order for the foreign key to function efficiently, the index must either:

  • Cover the entire table (ie have no WHERE condition), or
  • Be a partial index with a condition that exactly matches WHERE (<foreign_key_column> IS NOT NULL)

With these changes, indexes that don't meet one of the above conditions will cause schema_spec.rb to fail.

Screenshots or screen recordings

These are strongly recommended to assist reviewers and reduce the time to merge your change.

How to set up and validate locally

Numbered steps to set up and validate the change are strongly suggested.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Tiger Watson

Merge request reports