I’m using SQLFluff for formatting SQL files and trying to replicate the behavior of JetBrains IDEs where types are perfectly aligned under each other, and constraints (e.g., NOT NULL) dynamically adjust with spaces for proper alignment. Here’s an example of the alignment I’m trying to achieve:
CREATE TABLE products (
product_id BIGINT PRIMARY KEY,
name TEXT NOT NULL,
description TEXT,
price NUMERIC(10, 2) NOT NULL,
in_stock BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP DEFAULT now()
);
The key formatting features I’m looking for are:
- Dynamic type alignment: Types (TEXT, BIGINT, NUMERIC) should align under each other.
- Constraint alignment: Keywords like NOT NULL, DEFAULT, or PRIMARY KEY should also align dynamically.
I’ve explored the following SQLFluff configuration: • layout.indent: For consistent indentation. • layout.spacing: To prevent extra spaces. • Rules like layout.type:datatype and layout.type:column_constraint for alignment.
Here’s an excerpt from my .sqlfluff file:
[sqlfluff]
dialect = postgres
[sqlfluff:rules:layout.indent]
indent_unit = space
tab_space_size = 4
[sqlfluff:layout:type:datatype]
align_with = type
[sqlfluff:layout:type:column_constraint]
align_with = type
Any help or suggestions would be greatly appreciated!