Skip to content

Commit 94349f8

Browse files
committed
ast updates
1 parent e5d5b88 commit 94349f8

File tree

7 files changed

+88
-47
lines changed

7 files changed

+88
-47
lines changed

packages/ast/deploy/schemas/deparser/procedures/deparse.sql

+16-14
Original file line numberDiff line numberDiff line change
@@ -4343,21 +4343,17 @@ BEGIN
43434343

43444344
node = node->'AlterOwnerStmt';
43454345
objectType = node->>'objectType';
4346-
IF (
4347-
objectType = 'OBJECT_FUNCTION' OR
4348-
objectType = 'OBJECT_FOREIGN_TABLE' OR
4349-
objectType = 'OBJECT_FDW' OR
4350-
objectType = 'OBJECT_FOREIGN_SERVER'
4351-
) THEN
4352-
output = array_append(output, 'ALTER');
4353-
output = array_append(output, ast_utils.objtype_name(objectType) );
4354-
output = array_append(output, deparser.expression(node->'object'));
4355-
output = array_append(output, 'OWNER');
4356-
output = array_append(output, 'TO');
4357-
output = array_append(output, deparser.role_spec(node->'newowner'));
4346+
4347+
output = array_append(output, 'ALTER');
4348+
output = array_append(output, ast_utils.objtype_name(objectType) );
4349+
IF (jsonb_typeof(node->'object') = 'array') THEN
4350+
output = array_append(output, deparser.list_quotes(node->'object', '.'));
43584351
ELSE
4359-
RAISE EXCEPTION 'BAD_EXPRESSION %', 'AlterOwnerStmt new objectType';
4352+
output = array_append(output, deparser.expression(node->'object'));
43604353
END IF;
4354+
output = array_append(output, 'OWNER');
4355+
output = array_append(output, 'TO');
4356+
output = array_append(output, deparser.role_spec(node->'newowner'));
43614357

43624358
RETURN array_to_string(output, ' ');
43634359
END;
@@ -4393,7 +4389,13 @@ BEGIN
43934389
IF ( (node->'missing_ok')::bool IS TRUE ) THEN
43944390
output = array_append(output, 'IF EXISTS');
43954391
END IF;
4396-
output = array_append(output, deparser.expression(node->'object'));
4392+
4393+
IF (jsonb_typeof(node->'object') = 'array') THEN
4394+
output = array_append(output, deparser.list_quotes(node->'object', '.'));
4395+
ELSE
4396+
output = array_append(output, deparser.expression(node->'object'));
4397+
END IF;
4398+
43974399
output = array_append(output, 'SET SCHEMA');
43984400
output = array_append(output, quote_ident(node->>'newschema'));
43994401
END IF;

packages/ast/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
"graphql": "^14.0.2",
3030
"graphql-tag": "2.10.3",
3131
"jest": "26.1.0",
32-
"pgsql-parser": "13.1.1",
33-
"pgsql-deparser": "13.1.1",
32+
"pgsql-parser": "13.1.2",
33+
"pgsql-deparser": "13.1.2",
3434
"prettier": "2.0.5",
3535
"regenerator-runtime": "^0.13.2"
3636
},
@@ -42,4 +42,4 @@
4242
"__fixtures__"
4343
]
4444
}
45-
}
45+
}

packages/ast/sql/ast--0.0.1.sql

+21-15
Original file line numberDiff line numberDiff line change
@@ -4976,9 +4976,13 @@ BEGIN
49764976
ELSIF (targtype = 'ACL_TARGET_DEFAULTS') THEN
49774977
RETURN 'TABLES';
49784978
END IF;
4979-
-- TODO could be a view
49804979
RETURN 'TABLE';
49814980
ELSIF (objtype = 'OBJECT_SEQUENCE') THEN
4981+
IF (targtype = 'ACL_TARGET_ALL_IN_SCHEMA') THEN
4982+
RETURN 'ALL SEQUENCES IN SCHEMA';
4983+
ELSIF (targtype = 'ACL_TARGET_DEFAULTS') THEN
4984+
RETURN 'SEQUENCES';
4985+
END IF;
49824986
RETURN 'SEQUENCE';
49834987
ELSIF (objtype = 'OBJECT_DATABASE') THEN
49844988
RETURN 'DATABASE';
@@ -8916,21 +8920,17 @@ BEGIN
89168920

