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

Convert ADD COLUMN SQL into pgroll operations #544

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ryanslade
Copy link
Collaborator

@ryanslade ryanslade commented Dec 17, 2024

Converts ALTER TABLE ADD COLUMN statements into pgroll operations. There are many combinations, at the moment we support the following:

ALTER TABLE foo ADD COLUMN bar int
ALTER TABLE foo ADD COLUMN bar int NOT NULL
ALTER TABLE schema.foo ADD COLUMN bar int
ALTER TABLE foo ADD COLUMN bar int DEFAULT 123
ALTER TABLE foo ADD COLUMN bar int DEFAULT 'baz'
ALTER TABLE foo ADD COLUMN bar int DEFAULT null
ALTER TABLE foo ADD COLUMN bar int NULL
ALTER TABLE foo ADD COLUMN bar int UNIQUE
ALTER TABLE foo ADD COLUMN bar int UNIQUE NOT DEFERRABLE
ALTER TABLE foo ADD COLUMN bar int UNIQUE INITIALLY IMMEDIATE
ALTER TABLE foo ADD COLUMN bar int PRIMARY KEY
ALTER TABLE foo ADD COLUMN bar int CHECK (bar > 0)
ALTER TABLE foo ADD COLUMN bar int CONSTRAINT check_bar CHECK (bar > 0)
ALTER TABLE foo ADD COLUMN bar int CONSTRAINT fk_baz REFERENCES baz (bar)
ALTER TABLE foo ADD COLUMN bar int CONSTRAINT fk_baz REFERENCES baz (bar) ON UPDATE NO ACTION
ALTER TABLE foo ADD COLUMN bar int CONSTRAINT fk_baz REFERENCES baz (bar) ON DELETE NO ACTION
ALTER TABLE foo ADD COLUMN bar int CONSTRAINT fk_baz REFERENCES baz (bar) ON DELETE RESTRICT
ALTER TABLE foo ADD COLUMN bar int CONSTRAINT fk_baz REFERENCES baz (bar) ON DELETE SET NULL 
ALTER TABLE foo ADD COLUMN bar int CONSTRAINT fk_baz REFERENCES baz (bar) ON DELETE SET DEFAULT
ALTER TABLE foo ADD COLUMN bar int CONSTRAINT fk_baz REFERENCES baz (bar) ON DELETE CASCADE

And fall back to raw SQL for these cases:

ALTER TABLE foo ADD COLUMN bar int REFERENCES bar (c) ON UPDATE RESTRICT
ALTER TABLE foo ADD COLUMN bar int REFERENCES bar (c) ON UPDATE CASCADE
ALTER TABLE foo ADD COLUMN bar int REFERENCES bar (c) ON UPDATE SET NULL
ALTER TABLE foo ADD COLUMN bar int REFERENCES bar (c) ON UPDATE SET DEFAULT
ALTER TABLE foo ADD COLUMN IF NOT EXISTS bar int
ALTER TABLE foo ADD COLUMN bar int UNIQUE DEFERRABLE
ALTER TABLE foo ADD COLUMN bar int UNIQUE INITIALLY DEFERRED
ALTER TABLE foo ADD COLUMN bar int GENERATED BY DEFAULT AS IDENTITY
ALTER TABLE foo ADD COLUMN bar int GENERATED ALWAYS AS ( 123 ) STORED

@ryanslade ryanslade force-pushed the rs/add-column branch 3 times, most recently from 31d1920 to 2491530 Compare December 18, 2024 09:46
@ryanslade ryanslade changed the title WIP Convert ADD COLUMN SQL into pgroll operations Dec 18, 2024
@ryanslade ryanslade marked this pull request as ready for review December 18, 2024 10:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant