-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindexHelper.js
More file actions
174 lines (153 loc) · 4.82 KB
/
indexHelper.js
File metadata and controls
174 lines (153 loc) · 4.82 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
164
165
166
167
168
169
170
171
172
173
174
const _ = require('lodash');
const {
wrapInQuotes,
checkAllKeysDeactivated,
getColumnsList,
getNamePrefixedWithSchemaName,
getDbVersion,
commentIfDeactivated,
} = require('../../utils/general');
const assignTemplates = require('../../utils/assignTemplates');
const templates = require('../templates');
/**
* @param {object} params
* @param {Record<string, unknown>} params.index
* @returns {string[]}
*/
const getIndexExpressions = ({ index }) => {
return _.map(index.indxExpression, ({ value = '' }) => _.trim(value)).filter(Boolean);
};
const mapIndexKey = ({ name, sortOrder, nullsOrder, collation, opclass }) => {
const sortOrderStr = sortOrder ? ` ${sortOrder}` : '';
const nullsOrderStr = nullsOrder ? ` ${nullsOrder}` : '';
const collate = _.includes(collation, '"') ? collation : `"${collation}"`;
const collationStr = collation ? ` COLLATE ${collate}` : '';
const opclassStr = opclass ? ` ${opclass}` : '';
return `${wrapInQuotes(name)}${collationStr}${opclassStr}${sortOrderStr}${nullsOrderStr}`;
};
const getIndexKeys = ({ columns = [], indexExpressions, isParentActivated, isAllColumnsDeactivated }) => {
return indexExpressions.length
? ' (' + indexExpressions.join(', ') + ')'
: getColumnsList(columns, isAllColumnsDeactivated, isParentActivated, mapIndexKey);
};
const getIndexOptions = (index, isParentActivated) => {
const includeKeys = getColumnsList(
index.include || [],
checkAllKeysDeactivated(index.include || []),
isParentActivated,
);
const include = index.include?.length ? ` INCLUDE ${_.trim(includeKeys)}` : '';
const withOptionsString = getWithOptions(index);
const withOptions = withOptionsString ? ` WITH (\n\t${withOptionsString})` : '';
const tableSpace = index.index_tablespace_name ? ` TABLESPACE ${index.index_tablespace_name}` : '';
const whereExpression = index.where ? ` WHERE ${index.where}` : '';
return _.compact([' ', include, withOptions, tableSpace, whereExpression]).join('\n');
};
const INDEX_STORAGE_OPTIONS_BY_METHOD = {
btree: {
index_fillfactor: 'fillfactor',
deduplicate_items: 'deduplicate_items',
},
hash: {
index_fillfactor: 'fillfactor',
},
spgist: {
index_fillfactor: 'fillfactor',
},
gist: {
index_fillfactor: 'fillfactor',
index_buffering: 'buffering',
},
gin: {
fastupdate: 'fastupdate',
gin_pending_list_limit: 'gin_pending_list_limit',
},
brin: {
pages_per_range: 'pages_per_range',
autosummarize: 'autosummarize',
},
};
const getWithOptions = index => {
const config = INDEX_STORAGE_OPTIONS_BY_METHOD[index.index_method];
return _.chain(config)
.toPairs()
.map(([keyInModel, postgresKey]) => {
const value = index.index_storage_parameter?.[keyInModel];
if (_.isNil(value) || value === '') {
return;
}
return `${postgresKey}=${getValue(value)}`;
})
.compact()
.join(',\n\t')
.value();
};
const getValue = value => {
if (_.isBoolean(value)) {
return value ? 'ON' : 'OFF';
}
return value;
};
const createIndex = (tableName, index, dbData, isParentActivated = true) => {
const isNameEmpty = !index.indxName && index.ifNotExist;
const indexExpressions = getIndexExpressions({ index });
const hasKeys = index.columns.length || indexExpressions.length;
if (!hasKeys || isNameEmpty) {
return '';
}
const isUnique = index.unique && index.index_method === 'btree';
const name = wrapInQuotes(index.indxName);
const unique = isUnique ? ' UNIQUE' : '';
const concurrently = index.concurrently ? ' CONCURRENTLY' : '';
const ifNotExist = index.ifNotExist ? ' IF NOT EXISTS' : '';
const only = index.only ? ' ONLY' : '';
const using = index.index_method ? ` USING ${_.toUpper(index.index_method)}` : '';
const dbVersion = getDbVersion(_.get(dbData, 'dbVersion', ''));
const nullsDistinct = isUnique && index.nullsDistinct && dbVersion >= 15 ? `\n ${index.nullsDistinct}` : '';
const indexColumns =
index.index_method === 'btree'
? index.columns
: _.map(index.columns, column => _.omit(column, 'sortOrder', 'nullsOrder'));
const isAllColumnsDeactivated = checkAllKeysDeactivated(indexColumns);
const keys = getIndexKeys({
columns: indexColumns,
indexExpressions,
isParentActivated,
isAllColumnsDeactivated,
});
const options = getIndexOptions(index, isParentActivated);
return commentIfDeactivated(
assignTemplates(templates.index, {
unique,
concurrently,
ifNotExist,
name,
only,
using,
keys,
options,
nullsDistinct,
tableName: getNamePrefixedWithSchemaName(tableName, index.schemaName),
}),
{
isActivated: index.isActivated && isParentActivated && !isAllColumnsDeactivated,
},
);
};
/**
* @param indexName {string}
* @return {string}
* */
const dropIndex = ({ indexName }) => {
const templatesConfig = {
indexName,
};
return assignTemplates(templates.dropIndex, templatesConfig);
};
module.exports = {
createIndex,
dropIndex,
getIndexKeys,
getIndexOptions,
getWithOptions,
};