Skip to content

Commit 662214a

Browse files
authored
Merge pull request #14 from fljdin/quoted-objects-in-IndexStmt
Fix quotes for index names (reviewed)
2 parents 45eccb2 + 7fdb0ab commit 662214a

File tree

5 files changed

+228
-8
lines changed

5 files changed

+228
-8
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2780,7 +2780,7 @@ BEGIN
27802780
node = node->'IndexElem';
27812781

27822782
IF (node->'name' IS NOT NULL) THEN
2783-
RETURN node->>'name';
2783+
RETURN quote_ident(node->>'name');
27842784
END IF;
27852785

27862786
IF (node->'expr' IS NOT NULL) THEN
@@ -3120,9 +3120,8 @@ BEGIN
31203120
output = array_append(output, 'CONCURRENTLY');
31213121
END IF;
31223122

3123-
IF (node->'idxname' IS NOT NULL) THEN
3124-
-- TODO needs quote?
3125-
output = array_append(output, node->>'idxname');
3123+
IF (node->'idxname' IS NOT NULL) THEN
3124+
output = array_append(output, quote_ident(node->>'idxname'));
31263125
END IF;
31273126

31283127
output = array_append(output, 'ON');

packages/ast/sql/ast--13.0.4.sql

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7909,7 +7909,7 @@ BEGIN
79097909
node = node->'IndexElem';
79107910

79117911
IF (node->'name' IS NOT NULL) THEN
7912-
RETURN node->>'name';
7912+
RETURN quote_ident(node->>'name');
79137913
END IF;
79147914

79157915
IF (node->'expr' IS NOT NULL) THEN
@@ -8229,9 +8229,8 @@ BEGIN
82298229
output = array_append(output, 'CONCURRENTLY');
82308230
END IF;
82318231

8232-
IF (node->'idxname' IS NOT NULL) THEN
8233-
-- TODO needs quote?
8234-
output = array_append(output, node->>'idxname');
8232+
IF (node->'idxname' IS NOT NULL) THEN
8233+
output = array_append(output, quote_ident(node->>'idxname'));
82358234
END IF;
82368235