89178921
node = node->'AlterOwnerStmt';
89188922
objectType = node->>'objectType';
8919-
IF (
8920-
objectType = 'OBJECT_FUNCTION' OR
8921-
objectType = 'OBJECT_FOREIGN_TABLE' OR
8922-
objectType = 'OBJECT_FDW' OR
8923-
objectType = 'OBJECT_FOREIGN_SERVER'
8924-
) THEN
8925-
output = array_append(output, 'ALTER');
8926-
output = array_append(output, ast_utils.objtype_name(objectType) );
8927-
output = array_append(output, deparser.expression(node->'object'));
8928-
output = array_append(output, 'OWNER');
8929-
output = array_append(output, 'TO');
8930-
output = array_append(output, deparser.role_spec(node->'newowner'));
8923+
8924+
output = array_append(output, 'ALTER');
8925+
output = array_append(output, ast_utils.objtype_name(objectType) );
8926+
IF (jsonb_typeof(node->'object') = 'array') THEN
8927+
output = array_append(output, deparser.list_quotes(node->'object', '.'));
89318928
ELSE
8932-
RAISE EXCEPTION 'BAD_EXPRESSION %', 'AlterOwnerStmt new objectType';
8929+
output = array_append(output, deparser.expression(node->'object'));
89338930
END IF;
8931+
output = array_append(output, 'OWNER');
8932+
output = array_append(output, 'TO');
8933+
output = array_append(output, deparser.role_spec(node->'newowner'));
89348934

89358935
RETURN array_to_string(output, ' ');
89368936
END;
@@ -8962,7 +8962,13 @@ BEGIN
89628962
IF ( (node->'missing_ok')::bool IS TRUE ) THEN
89638963
output = array_append(output, 'IF EXISTS');
89648964
END IF;
8965-
output = array_append(output, deparser.expression(node->'object'));
8965+
8966+
IF (jsonb_typeof(node->'object') = 'array') THEN
8967+
output = array_append(output, deparser.list_quotes(node->'object', '.'));
8968+
ELSE
8969+
output = array_append(output, deparser.expression(node->'object'));
8970+
END IF;
8971+
89668972
output = array_append(output, 'SET SCHEMA');
89678973
output = array_append(output, quote_ident(node->>'newschema'));
89688974
END IF;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
ALTER TYPE electronic_mail RENAME TO email;
2+
3+
ALTER TYPE email OWNER TO joe;
4+
ALTER TYPE email SET SCHEMA customers;
5+
ALTER TYPE compfoo ADD ATTRIBUTE f3 int;
6+
ALTER TYPE colors ADD VALUE 'orange' AFTER 'red';
7+
ALTER TYPE colors ADD VALUE 'orange' BEFORE 'red';
8+
9+
ALTER TYPE enum_type ADD VALUE 'new_value';
10+

packages/ast/test/__tests__/__snapshots__/kitchen-sink.test.js.snap

+20
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,26 @@ DROP DOMAIN testdomain1;
572572
ALTER TABLE mytable ADD COLUMN height_in numeric GENERATED ALWAYS AS ( height_cm / 2.54 ) STORED;"
573573
`;
574574

575+
exports[`kitchen sink alter enums 1`] = `
576+
"ALTER TYPE electronic_mail RENAME TO email;
577+
ALTER TYPE email OWNER TO joe;
578+
ALTER TYPE email SET SCHEMA customers;
579+
ALTER TYPE compfoo ADD ATTRIBUTE f3 int;
580+
ALTER TYPE colors ADD VALUE 'orange' AFTER 'red';
581+
ALTER TYPE colors ADD VALUE 'orange' BEFORE 'red';
582+
ALTER TYPE enum_type ADD VALUE 'new_value';"
583+
`;
584+
585+
exports[`kitchen sink alter enums 2`] = `
586+
"ALTER TYPE electronic_mail RENAME TO email;
587+
ALTER TYPE email OWNER TO joe;
588+
ALTER TYPE email SET SCHEMA customers;
589+
ALTER TYPE compfoo ADD ATTRIBUTE f3 int;
590+
ALTER TYPE colors ADD VALUE 'orange' AFTER 'red';
591+
ALTER TYPE colors ADD VALUE 'orange' BEFORE 'red';
592+
ALTER TYPE enum_type ADD VALUE 'new_value';"
593+
`;
594+
575595
exports[`kitchen sink conflicts 1`] = `
576596
"INSERT INTO yo.\\"table\\" (project_id, name, field_name) VALUES (v_obj_key_id, v_secret_name::bytea, v_secret_value) ON CONFLICT (project_id, name) DO UPDATE SET field_name = EXCLUDED.field_name;
577597
INSERT INTO yo.\\"table\\" (project_id, name, field_name) VALUES (v_obj_key_id, v_secret_name::bytea, v_secret_value) ON CONFLICT (project_id, name) DO UPDATE SET field_name = EXCLUDED.field_name WHERE prop = 1;

