Skip to content

Commit c1adefb

Browse files
committed
AlterSeqStmt
1 parent fae1c76 commit c1adefb

File tree

6 files changed

+100
-19
lines changed

6 files changed

+100
-19
lines changed

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,6 +1873,35 @@ END;
18731873
$$
18741874
LANGUAGE 'plpgsql' IMMUTABLE;
18751875

1876+
CREATE FUNCTION deparser.alter_seq_stmt(
1877+
node jsonb,
1878+
context jsonb default '{}'::jsonb
1879+
) returns text as $$
1880+
DECLARE
1881+
output text[];
1882+
BEGIN
1883+
IF (node->'AlterSeqStmt') IS NULL THEN
1884+
RAISE EXCEPTION 'BAD_EXPRESSION %', 'AlterSeqStmt';
1885+
END IF;
1886+
1887+
IF (node->'AlterSeqStmt'->'sequence') IS NULL THEN
1888+
RAISE EXCEPTION 'BAD_EXPRESSION %', 'AlterSeqStmt';
1889+
END IF;
1890+
1891+
node = node->'AlterSeqStmt';
1892+
1893+
output = array_append(output, 'ALTER SEQUENCE');
1894+
output = array_append(output, deparser.range_var(node->'sequence'));
1895+
1896+
IF (node->'options' IS NOT NULL AND jsonb_array_length(node->'options') > 0) THEN
1897+
output = array_append(output, deparser.list(node->'options', ' ', jsonb_set(context, '{sequence}', to_jsonb(TRUE))));
1898+
END IF;
1899+
1900+
RETURN array_to_string(output, ' ');
1901+
END;
1902+
$$
1903+
LANGUAGE 'plpgsql' IMMUTABLE;
1904+
18761905
CREATE FUNCTION deparser.do_stmt(
18771906
node jsonb,
18781907
context jsonb default '{}'::jsonb
@@ -2026,6 +2055,7 @@ CREATE FUNCTION deparser.def_elem(
20262055
) returns text as $$
20272056
DECLARE
20282057
defname text;
2058+
output text[];
20292059
BEGIN
20302060
IF (node->'DefElem') IS NULL THEN
20312061
RAISE EXCEPTION 'BAD_EXPRESSION %', 'DefElem';
@@ -2093,6 +2123,33 @@ BEGIN
20932123
ELSE
20942124
RETURN defname || ' ' || deparser.expression(node->'arg', jsonb_set(context, '{simple}', to_jsonb(TRUE)));
20952125
END IF;
2126+
ELSIF (defname = 'owned_by') THEN
2127+
output = ARRAY['OWNED BY']::text;
2128+
2129+
IF (node->'arg' IS NOT NULL AND jsonb_array_length(node->'arg') > 0) THEN
2130+
output = array_append(output, deparser.list_quotes(node->'arg', '.', jsonb_set(context, '{sequence}', to_jsonb(TRUE))));
2131+
END IF;
2132+
2133+
RETURN array_to_string(output, ' ');
2134+
2135+
ELSIF (defname = 'start') THEN
2136+
2137+
output = ARRAY['START WITH']::text;
2138+
output = array_append(output, deparser.expression(node->'arg', jsonb_set(context, '{sequence}', to_jsonb(TRUE))));
2139+
2140+
RETURN array_to_string(output, ' ');
2141+
2142+
ELSIF (defname = 'restart') THEN
2143+
2144+
IF (node->'arg' IS NOT NULL) THEN
2145+
output = ARRAY['RESTART WITH']::text;
2146+
output = array_append(output, deparser.expression(node->'arg', jsonb_set(context, '{sequence}', to_jsonb(TRUE))));
2147+
ELSE
2148+
output = ARRAY['RESTART']::text;
2149+
END IF;
2150+
2151+
RETURN array_to_string(output, ' ');
2152+
20962153
ELSIF (node->'arg' IS NOT NULL) THEN
20972154
RETURN defname || ' ' || deparser.expression(node->'arg', jsonb_set(context, '{simple}', to_jsonb(TRUE)));
20982155
END IF;
@@ -5184,6 +5241,8 @@ BEGIN
51845241
RETURN deparser.alter_enum_stmt(expr, context);
51855242
ELSEIF (expr->>'AlterPolicyStmt') IS NOT NULL THEN
51865243
RETURN deparser.alter_policy_stmt(expr, context);
5244+
ELSEIF (expr->>'AlterSeqStmt') IS NOT NULL THEN
5245+
RETURN deparser.alter_seq_stmt(expr, context);
51875246
ELSEIF (expr->>'AlterTableCmd') IS NOT NULL THEN
51885247
RETURN deparser.alter_table_cmd(expr, context);
51895248
ELSEIF (expr->>'AlterTableStmt') IS NOT NULL THEN

packages/ast/package.json

Lines changed: 2 additions & 2 deletions
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-deparser": "13.1.7",
33-
"pgsql-parser": "13.1.7",
32+
"pgsql-deparser": "13.1.9",
33+
"pgsql-parser": "13.1.9",
3434
"prettier": "2.0.5",
3535
"regenerator-runtime": "^0.13.2"
3636
},
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ALTER SEQUENCE public."User_id_seq" OWNED BY public."User".id;
2+
ALTER SEQUENCE serial RESTART WITH 105;
3+
ALTER SEQUENCE payments_id_seq RESTART WITH 22;
4+
ALTER SEQUENCE payments_id_seq START WITH 22;
5+
ALTER SEQUENCE payments_id_seq RESTART;

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,18 +1186,34 @@ SELECT character,
11861186
11871187
exports[`kitchen sink sequences 1`] = `
11881188
"CREATE SEQUENCE foo.bar;
1189-
CREATE SEQUENCE mysequence increment 5 start 100;
1190-
CREATE SEQUENCE three increment -1 minvalue 1 maxvalue 3 start 3 CYCLE;
1191-
CREATE SEQUENCE three3 increment -1 minvalue 1 maxvalue 3 start 3 NO CYCLE;
1192-
CREATE SEQUENCE app_jobs.jobs_id_seq start 1 increment 1 NO MINVALUE NO MAXVALUE cache 1;"
1189+
CREATE SEQUENCE mysequence increment 5 START WITH 100;
1190+
CREATE SEQUENCE three increment -1 minvalue 1 maxvalue 3 START WITH 3 CYCLE;
1191+
CREATE SEQUENCE three3 increment -1 minvalue 1 maxvalue 3 START WITH 3 NO CYCLE;
1192+
CREATE SEQUENCE app_jobs.jobs_id_seq START WITH 1 increment 1 NO MINVALUE NO MAXVALUE cache 1;"
11931193
`;
11941194
11951195
exports[`kitchen sink sequences 2`] = `
11961196
"CREATE SEQUENCE foo.bar;
1197-
CREATE SEQUENCE mysequence increment 5 start 100;
1198-
CREATE SEQUENCE three increment -1 minvalue 1 maxvalue 3 start 3 CYCLE;
1199-
CREATE SEQUENCE three3 increment -1 minvalue 1 maxvalue 3 start 3 NO CYCLE;
1200-
CREATE SEQUENCE app_jobs.jobs_id_seq start 1 increment 1 NO MINVALUE NO MAXVALUE cache 1;"
1197+
CREATE SEQUENCE mysequence increment 5 START WITH 100;
1198+
CREATE SEQUENCE three increment -1 minvalue 1 maxvalue 3 START WITH 3 CYCLE;
1199+
CREATE SEQUENCE three3 increment -1 minvalue 1 maxvalue 3 START WITH 3 NO CYCLE;
1200+
CREATE SEQUENCE app_jobs.jobs_id_seq START WITH 1 increment 1 NO MINVALUE NO MAXVALUE cache 1;"
1201+
`;
1202+
1203+
exports[`kitchen sink sequences 3`] = `
1204+
"ALTER SEQUENCE public.\\"User_id_seq\\" OWNED BY public.\\"User\\".id;
1205+
ALTER SEQUENCE serial RESTART WITH 105;
1206+
ALTER SEQUENCE payments_id_seq RESTART WITH 22;
1207+
ALTER SEQUENCE payments_id_seq START WITH 22;
1208+
ALTER SEQUENCE payments_id_seq RESTART;"
1209+
`;
1210+
1211+
exports[`kitchen sink sequences 4`] = `
1212+
"ALTER SEQUENCE public.\\"User_id_seq\\" OWNED BY public.\\"User\\".id;
1213+
ALTER SEQUENCE serial RESTART WITH 105;
1214+
ALTER SEQUENCE payments_id_seq RESTART WITH 22;
1215+
ALTER SEQUENCE payments_id_seq START WITH 22;
1216+
ALTER SEQUENCE payments_id_seq RESTART;"
12011217
`;
12021218
12031219
exports[`kitchen sink set 1`] = `

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ describe('kitchen sink', () => {
9191
});
9292
it('sequences', async () => {
9393
await check('sequences/sequences.sql');
94+
await check('sequences/alter.sql');
9495
});
9596
it('policies', async () => {
9697
await check('policies/custom.sql');

yarn.lock

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8752,10 +8752,10 @@ [email protected]:
87528752
dependencies:
87538753
split2 "^3.1.1"
87548754

8755-
[email protected].7, pgsql-deparser@^13.1.7:
8756-
version "13.1.7"
8757-
resolved "https://registry.npmjs.org/pgsql-deparser/-/pgsql-deparser-13.1.7.tgz#87d9ec43a40f5aefa343c56193ee41dd03fad225"
8758-
integrity sha512-VozcqTNfW8xSrV+Qn6PdGWJJ/9jtUJ9F7aDXDBNTWfroSOZkFETP3M71OKzwVBMoBFiifcU+/ryOVGv7XluGhw==
8755+
[email protected].9, pgsql-deparser@^13.1.9:
8756+
version "13.1.9"
8757+
resolved "https://registry.npmjs.org/pgsql-deparser/-/pgsql-deparser-13.1.9.tgz#3a1f4f70caee9b66100aca3708210d09a8556616"
8758+
integrity sha512-bhxKh9tTtVr45CkfJYCUdE3cyX0Ikcp3KRZLmYEUiFxwm3xP+xSS+gQocBUj1J7Ulwk/a2rBUzsZxyNU++l57g==
87598759
dependencies:
87608760
"@babel/runtime" "^7.11.2"
87618761
dotty "0.1.0"
@@ -8790,15 +8790,15 @@ [email protected]:
87908790
pgsql-deparser "^13.1.6"
87918791
pgsql-enums "^13.1.2"
87928792

8793-
8794-
version "13.1.7"
8795-
resolved "https://registry.npmjs.org/pgsql-parser/-/pgsql-parser-13.1.7.tgz#d8e81c62d625342245c4ae0a42f909eec138257f"
8796-
integrity sha512-iLxtU06NEg0RjEB/WMBaQz2PLhZLhwyoVswO5PLhn/iu53rLc6T0pjBcuugEiWJ6k92ivna1Aj9W/q8b2rBUIw==
8793+
8794+
version "13.1.9"
8795+
resolved "https://registry.npmjs.org/pgsql-parser/-/pgsql-parser-13.1.9.tgz#e70140fd9a8f54373e6e1393434520a3a88f6053"
8796+
integrity sha512-wI+04o8q8CfeEr+u1ax0xQOJ9RkRqfgSUJ7u7ewnangF/V1AdH5NNAsDpDoCGYzSAjy8z9zg4v4JdBYsYEfrSA==
87978797
dependencies:
87988798
"@babel/runtime" "^7.11.2"
87998799
libpg-query "13.1.1"
88008800
minimist "^1.2.5"
8801-
pgsql-deparser "^13.1.7"
8801+
pgsql-deparser "^13.1.9"
88028802
pgsql-enums "^13.1.2"
88038803

88048804
picomatch@^2.0.4, picomatch@^2.2.3:

0 commit comments

Comments
 (0)