82378236
output = array_append(output, 'ON');
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
export const indexes = [
2+
{
3+
"RawStmt": {
4+
"stmt": {
5+
"IndexStmt": {
6+
"idxname": "idx_CaseSimple",
7+
"relation": {
8+
"relname": "Table_CaseSimple",
9+
"inh": true,
10+
"relpersistence": "p",
11+
"location": 33
12+
},
13+
"accessMethod": "btree",
14+
"indexParams": [
15+
{
16+
"IndexElem": {
17+
"name": "CaseSimple",
18+
"ordering": "SORTBY_DEFAULT",
19+
"nulls_ordering": "SORTBY_NULLS_DEFAULT"
20+
}
21+
}
22+
]
23+
}
24+
},
25+
"stmt_len": 66,
26+
"stmt_location": 0
27+
}
28+
},
29+
{
30+
"RawStmt": {
31+
"stmt": {
32+
"IndexStmt": {
33+
"idxname": "idx_CaseExpression",
34+
"relation": {
35+
"relname": "Table_CaseExperssion",
36+
"inh": true,
37+
"relpersistence": "p",
38+
"location": 106
39+
},
40+
"accessMethod": "btree",
41+
"indexParams": [
42+
{
43+
"IndexElem": {
44+
"expr": {
45+
"FuncCall": {
46+
"funcname": [
47+
{
48+
"String": {
49+
"str": "lower"
50+
}
51+
}
52+
],
53+
"args": [
54+
{
55+
"ColumnRef": {
56+
"fields": [
57+
{
58+
"String": {
59+
"str": "CaseSimple"
60+
}
61+
}
62+
],
63+
"location": 136
64+
}
65+
}
66+
],
67+
"location": 130
68+
}
69+
},
70+
"ordering": "SORTBY_DEFAULT",
71+
"nulls_ordering": "SORTBY_NULLS_DEFAULT"
72+
}
73+
}
74+
]
75+
}
76+
},
77+
"stmt_len": 83,
78+
"stmt_location": 67
79+
}
80+
},
81+
{
82+
"RawStmt": {
83+
"stmt": {
84+
"IndexStmt": {
85+
"idxname": "idx_CaseInclude",
86+
"relation": {
87+
"relname": "table_caseinclude",
88+
"inh": true,
89+
"relpersistence": "p",
90+
"location": 187
91+
},
92+
"accessMethod": "btree",
93+
"indexParams": [
94+
{
95+
"IndexElem": {
96+
"name": "simple",
97+
"ordering": "SORTBY_DEFAULT",
98+
"nulls_ordering": "SORTBY_NULLS_DEFAULT"
99+
}
100+
}
101+
],
102+
"indexIncludingParams": [
103+
{
104+
"IndexElem": {
105+
"name": "CaseInclude",
106+
"ordering": "SORTBY_DEFAULT",
107+
"nulls_ordering": "SORTBY_NULLS_DEFAULT"
108+
}
109+
}
110+
]
111+
}
112+
},
113+
"stmt_len": 86,
114+
"stmt_location": 151
115+
}
116+
},
117+
{
118+
"RawStmt": {
119+
"stmt": {
120+
"IndexStmt": {
121+
"idxname": "idx_CaseIncludeExpr",
122+
"relation": {
123+
"relname": "table_caseincludeexpr",
124+
"inh": true,
125+
"relpersistence": "p",
126+
"location": 277
127+
},
128+
"accessMethod": "btree",
129+
"indexParams": [
130+
{
131+
"IndexElem": {
132+
"name": "simple",
133+
"ordering": "SORTBY_DEFAULT",
134+
"nulls_ordering": "SORTBY_NULLS_DEFAULT"
135+
}
136+
}
137+
],
138+
"indexIncludingParams": [
139+
{
140+
"IndexElem": {
141+
"expr": {
142+
"FuncCall": {
143+
"funcname": [
144+
{
145+
"String": {
146+
"str": "lower"
147+
}
148+
}
149+
],
150+
"args": [
151+
{
152+
"ColumnRef": {
153+
"fields": [
154+
{
155+
"String": {
156+
"str": "CaseInclude"
157+
}
158+
}
159+
],
160+
"location": 323
161+
}
162+
}
163+
],
164+
"location": 317
165+
}
166+
},
167+
"ordering": "SORTBY_DEFAULT",
168+
"nulls_ordering": "SORTBY_NULLS_DEFAULT"
169+
}
170+
}
171+
]
172+
}
173+
},
174+
"stmt_len": 100,
175+
"stmt_location": 238
176+
}
177+
}
178+
];
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`index_stmt 1`] = `"CREATE INDEX \\"idx_CaseSimple\\" ON \\"Table_CaseSimple\\" (\\"CaseSimple\\");"`;
4+
5+
exports[`index_stmt 2`] = `"CREATE INDEX \\"idx_CaseExpression\\" ON \\"Table_CaseExperssion\\" (lower(\\"CaseSimple\\"));"`;
6+
7+
exports[`index_stmt 3`] = `"CREATE INDEX \\"idx_CaseInclude\\" ON table_caseinclude (simple) INCLUDE (\\"CaseInclude\\");"`;
8+
9+
exports[`index_stmt 4`] = `"CREATE INDEX \\"idx_CaseIncludeExpr\\" ON table_caseincludeexpr (simple) INCLUDE (lower(\\"CaseInclude\\"));"`;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { getConnections } from '../../utils';
2+
import { indexes } from './__fixtures__/indexes';
3+
4+
let db, teardown;
5+
const objs = {
6+
tables: {}
7+
};
8+
9+
beforeAll(async () => {
10+
({ db, teardown } = await getConnections());
11+
await db.begin();
12+
await db.savepoint();
13+
});
14+
afterAll(async () => {
15+
try {
16+
//try catch here allows us to see the sql parsing issues!
17+
await db.rollback();
18+
await db.commit();
19+
await teardown();
20+
} catch (e) {}
21+
});
22+
23+
it('index_stmt', async () => {
24+
for (const index of indexes) {
25+
const [{ deparse: result }] = await db.any(
26+
`
27+
select deparser.deparse(
28+
$1::jsonb
29+
);
30+
`,
31+
[index]
32+
);
33+
expect(result).toMatchSnapshot();
34+
}
35+
});

0 commit comments

Comments
 (0)