sql-lint icon indicating copy to clipboard operation
sql-lint copied to clipboard

Add support for PostgreSQL DO statements

Open AlexVulaj opened this issue 3 months ago • 0 comments

Summary

Adds support for PostgreSQL DO statements to the SQL linter. Previously, the linter would fail with "sql-lint was unable to lint the following query" errors when encountering DO anonymous code blocks, which are commonly used in PostgreSQL migration files.

Background

The linter was throwing parsing errors on valid PostgreSQL migration files containing DO statements like:

  DO $$
  BEGIN
      IF NOT EXISTS (
          SELECT 1 FROM information_schema.table_constraints
          WHERE constraint_name = 'fk_example_constraint'
          AND table_name = 'example_table'
      ) THEN
          ALTER TABLE example_table ADD CONSTRAINT fk_example_constraint
          FOREIGN KEY (example_id) REFERENCES other_table(id);
      END IF;
  END $$;

Changes

  • Added DO keyword to src/syntax/keywords.ts
  • Created PostgreSQL DO statement handler at src/lexer/statements/postgres/do.ts following the same pattern as existing statement handlers
  • Updated statement factory in src/lexer/statementFactory.ts to recognize and handle do statements
  • Added barrel export in src/barrel/statements.ts

The implementation follows the existing codebase patterns and conventions used by other PostgreSQL statement handlers like ALTER, CREATE, etc.

Test Plan

  • Built project successfully with npm run build
  • Tested with migration files containing DO statements - no more "unable to lint" errors
  • Verified the linter now recognizes DO as valid SQL syntax

Impact

This resolves parsing failures for PostgreSQL migration files that use DO statements, allowing the SQL linter to process these files without errors.

AlexVulaj avatar Sep 26 '25 20:09 AlexVulaj