squawk icon indicating copy to clipboard operation
squawk copied to clipboard

`adding-not-nullable-field` not succeeding for pg >= 12

Open conanjen opened this issue 4 months ago • 1 comments

Likely related to #300 and #519, but following the rules solution for adding-not-nullable-field on pg-version 16 and using the additional ALTER COLUMN bar SET NOT NULL as suggested here (https://squawkhq.com/docs/adding-not-nullable-field) raises an error.

Attached a minimal reproduction case:

-- test.sql
BEGIN;

ALTER TABLE foo ADD COLUMN bar BIGINT;

ALTER TABLE foo ADD CONSTRAINT bar_not_null CHECK (bar IS NOT NULL) NOT VALID;

COMMIT;

BEGIN;

ALTER TABLE foo VALIDATE CONSTRAINT bar_not_null;

ALTER TABLE foo ALTER COLUMN bar SET NOT NULL;

ALTER TABLE foo DROP CONSTRAINT bar_not_null;

COMMIT;
squawk test.sql --pg-version 16

warning[adding-not-nullable-field]: Setting a column `NOT NULL` blocks reads while the table is scanned.
  --> test.sql:15:34
   |
15 | ALTER TABLE foo ALTER COLUMN bar SET NOT NULL;
   |                                  ------------
   |
   = help: Make the field nullable and use a `CHECK` constraint instead.

Find detailed examples and solutions for each rule at https://squawkhq.com/docs/rules
Found 1 issue in 1 file (checked 1 source file)

conanjen avatar Aug 25 '25 20:08 conanjen

Oh nice find, looks like we're not checking for the postgres version in the rule:

https://github.com/sbdchd/squawk/blob/412466cb8648125ab2a5c1206ac97aec8354224a/crates/squawk_linter/src/rules/adding_not_null_field.rs#L18C55-L18C55

Edit: Oh we used to not have a check, then we added one, then the rewrite removed it, and then we added it back. Need to think about this more

sbdchd avatar Aug 25 '25 22:08 sbdchd