Warn about unindexed foreign key constraints.
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
Research direction
Start by reviewing the proposed PostgreSQL query and determine how it would integrate with the linter's running-database analysis. The issue provides no file or test entry point; done would mean warning about foreign key constraints without a suitable index while avoiding false positives.
Written by the indexing model from the issue text.
Description
I'd like to have is a warning that a foreign key constraint isn't indexed. Static analysis of the migration file can't provide enough information to eliminate false positives for such a rule, so querying a running database would be required.
Such a query might look like this:
WITH indexes AS (
SELECT
n.nspname as schema_name,
t.relname as table_name,
a.attname as column_name
FROM
pg_class t
JOIN
pg_index i ON t.oid = i.indrelid
JOIN
pg_attribute a ON a.attrelid = t.oid AND a.attnum = ANY(i.indkey)
JOIN
pg_namespace n ON t.relnamespace = n.oid
WHERE
t.relkind = 'r' -- real tables
AND array_position(i.indkey, a.attnum) = 0 -- only first column of possibly compound index
),
foreign_keys AS (
SELECT
n.nspname AS schema_name,
cl.relname AS table_name,
a.attname AS column_name,
ct.conname AS constraint_name
FROM
pg_constraint ct
JOIN pg_class cl ON ct.conrelid = cl.oid
JOIN pg_namespace n ON cl.relnamespace = n.oid
JOIN pg_attribute a ON a.attnum = ANY(ct.conkey) AND a.attrelid = cl.oid
WHERE
ct.contype = 'f'
)
SELECT
schema_name as "schema!",
table_name as "table!",
column_name as "name!"
FROM foreign_keys
LEFT JOIN indexes USING (schema_name, table_name, column_name)
WHERE indexes.column_name IS NULL
AND schema_name = ANY($1)
(query is not perfect but you get the idea)
- Dominant language
- Rust
- Stars
- 1.2k
- Forks
- 70
- Avg merge
- 52m
- Merged PRs (30d)
- 47
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from sbdchd/squawk
-
enhancement
Difficulty 4/5 3-5 days Newbie friendliness 48/100
-
enhancement
Difficulty 3/5 1-2 days Newbie friendliness 58/100
-
enhancement
Difficulty 3/5 1-2 days Newbie friendliness 56/100
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
-
enhancement
Difficulty 5/5 Over a week Newbie friendliness 35/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
kwakseongjae/auto-hwp#319 ·
-
area:cli bug filter-quality good first issue priority:medium
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100
bevyengine/bevy#25861 ·
-
comp-datalake
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
ClickHouse/ClickHouse#121222 ·
-
enhancement remote
Difficulty 2/5 1-3 hours Newbie friendliness 68/100