Repository navigation
linter: add ban-set-null-on-not-null-column rule - #1420
MankhongGarden wants to merge 1 commit into
Conversation
👷 Deploy request for squawkhq pending review.Visit the deploys page to approve it
|
|
How often do you have the foreign key part in the same file as the part that creates the original table? I'm wondering if we need a more fully featured semantic model to support this |
|
Good question. From what I've seen, the most common case is a single migration that creates a new table together with its foreign keys, like the example in #1136. The The case it misses is a later migration that adds a
|
Warn when a foreign key uses `ON DELETE SET NULL` / `ON UPDATE SET NULL` on a column that is `NOT NULL` (including primary key columns) in the same file, since deleting/updating a referenced row will then fail. rel: sbdchd#1136
3d0f058 to
6a6e87f
Compare
Fixes #1136
Adds
ban-set-null-on-not-null-column(enabled by default). It warns when a foreign key usesON DELETE SET NULLorON UPDATE SET NULLon a column that isNOT NULL, since deleting/updating a referenced row then fails with a not-null violation.What it catches (within one file)
create tablewith aNOT NULLcolumn and a column-levelreferences ... on delete set nullor a table-levelforeign key (...) ... on delete set nullprimary key (...)), since they're implicitlyNOT NULLon update set null, same ason delete set nullon delete set null (col, ...): only the listed columns are checkedalter table ... add column ... not null references ... on delete set nullalter table ... add constraint ... foreign key ... on delete set nullon a column already known to beNOT NULL(fromcreate table,add column, oralter column ... set not null)alter column ... set not nullon a column that already has aSET NULLforeign key in the file. This one is reported on theset not nullstatement.State is reset by
drop not null,drop constraint <fk name>,drop table, and re-creating the table.What it doesn't catch
NOT NULLconstraints from earlier migration files. Squawk only sees the current file.messagesandpublic.messagesare treated as different tables.NOT NULLthat comes from domains,CHECK (col IS NOT NULL),LIKE/INHERITS/PARTITION OF, orADD PRIMARY KEY USING INDEX.Testing
cargo insta test -p squawk-linter --check --unreferenced reject: 17 new tests (10 snapshot error cases, 7 ok cases). Allsquawk-lintertests pass exceptignore::test::starting_line_aka_zero, which fails on Windows on unmodifiedmastertoo (the fix emits\r\n, but the snapshot expects\n).cargo clippy -p squawk-linter --all-targets --all-features -- -D warnings: cleancargo fmt -- --check: cleanI only ran these locally on Windows with the GNU toolchain. I couldn't build crates that depend on
mimalloc(squawk,squawk-server,squawk-wasm,xtask) because there's no C compiler on that machine. So I added the rule stubs by hand, following whatcargo xtask new-rulegenerates, and I haven't run workspace-widecargo clippy/cargo check. CI should cover those. No other crate's test inputs containSET NULL, so I don't expect any other snapshots to change.