Skip to content

Commit 60f309f

Browse files
committed
fix: support create index if not exists
The flag `if_not_exists` was ignored when deparsing a create index statement.
1 parent f1df82e commit 60f309f

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

__fixtures__/misc.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ create table if not exists users (
1919
handle text not null,
2020
created_at timestamp not null default now(),
2121
updated_at timestamp not null default now()
22-
)
22+
);
23+
24+
CREATE INDEX CONCURRENTLY IF NOT EXISTS index_email_logs_on_created_at ON public.email_logs USING btree (created_at DESC);

packages/deparser/__tests__/__snapshots__/kitchen-sink.test.ts.snap

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246734,7 +246734,7 @@ exports[`misc 15`] = `
246734246734
],
246735246735
},
246736246736
},
246737-
"stmt_len": undefined,
246737+
"stmt_len": 238,
246738246738
"stmt_location": 629,
246739246739
},
246740246740
}
@@ -246747,9 +246747,44 @@ name text NOT NULL,
246747246747
handle text NOT NULL,
246748246748
created_at timestamp NOT NULL DEFAULT ( now() ),
246749246749
updated_at timestamp NOT NULL DEFAULT ( now() )
246750-
)"
246750+
);"
246751246751
`;
246752246752

246753+
exports[`misc 17`] = `
246754+
{
246755+
"RawStmt": {
246756+
"stmt": {
246757+
"IndexStmt": {
246758+
"accessMethod": "btree",
246759+
"concurrent": true,
246760+
"idxname": "index_email_logs_on_created_at",
246761+
"if_not_exists": true,
246762+
"indexParams": [
246763+
{
246764+
"IndexElem": {
246765+
"name": "created_at",
246766+
"nulls_ordering": "SORTBY_NULLS_DEFAULT",
246767+
"ordering": "SORTBY_DESC",
246768+
},
246769+
},
246770+
],
246771+
"relation": {
246772+
"inh": true,
246773+
"location": 944,
246774+
"relname": "email_logs",
246775+
"relpersistence": "p",
246776+
"schemaname": "public",
246777+
},
246778+
},
246779+
},
246780+
"stmt_len": 123,
246781+
"stmt_location": 868,
246782+
},
246783+
}
246784+
`;
246785+
246786+
exports[`misc 18`] = `"CREATE INDEX CONCURRENTLY IF NOT EXISTS index_email_logs_on_created_at ON public.email_logs ( created_at DESC );"`;
246787+
246753246788
exports[`parens 1`] = `
246754246789
{
246755246790
"RawStmt": {

packages/deparser/src/deparser.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,6 +1510,9 @@ export default class Deparser {
15101510
if (node.concurrent) {
15111511
output.push('CONCURRENTLY');
15121512
}
1513+
if (node.if_not_exists) {
1514+
output.push('IF NOT EXISTS');
1515+
}
15131516

15141517
if (node.idxname) {
15151518
output.push(node.idxname);

0 commit comments

Comments
 (0)