-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcolumnDefinitionHelper.js
More file actions
163 lines (139 loc) · 4.53 KB
/
Copy pathcolumnDefinitionHelper.js
File metadata and controls
163 lines (139 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const _ = require('lodash');
const { commentIfDeactivated, wrapInQuotes, wrapComment } = require('../../utils/general');
const assignTemplates = require('../../utils/assignTemplates');
const templates = require('../templates');
const { isVector, isString, isDateTime, isUUID, isInet } = require('./typeHelper');
const addLength = (type, length) => {
return `${type}(${length})`;
};
const addScalePrecision = (type, precision, scale) => {
if (_.isNumber(scale)) {
return `${type}(${precision},${scale})`;
} else {
return `${type}(${precision})`;
}
};
const addPrecision = (type, precision) => {
if (_.isNumber(precision)) {
return `${type}(${precision})`;
}
return type;
};
const addWithTimezone = (type, timezone) => {
if (timezone) {
return `${type} ${timezone}`;
}
return type;
};
const addTypeModifier = ({ type, typeModifier, srid }) => {
const typeSrid = srid ? `, ${srid}` : ``;
if (typeModifier && typeModifier !== '') {
return `${type}(${typeModifier}${typeSrid})`;
}
return type;
};
const addArrayDecorator = (type, array_type) => {
const arrayDecorator =
array_type
?.map(item => `[${item?.array_size_limit ?? ''}]`)
.join('')
.trim() || '';
return `${type}${arrayDecorator}`;
};
const decorateVector = (type, dimension) => {
return `${type}(${dimension})`;
};
const canHaveLength = type => ['char', 'varchar', 'bit', 'varbit'].includes(type);
const canHavePrecision = type => type === 'numeric';
const canHaveTimePrecision = type => ['time', 'timestamp'].includes(type);
const canHaveScale = type => type === 'numeric';
const canHaveTypeModifier = type => ['geography', 'geometry'].includes(type);
const decorateType = (type, columnDefinition) => {
const { length, precision, scale, typeModifier, srid, timezone, timePrecision, dimension, subtype, array_type } =
columnDefinition;
const safeType = wrapInQuotes(type);
if (canHaveLength(type) && _.isNumber(length)) {
type = addLength(safeType, length);
} else if (canHavePrecision(type) && canHaveScale(type) && _.isNumber(precision)) {
type = addScalePrecision(safeType, precision, scale);
} else if (canHavePrecision(type) && _.isNumber(precision)) {
type = addPrecision(safeType, precision);
} else if (canHaveTypeModifier(type)) {
type = addTypeModifier({ type: safeType, typeModifier, srid });
} else if (canHaveTimePrecision(type) && (_.isNumber(timePrecision) || timezone)) {
type = addWithTimezone(addPrecision(safeType, timePrecision), timezone);
} else if (isVector(type)) {
type = dimension ? decorateVector(subtype, dimension) : subtype;
} else {
type = safeType;
}
return addArrayDecorator(type, array_type);
};
const decorateDefault = (type, defaultValue, isArrayType) => {
const constantsValues = ['current_timestamp', 'current_user', 'null'];
const isConstantValue = constantsValues.includes(_.toLower(defaultValue));
const isQuotableType = isString(type) || isDateTime(type) || isUUID(type) || isInet(type);
if (!isQuotableType || isConstantValue || isArrayType) {
return defaultValue;
}
return wrapComment(defaultValue);
};
const getColumnComments = (tableName, columnDefinitions) => {
return _.chain(columnDefinitions)
.filter('comment')
.map(columnData => {
const comment = assignTemplates(templates.comment, {
object: 'COLUMN',
objectName: `${tableName}.${wrapInQuotes(columnData.name)}`,
comment: wrapComment(columnData.comment),
});
return commentIfDeactivated(comment, columnData);
})
.join('\n')
.value();
};
const TYPES_MAPPING_BY_VERSION = {
'v13.x': {
int4multirange: 'int4range',
int8multirange: 'int8range',
nummultirange: 'numrange',
datemultirange: 'daterange',
tsmultirange: 'tsrange',
tstzmultirange: 'tstzrange',
},
'v12.x': {
int4multirange: 'int4range',
int8multirange: 'int8range',
nummultirange: 'numrange',
datemultirange: 'daterange',
tsmultirange: 'tsrange',
tstzmultirange: 'tstzrange',
},
'v11.x': {
int4multirange: 'int4range',
int8multirange: 'int8range',
nummultirange: 'numrange',
datemultirange: 'daterange',
tsmultirange: 'tsrange',
tstzmultirange: 'tstzrange',
},
'v10.x': {
int4multirange: 'int4range',
int8multirange: 'int8range',
nummultirange: 'numrange',
datemultirange: 'daterange',
tsmultirange: 'tsrange',
tstzmultirange: 'tstzrange',
},
};
const replaceTypeByVersion = (type, dbVersion) => {
const dbVersionMap = TYPES_MAPPING_BY_VERSION[dbVersion];
const replacedType = _.get(dbVersionMap, type);
return replacedType || type;
};
module.exports = {
decorateType,
decorateDefault,
getColumnComments,
replaceTypeByVersion,
};