-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from supabase/or/lint-no-primary-key
Add lint for no primary key
- Loading branch information
Showing
7 changed files
with
86 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
create view lint."0004_no_primary_key" as | ||
|
||
select | ||
'no_primary_key' as name, | ||
'INFO' as level, | ||
'EXTERNAL' as facing, | ||
'Detects if a table does not have a primary key. Tables without a primary key can be inefficient to interact with at scale.' as description, | ||
format( | ||
'Table "%s.%s" does not have a primary key', | ||
pgns.nspname, | ||
pgc.relname | ||
) as detail, | ||
null as remediation, | ||
null as metadata, | ||
format( | ||
'no_primary_key_%s_%s', | ||
pgns.nspname, | ||
pgc.relname | ||
) as cache_key | ||
from | ||
pg_class pgc | ||
join pg_namespace pgns | ||
on pgns.oid = pgc.relnamespace | ||
left join pg_index pgi | ||
on pgi.indrelid = pgc.oid | ||
where | ||
pgc.relkind = 'r' -- regular tables | ||
and pgns.nspname not in ( | ||
'pg_catalog', 'information_schema', 'auth', 'storage', 'vault', 'pgsodium' | ||
) | ||
group by | ||
pgc.oid, | ||
pgns.nspname, | ||
pgc.relname | ||
having | ||
max(coalesce(pgi.indisprimary, false)::int) = 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
begin; | ||
-- No issues | ||
select * from lint."0004_no_primary_key"; | ||
name | level | facing | description | detail | remediation | metadata | cache_key | ||
------+-------+--------+-------------+--------+-------------+----------+----------- | ||
(0 rows) | ||
|
||
-- Table with a primary key | ||
create table public.foo ( | ||
id int primary key | ||
); | ||
-- Table with a primary key | ||
create table public.bar ( | ||
id int | ||
); | ||
-- Only the "bar" table is listed | ||
select * from lint."0004_no_primary_key"; | ||
name | level | facing | description | detail | remediation | metadata | cache_key | ||
----------------+-------+----------+----------------------------------------------------------------------------------------------------------------------------+------------------------------------------------+-------------+----------+--------------------------- | ||
no_primary_key | INFO | EXTERNAL | Detects if a table does not have a primary key. Tables without a primary key can be inefficient to interact with at scale. | Table "public.bar" does not have a primary key | | | no_primary_key_public_bar | ||
(1 row) | ||
|
||
rollback; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
begin; | ||
|
||
-- No issues | ||
select * from lint."0004_no_primary_key"; | ||
|
||
-- Table with a primary key | ||
create table public.foo ( | ||
id int primary key | ||
); | ||
|
||
-- Table with a primary key | ||
create table public.bar ( | ||
id int | ||
); | ||
|
||
-- Only the "bar" table is listed | ||
select * from lint."0004_no_primary_key"; | ||
|
||
rollback; |