Skip to content

Commit c2baad5

Browse files
committed
Improve Snowflake ALTER TABLE/ALTER COLUMN syntax and tests
1 parent 5f2b150 commit c2baad5

File tree

2 files changed

+98
-9
lines changed

2 files changed

+98
-9
lines changed

src/languages/snowflake/snowflake.formatter.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,23 @@ const reservedCommands = expandPhrases([
6161
'ALTER TABLE [IF EXISTS]',
6262
'RENAME TO',
6363
'SWAP WITH',
64-
'{SET | UNSET} [TAG | DATA_RETENTION_TIME_IN_DAYS | MAX_DATA_EXTENSION_TIME_IN_DAYS | CHANGE_TRACKING | DEFAULT_DDL_COLLATION | COMMENT]',
65-
'{ADD | DROP} ROW ACCESS POLICY',
66-
'DROP ALL ROW ACCESS POLICIES',
6764
'[SUSPEND | RESUME] RECLUSTER',
6865
'DROP CLUSTERING KEY',
6966
'ADD [COLUMN]',
7067
'RENAME COLUMN',
7168
'{ALTER | MODIFY} [COLUMN]',
7269
'DROP [COLUMN]',
70+
'{ADD | ALTER | MODIFY | DROP} [CONSTRAINT]',
71+
'RENAME CONSTRAINT',
72+
'{ADD | DROP} SEARCH OPTIMIZATION',
73+
'{SET | UNSET} [TAG]',
74+
'{ADD | DROP} ROW ACCESS POLICY',
75+
'DROP ALL ROW ACCESS POLICIES',
7376
'{SET | DROP} DEFAULT', // for alter column
74-
'SET NOT NULL', // for alter column
75-
'DROP NOT NULL', // for alter column
77+
'{SET | DROP} NOT NULL', // for alter column
7678
'[SET DATA] TYPE', // for alter column
7779
'[UNSET] COMMENT', // for alter column
78-
'{SET | UNSET} {MASKING POLICY | TAG}', // for alter column
79-
'[FOREIGN KEY] REFERENCES', // for alter column
80-
'{ADD | RENAME | ALTER | MODIFY | DROP} CONSTRAINT', // for alter column
81-
'{ADD | DROP} SEARCH OPTIMIZATION', // for alter column
80+
'{SET | UNSET} MASKING POLICY', // for alter column
8281

8382
// other
8483
// https://docs.snowflake.com/en/sql-reference/sql-all.html

test/snowflake.test.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,94 @@ describe('SnowflakeFormatter', () => {
8989
$$foo$$ $$bar$$
9090
`);
9191
});
92+
93+
it('formats ALTER TABLE ... ALTER COLUMN', () => {
94+
expect(
95+
format(
96+
`ALTER TABLE t ALTER COLUMN foo SET DATA TYPE VARCHAR;
97+
ALTER TABLE t ALTER COLUMN foo SET DEFAULT 5;
98+
ALTER TABLE t ALTER COLUMN foo DROP DEFAULT;
99+
ALTER TABLE t ALTER COLUMN foo SET NOT NULL;
100+
ALTER TABLE t ALTER COLUMN foo DROP NOT NULL;
101+
ALTER TABLE t ALTER COLUMN foo COMMENT 'blah';
102+
ALTER TABLE t ALTER COLUMN foo UNSET COMMENT;
103+
ALTER TABLE t ALTER COLUMN foo SET MASKING POLICY polis;
104+
ALTER TABLE t ALTER COLUMN foo UNSET MASKING POLICY;
105+
ALTER TABLE t ALTER COLUMN foo SET TAG tname = 10;
106+
ALTER TABLE t ALTER COLUMN foo UNSET TAG tname;`
107+
)
108+
).toBe(dedent`
109+
ALTER TABLE
110+
t
111+
ALTER COLUMN
112+
foo
113+
SET DATA TYPE
114+
VARCHAR;
115+
116+
ALTER TABLE
117+
t
118+
ALTER COLUMN
119+
foo
120+
SET DEFAULT
121+
5;
122+
123+
ALTER TABLE
124+
t
125+
ALTER COLUMN
126+
foo
127+
DROP DEFAULT;
128+
129+
ALTER TABLE
130+
t
131+
ALTER COLUMN
132+
foo
133+
SET NOT NULL;
134+
135+
ALTER TABLE
136+
t
137+
ALTER COLUMN
138+
foo
139+
DROP NOT NULL;
140+
141+
ALTER TABLE
142+
t
143+
ALTER COLUMN
144+
foo
145+
COMMENT
146+
'blah';
147+
148+
ALTER TABLE
149+
t
150+
ALTER COLUMN
151+
foo
152+
UNSET COMMENT;
153+
154+
ALTER TABLE
155+
t
156+
ALTER COLUMN
157+
foo
158+
SET MASKING POLICY
159+
polis;
160+
161+
ALTER TABLE
162+
t
163+
ALTER COLUMN
164+
foo
165+
UNSET MASKING POLICY;
166+
167+
ALTER TABLE
168+
t
169+
ALTER COLUMN
170+
foo
171+
SET TAG
172+
tname = 10;
173+
174+
ALTER TABLE
175+
t
176+
ALTER COLUMN
177+
foo
178+
UNSET TAG
179+
tname;
180+
`);
181+
});
92182
});

0 commit comments

Comments
 (0)