sql-lint
sql-lint copied to clipboard
Add support for PostgreSQL DO statements
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
DOkeyword tosrc/syntax/keywords.ts - Created PostgreSQL
DOstatement handler atsrc/lexer/statements/postgres/do.tsfollowing the same pattern as existing statement handlers - Updated statement factory in
src/lexer/statementFactory.tsto 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
DOstatements - no more "unable to lint" errors - Verified the linter now recognizes
DOas 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.