Skip to content

Support database linters #560

@psteinroe

Description

@psteinroe

Our check command is currently only looking at files in the filesystem. To properly guard the user against issues, we also need to run checks directly against the database. There are a few tools out there that do exactly that, further validating the use-case:

  • https://github.com/supabase/splinter: it comes as a single query. not a database extension. we copy the query and run it ourselves. even supabase just copies it. Although I would prefer to not manage it ourselves, especially since supabase will probably allow users to enable / disable individual rules. maybe they could add it as an extension too, and expose a simple rpc-based api? since the rules are geared towards supabase, we could at least check if its a supabase project via the existence of certain schemas and only run it then.
  • https://github.com/pmpetit/pglinter: this one is basically the same as splinter, but distributed as a postgres extension. we could detect its existence.

what I am not sure about yet is whether we should integrate it into our existing check command. we could introduce a new location notion for diagnostics that points to a schema object and simply append it to the output from the file-based checks.

public.mytable.test_id splinter/performance ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  ⚠ Identifies foreign key constraints without a covering index, which can impact database performance.

  ℹ Table public.mytable has a foreign key test_id without a covering index. This can lead to suboptimal query performance.

Whether or not db checks should be includes could be controlled by a flag, e.g. no-schema-lints.

or we add a new command, e.g. check-db to distinguish. this would have improve clarity and we would not mix up cli flags that target files (e.g. —since) with the ones that run directly against the db (potentially —schema or similar filters).

In both cases, I would prefer to not bring the actual rules into this project, but detect their existence in the database and expose it to the user. the goal is an unified lint interface and a better dx (one CI lint to rule them all). We also should not allow configuring the respective rules directly if not necessary, and rather ask the user to make the settings directly via the api the tool itself provides (e.g. SELECT pglinter.disable_rule('B001');).

we could provide api-level settings though (next to disabled: bool). pglinter has functions to just check specific categories, so we could allow the user to choose them in our config too. this could lead to confusion though, since a user could achieve exactly the same by disabling individual rules from the different categories.

-- Quick comprehensive check (output to prompt)
SELECT pglinter.check_all();

-- Individual category checks (output to prompt)
SELECT pglinter.check_base();
SELECT pglinter.check_cluster();
SELECT pglinter.check_table();
SELECT pglinter.check_schema();

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions