Skip to content

Commit 931fd3d

Browse files
RE: fix reversing views with prohibited symbols in name
1 parent e1c2822 commit 931fd3d

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed

reverse_engineering/helpers/postgresHelpers/common.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,101 @@
1+
const POSTGRES_RESERVED_WORDS = [
2+
'ALL',
3+
'ANALYSE',
4+
'ANALYZE',
5+
'AND',
6+
'ANY',
7+
'ARRAY',
8+
'ASC',
9+
'ASYMMETRIC',
10+
'AUTHORIZATION',
11+
'BINARY',
12+
'BOTH',
13+
'CASE',
14+
'CAST',
15+
'CHECK',
16+
'COLLATE',
17+
'COLUMN',
18+
'CONCURRENTLY',
19+
'CONSTRAINT',
20+
'CREATE',
21+
'CROSS',
22+
'CURRENT_CATALOG',
23+
'CURRENT_DATE',
24+
'CURRENT_ROLE',
25+
'CURRENT_SCHEMA',
26+
'CURRENT_TIME',
27+
'CURRENT_TIMESTAMP',
28+
'CURRENT_USER',
29+
'DEFAULT',
30+
'DEFERRABLE',
31+
'DESC',
32+
'DISTINCT',
33+
'DO',
34+
'ELSE',
35+
'END',
36+
'EXCEPT',
37+
'FALSE',
38+
'FOR',
39+
'FOREIGN',
40+
'FREEZE',
41+
'FROM',
42+
'FULL',
43+
'GRANT',
44+
'GROUP',
45+
'HAVING',
46+
'ILIKE',
47+
'IN',
48+
'INITIALLY',
49+
'INTERSECT',
50+
'INTO',
51+
'IS',
52+
'ISNULL',
53+
'JOIN',
54+
'LATERAL',
55+
'LEADING',
56+
'LEFT',
57+
'LIKE',
58+
'LIMIT',
59+
'LOCALTIME',
60+
'LOCALTIMESTAMP',
61+
'NATURAL',
62+
'NOT',
63+
'NULL',
64+
'OFFSET',
65+
'ON',
66+
'ONLY',
67+
'OR',
68+
'ORDER',
69+
'OUTER',
70+
'OVERLAPS',
71+
'PLACING',
72+
'PRIMARY',
73+
'REFERENCES',
74+
'RETURNING',
75+
'RIGHT',
76+
'SELECT',
77+
'SESSION_USER',
78+
'SIMILAR',
79+
'SOME',
80+
'SYMMETRIC',
81+
'TABLE',
82+
'TABLESAMPLE',
83+
'THEN',
84+
'TO',
85+
'TRAILING',
86+
'TRUE',
87+
'UNION',
88+
'UNIQUE',
89+
'USER',
90+
'USING',
91+
'VARIADIC',
92+
'VERBOSE',
93+
'WHEN',
94+
'WHERE',
95+
'WINDOW',
96+
'WITH',
97+
];
98+
199
let _ = null;
2100

3101
const setDependencies = app => {
@@ -12,9 +110,12 @@ const clearEmptyPropertiesInObject = obj =>
12110
.value();
13111

14112
const getColumnNameByPosition = columns => position => _.find(columns, { ordinal_position: position })?.column_name;
113+
const wrapInQuotes = name =>
114+
/\s|\W/.test(name) || _.includes(POSTGRES_RESERVED_WORDS, _.toUpper(name)) ? `"${name}"` : name;
15115

16116
module.exports = {
17117
clearEmptyPropertiesInObject,
18118
setDependencies,
19119
getColumnNameByPosition,
120+
wrapInQuotes,
20121
};

reverse_engineering/helpers/postgresHelpers/viewHelper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { clearEmptyPropertiesInObject } = require('./common');
1+
const { clearEmptyPropertiesInObject, wrapInQuotes } = require('./common');
22

33
let _ = null;
44

@@ -20,7 +20,7 @@ const generateCreateViewScript = (viewName, viewData, viewDefinitionFallback = {
2020
return '';
2121
}
2222

23-
return `CREATE VIEW ${viewName} AS ${selectStatement}`;
23+
return `CREATE VIEW ${wrapInQuotes(viewName)} AS ${selectStatement}`;
2424
};
2525

2626
const prepareViewData = (viewData, viewOptions, triggers) => {

0 commit comments

Comments
 (0)