packages/ast/test/__tests__/kitchen-sink.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ describe('kitchen sink', () => {
110110
it('enums', async () => {
111111
await check('enums/create.sql');
112112
});
113+
it('alter enums', async () => {
114+
await check('enums/alter.sql');
115+
});
113116
it('do stmt', async () => {
114117
await check('do/custom.sql');
115118
});

yarn.lock

+15-15
Original file line numberDiff line numberDiff line change
@@ -8434,15 +8434,15 @@ [email protected]:
84348434
dependencies:
84358435
split "^1.0.0"
84368436

8437-
[email protected].1, pgsql-deparser@^13.1.1:
8438-
version "13.1.1"
8439-
resolved "https://registry.npmjs.org/pgsql-deparser/-/pgsql-deparser-13.1.1.tgz#95a3a9c1b73ffca6095fa2e7839447d23d67f972"
8440-
integrity sha512-V8fKumXROotviMqQ1HVRJUqjVd6P5lKaigZJYnK3bo6cjCbJMpmiHNaIFIlordXLbS/3VE5MAQKMFfPVLuhH+w==
8437+
[email protected].2, pgsql-deparser@^13.1.2:
8438+
version "13.1.2"
8439+
resolved "https://registry.npmjs.org/pgsql-deparser/-/pgsql-deparser-13.1.2.tgz#b98c5035e996237a7c04da236606b4bbf1e407e5"
8440+
integrity sha512-87X5ME3hAcdiuOuEZUyMqEGH+2topbdx5OuUecwdmg2tPJJcfsbJt69LTS3z5SiypkZt5caHfvZTfWbDDZNC9Q==
84418441
dependencies:
84428442
"@babel/runtime" "^7.11.2"
84438443
dotty "0.1.0"
84448444
lodash "4.17.20"
8445-
pgsql-enums "^13.1.0"
8445+
pgsql-enums "^13.1.1"
84468446

84478447
pgsql-deparser@^1.2.2:
84488448
version "1.2.3"
@@ -8461,10 +8461,10 @@ pgsql-enums@^1.0.2:
84618461
dependencies:
84628462
"@babel/runtime" "^7.4.2"
84638463

8464-
pgsql-enums@^13.1.0:
8465-
version "13.1.0"
8466-
resolved "https://registry.npmjs.org/pgsql-enums/-/pgsql-enums-13.1.0.tgz#b771bf08295ffdd4e268b4a6ae04659631d53e35"
8467-
integrity sha512-OZsx3abb6KIc34Tg5fL7eFgHcqaFERA73nw/bYx/52xMircArfYITbMhwhvaf0oxUV24o8eFciMkCL0QJ584hw==
8464+
pgsql-enums@^13.1.1:
8465+
version "13.1.1"
8466+
resolved "https://registry.npmjs.org/pgsql-enums/-/pgsql-enums-13.1.1.tgz#f995bb33ca11ed30b18a0b346904f9d1493c5b44"
8467+
integrity sha512-Z2DKIPFj2FkLwYOiizjEFChSesoUBUxLvADVF+m0D9DruJtgW340+n146yDNnz7nM/m02TvTD/RP9Z26VY4h8w==
84688468
dependencies:
84698469
"@babel/runtime" "^7.4.2"
84708470

@@ -8490,16 +8490,16 @@ [email protected]:
84908490
pgsql-deparser "^1.2.2"
84918491
pgsql-enums "^1.0.2"
84928492

8493-
8494-
version "13.1.1"
8495-
resolved "https://registry.npmjs.org/pgsql-parser/-/pgsql-parser-13.1.1.tgz#7153d9fc114d7d66ae75258c464be18dc2bf24f0"
8496-
integrity sha512-Fh7Svor+9lAue9/vICvx+PlJB9W7SKfVuhfY1S72NX2gk5onrbMZXeY2vhd4AYzTzk9JumA+alNmb5/htCX9gg==
8493+
8494+
version "13.1.2"
8495+
resolved "https://registry.npmjs.org/pgsql-parser/-/pgsql-parser-13.1.2.tgz#0987a288679ad349a6cb645e3f41f9940a0e0352"
8496+
integrity sha512-2HXra7/ho3pHTD5HSRQKQ67MEq4UEOCLFqKA/rnhcAeV5nYQzVWWajw39oELT4RDbxR9OWR6eXh0SEFHj8vfDA==
84978497
dependencies:
84988498
"@babel/runtime" "^7.11.2"
84998499
libpg-query "13.0.1"
85008500
minimist "^1.2.5"
8501-
pgsql-deparser "^13.1.1"
8502-
pgsql-enums "^13.1.0"
8501+
pgsql-deparser "^13.1.2"
8502+
pgsql-enums "^13.1.1"
85038503

85048504
picomatch@^2.0.4, picomatch@^2.0.5:
85058505
version "2.2.2"

0 commit comments

Comments
 (0)