Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding column with unique constraint is blocking #336

Closed
froque opened this issue Jan 8, 2024 · 1 comment · Fixed by #337
Closed

Adding column with unique constraint is blocking #336

froque opened this issue Jan 8, 2024 · 1 comment · Fixed by #337

Comments

@froque
Copy link
Contributor

froque commented Jan 8, 2024

ALTER TABLE IF EXISTS foo ADD COLUMN IF NOT EXISTS bar text CONSTRAINT foo_bar_unique UNIQUE;

also blocks updates while the index is built

❯ cat unique.sql            
ALTER TABLE IF EXISTS foo ADD COLUMN IF NOT EXISTS bar text CONSTRAINT foo_bar_unique UNIQUE;
❯ squawk unique.sql 
Found 0 issues in 1 file 🎉
❯ squawk --version  
squawk 0.26.0

Solution:

split into 3 steps:

step 1 - create column without constraint

ALTER TABLE IF EXISTS foo ADD COLUMN IF NOT EXISTS bar text;

step 2 - add unique index concurrently

 -- allows reads and writes while index is built
CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS foo_bar_idx ON foo (bar);

step 3 - create table constraint from unique index

ALTER TABLE foo ADD CONSTRAINT foo_bar_unique UNIQUE USING INDEX foo_bar_idx;
-- NOTICE:  ALTER TABLE / ADD CONSTRAINT USING INDEX will rename index "foo_bar_idx" to "foo_bar_unique"
@chdsbd
Copy link
Collaborator

chdsbd commented Jan 12, 2024

I think we could update the existing disallowed-unique-constraint rule to cover this

chdsbd added a commit that referenced this issue Jan 12, 2024
@kodiakhq kodiakhq bot closed this as completed in #337 Jan 13, 2024
kodiakhq bot pushed a commit that referenced this issue Jan 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants