Skip to content

Quotes foreign constraint names #19

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions packages/ast/deploy/schemas/deparser/procedures/deparse.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1772,8 +1772,7 @@ BEGIN
IF (has_pk_attrs AND has_fk_attrs) THEN
IF (node->'conname' IS NOT NULL) THEN
output = array_append(output, 'CONSTRAINT');
-- TODO needs quote?
output = array_append(output, node->>'conname');
output = array_append(output, quote_ident(node->>'conname'));
END IF;
output = array_append(output, 'FOREIGN KEY');
output = array_append(output, deparser.parens(deparser.list_quotes(node->'fk_attrs')));
Expand All @@ -1787,8 +1786,7 @@ BEGIN
ELSIF (has_fk_attrs) THEN
IF (node->'conname' IS NOT NULL) THEN
output = array_append(output, 'CONSTRAINT');
-- TODO needs quote?
output = array_append(output, node->>'conname');
output = array_append(output, quote_ident(node->>'conname'));
END IF;
output = array_append(output, 'FOREIGN KEY');
output = array_append(output, deparser.parens(deparser.list_quotes(node->'fk_attrs')));
Expand Down
6 changes: 2 additions & 4 deletions packages/ast/sql/ast--13.0.4.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6989,8 +6989,7 @@ BEGIN
IF (has_pk_attrs AND has_fk_attrs) THEN
IF (node->'conname' IS NOT NULL) THEN
output = array_append(output, 'CONSTRAINT');
-- TODO needs quote?
output = array_append(output, node->>'conname');
output = array_append(output, quote_ident(node->>'conname'));
END IF;
output = array_append(output, 'FOREIGN KEY');
output = array_append(output, deparser.parens(deparser.list_quotes(node->'fk_attrs')));
Expand All @@ -7004,8 +7003,7 @@ BEGIN
ELSIF (has_fk_attrs) THEN
IF (node->'conname' IS NOT NULL) THEN
output = array_append(output, 'CONSTRAINT');
-- TODO needs quote?
output = array_append(output, node->>'conname');
output = array_append(output, quote_ident(node->>'conname'));
END IF;
output = array_append(output, 'FOREIGN KEY');
output = array_append(output, deparser.parens(deparser.list_quotes(node->'fk_attrs')));
Expand Down
36 changes: 20 additions & 16 deletions packages/ast/test/__fixtures__/alter/alter.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,62 +24,66 @@ ALTER TABLE foo
ALTER COLUMN foo_timestamp SET DEFAULT now();

ALTER TABLE scha.foo
ADD CONSTRAINT my_constraint_fey
ADD CONSTRAINT my_constraint_key
FOREIGN KEY (order_id) REFERENCES othr.orders (id);

ALTER TABLE scha.foo
ADD CONSTRAINT my_constraint_fey
ADD CONSTRAINT "MyConstraintKey"
FOREIGN KEY (order_id) REFERENCES othr.orders (id);

ALTER TABLE scha.foo
ADD CONSTRAINT my_constraint_key
FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON DELETE CASCADE;

ALTER TABLE scha.foo
ADD CONSTRAINT my_constraint_fey
ADD CONSTRAINT my_constraint_key
FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON DELETE RESTRICT;

ALTER TABLE scha.foo
ADD CONSTRAINT my_constraint_fey
ADD CONSTRAINT my_constraint_key
FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON DELETE NO ACTION;

ALTER TABLE scha.foo
ADD CONSTRAINT my_constraint_fey
ADD CONSTRAINT my_constraint_key
FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON DELETE SET NULL;

ALTER TABLE scha.foo
ADD CONSTRAINT my_constraint_fey
ADD CONSTRAINT my_constraint_key
FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON UPDATE SET DEFAULT;

ALTER TABLE scha.foo
ADD CONSTRAINT my_constraint_fey
ADD CONSTRAINT my_constraint_key
FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON UPDATE CASCADE;

ALTER TABLE scha.foo
ADD CONSTRAINT my_constraint_fey
ADD CONSTRAINT my_constraint_key
FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON UPDATE RESTRICT;

ALTER TABLE scha.foo
ADD CONSTRAINT my_constraint_fey
ADD CONSTRAINT my_constraint_key
FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON UPDATE NO ACTION;

ALTER TABLE scha.foo
ADD CONSTRAINT my_constraint_fey
ADD CONSTRAINT my_constraint_key
FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON UPDATE SET NULL;

ALTER TABLE scha.foo
ADD CONSTRAINT my_constraint_fey
ADD CONSTRAINT my_constraint_key
FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON UPDATE SET DEFAULT;

ALTER TABLE scha.foo
ADD CONSTRAINT my_constraint_fey
ADD CONSTRAINT my_constraint_key
FOREIGN KEY (order_id) REFERENCES othr.orders (id)
ON UPDATE SET DEFAULT
ON DELETE SET NULL
;

ALTER TABLE scha.foo
ADD CONSTRAINT my_constraint_fey
ADD CONSTRAINT my_constraint_key
FOREIGN KEY (a,b) REFERENCES othr.orders (c,d) MATCH FULL;

ALTER TABLE scha.foo
ADD CONSTRAINT my_constraint_fey
ADD CONSTRAINT my_constraint_key
FOREIGN KEY (a, b) REFERENCES othr.orders (c,d) MATCH SIMPLE;

ALTER TABLE ONLY collections.mfield
Expand All @@ -92,7 +96,7 @@ ALTER TABLE collections.mfield
-- TODO MATCH after upgrading to newer engine: https://github.com/lfittl/libpg_query/issues/66

-- ALTER TABLE scha.foo
-- ADD CONSTRAINT my_constraint_fey
-- ADD CONSTRAINT my_constraint_key
-- FOREIGN KEY (order_id) REFERENCES othr.orders (id)
-- MATCH FULL
-- ON UPDATE SET DEFAULT
Expand All @@ -102,7 +106,7 @@ ALTER TABLE collections.mfield
-- MATCH PARTIAL not yet implemented

-- ALTER TABLE scha.foo
-- ADD CONSTRAINT my_constraint_fey
-- ADD CONSTRAINT my_constraint_key
-- FOREIGN KEY (a,b) REFERENCES othr.orders (c,d) MATCH PARTIAL;

ALTER TABLE schema_name.table_name ALTER COLUMN column_name SET DATA TYPE new_column_type USING column_name::new_column_type;
Expand Down
58 changes: 30 additions & 28 deletions packages/ast/test/__tests__/__snapshots__/kitchen-sink.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -373,20 +373,21 @@ foo_timestamp timestampz DEFAULT (CURRENT_DATE)
);
ALTER TABLE foo RENAME COLUMN name TO city;
ALTER TABLE foo ALTER COLUMN foo_timestamp DROP DEFAULT, ALTER COLUMN foo_timestamp TYPE pg_catalog.timestamptz USING 'epoch'::pg_catalog.timestamptz + (foo_timestamp * '1 second'::interval), ALTER COLUMN foo_timestamp SET DEFAULT now();
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY (order_id) REFERENCES othr.orders (id) ;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON DELETE CASCADE ;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON DELETE RESTRICT ;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY (order_id) REFERENCES othr.orders (id) ;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON DELETE SET NULL ;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON UPDATE SET DEFAULT;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON UPDATE CASCADE;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON UPDATE RESTRICT;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY (order_id) REFERENCES othr.orders (id) ;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON UPDATE SET NULL;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON UPDATE SET DEFAULT;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON DELETE SET NULL ON UPDATE SET DEFAULT;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY (a, b) REFERENCES othr.orders (c, d) MATCH FULL;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY (a, b) REFERENCES othr.orders (c, d) ;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY (order_id) REFERENCES othr.orders (id) ;
ALTER TABLE scha.foo ADD CONSTRAINT \\"MyConstraintKey\\" FOREIGN KEY (order_id) REFERENCES othr.orders (id) ;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON DELETE CASCADE ;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON DELETE RESTRICT ;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY (order_id) REFERENCES othr.orders (id) ;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON DELETE SET NULL ;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON UPDATE SET DEFAULT;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON UPDATE CASCADE;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON UPDATE RESTRICT;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY (order_id) REFERENCES othr.orders (id) ;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON UPDATE SET NULL;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON UPDATE SET DEFAULT;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY (order_id) REFERENCES othr.orders (id) ON DELETE SET NULL ON UPDATE SET DEFAULT;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY (a, b) REFERENCES othr.orders (c, d) MATCH FULL;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY (a, b) REFERENCES othr.orders (c, d) ;
ALTER TABLE ONLY collections.mfield ADD CONSTRAINT col_field_pkey PRIMARY KEY (id);
ALTER TABLE collections.mfield ADD CONSTRAINT col_field_pkey PRIMARY KEY (id);
ALTER TABLE schema_name.table_name ALTER COLUMN column_name TYPE new_column_type USING column_name::new_column_type;
Expand Down Expand Up @@ -481,20 +482,21 @@ foo_timestamp timestampz DEFAULT ( CURRENT_DATE )
);
ALTER TABLE foo RENAME name TO city;
ALTER TABLE foo ALTER COLUMN foo_timestamp DROP DEFAULT, ALTER COLUMN foo_timestamp TYPE pg_catalog.timestamptz USING 'epoch'::pg_catalog.timestamptz + (foo_timestamp * '1 second'::interval), ALTER COLUMN foo_timestamp SET DEFAULT now();
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id );
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id ) ON DELETE CASCADE;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id ) ON DELETE RESTRICT;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id );
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id ) ON DELETE SET NULL;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id ) ON UPDATE SET DEFAULT;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id ) ON UPDATE CASCADE;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id ) ON UPDATE RESTRICT;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id );
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id ) ON UPDATE SET NULL;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id ) ON UPDATE SET DEFAULT;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id ) ON DELETE SET NULL ON UPDATE SET DEFAULT;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY ( a, b ) REFERENCES othr.orders ( c, d ) MATCH FULL;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_fey FOREIGN KEY ( a, b ) REFERENCES othr.orders ( c, d );
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id );
ALTER TABLE scha.foo ADD CONSTRAINT \\"MyConstraintKey\\" FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id );
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id ) ON DELETE CASCADE;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id ) ON DELETE RESTRICT;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id );
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id ) ON DELETE SET NULL;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id ) ON UPDATE SET DEFAULT;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id ) ON UPDATE CASCADE;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id ) ON UPDATE RESTRICT;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id );
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id ) ON UPDATE SET NULL;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id ) ON UPDATE SET DEFAULT;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY ( order_id ) REFERENCES othr.orders ( id ) ON DELETE SET NULL ON UPDATE SET DEFAULT;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY ( a, b ) REFERENCES othr.orders ( c, d ) MATCH FULL;
ALTER TABLE scha.foo ADD CONSTRAINT my_constraint_key FOREIGN KEY ( a, b ) REFERENCES othr.orders ( c, d );
ALTER TABLE ONLY collections.mfield ADD CONSTRAINT col_field_pkey PRIMARY KEY ( id );
ALTER TABLE collections.mfield ADD CONSTRAINT col_field_pkey PRIMARY KEY ( id );
ALTER TABLE schema_name.table_name ALTER COLUMN column_name TYPE new_column_type USING column_name::new_column_type;
Expand Down