index.test.js
10.2 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
'use strict';
const Support = require('../support'),
expectsql = Support.expectsql,
current = Support.sequelize,
sql = current.dialect.queryGenerator,
Op = Support.Sequelize.Op;
// Notice: [] will be replaced by dialect specific tick/quote character when there is not dialect specific expectation but only a default expectation
describe(Support.getTestDialectTeaser('SQL'), () => {
describe('addIndex', () => {
it('naming', () => {
expectsql(sql.addIndexQuery('table', ['column1', 'column2'], {}, 'table'), {
default: 'CREATE INDEX [table_column1_column2] ON [table] ([column1], [column2])',
mariadb: 'ALTER TABLE `table` ADD INDEX `table_column1_column2` (`column1`, `column2`)',
mysql: 'ALTER TABLE `table` ADD INDEX `table_column1_column2` (`column1`, `column2`)'
});
if (current.dialect.supports.schemas) {
expectsql(sql.addIndexQuery('schema.table', ['column1', 'column2'], {}), {
default: 'CREATE INDEX [schema_table_column1_column2] ON [schema].[table] ([column1], [column2])',
mariadb: 'ALTER TABLE `schema`.`table` ADD INDEX `schema_table_column1_column2` (`column1`, `column2`)'
});
expectsql(sql.addIndexQuery({
schema: 'schema',
tableName: 'table'
}, ['column1', 'column2'], {}, 'schema_table'), {
default: 'CREATE INDEX [schema_table_column1_column2] ON [schema].[table] ([column1], [column2])',
mariadb: 'ALTER TABLE `schema`.`table` ADD INDEX `schema_table_column1_column2` (`column1`, `column2`)'
});
expectsql(sql.addIndexQuery(sql.quoteTable(sql.addSchema({
_schema: 'schema',
tableName: 'table'
})), ['column1', 'column2'], {}), {
default: 'CREATE INDEX [schema_table_column1_column2] ON [schema].[table] ([column1], [column2])',
mariadb: 'ALTER TABLE `schema`.`table` ADD INDEX `schema_table_column1_column2` (`column1`, `column2`)'
});
}
});
it('type and using', () => {
expectsql(sql.addIndexQuery('User', ['fieldC'], {
type: 'FULLTEXT',
concurrently: true
}), {
sqlite: 'CREATE INDEX `user_field_c` ON `User` (`fieldC`)',
mssql: 'CREATE FULLTEXT INDEX [user_field_c] ON [User] ([fieldC])',
postgres: 'CREATE INDEX CONCURRENTLY "user_field_c" ON "User" ("fieldC")',
mariadb: 'ALTER TABLE `User` ADD FULLTEXT INDEX `user_field_c` (`fieldC`)',
mysql: 'ALTER TABLE `User` ADD FULLTEXT INDEX `user_field_c` (`fieldC`)'
});
expectsql(sql.addIndexQuery('User', ['fieldB', { attribute: 'fieldA', collate: 'en_US', order: 'DESC', length: 5 }], {
name: 'a_b_uniq',
unique: true,
using: 'BTREE',
parser: 'foo'
}), {
sqlite: 'CREATE UNIQUE INDEX `a_b_uniq` ON `User` (`fieldB`, `fieldA` COLLATE `en_US` DESC)',
mssql: 'CREATE UNIQUE INDEX [a_b_uniq] ON [User] ([fieldB], [fieldA] DESC)',
postgres: 'CREATE UNIQUE INDEX "a_b_uniq" ON "User" USING BTREE ("fieldB", "fieldA" COLLATE "en_US" DESC)',
mariadb: 'ALTER TABLE `User` ADD UNIQUE INDEX `a_b_uniq` USING BTREE (`fieldB`, `fieldA`(5) DESC) WITH PARSER foo',
mysql: 'ALTER TABLE `User` ADD UNIQUE INDEX `a_b_uniq` USING BTREE (`fieldB`, `fieldA`(5) DESC) WITH PARSER foo'
});
});
it('POJO field', () => {
expectsql(sql.addIndexQuery('table', [{ attribute: 'column', collate: 'BINARY', length: 5, order: 'DESC' }], {}, 'table'), {
default: 'CREATE INDEX [table_column] ON [table] ([column] COLLATE [BINARY] DESC)',
mssql: 'CREATE INDEX [table_column] ON [table] ([column] DESC)',
mariadb: 'ALTER TABLE `table` ADD INDEX `table_column` (`column`(5) DESC)',
mysql: 'ALTER TABLE `table` ADD INDEX `table_column` (`column`(5) DESC)'
});
});
it('function', () => {
expectsql(sql.addIndexQuery('table', [current.fn('UPPER', current.col('test'))], { name: 'myindex' }), {
default: 'CREATE INDEX [myindex] ON [table] (UPPER([test]))',
mariadb: 'ALTER TABLE `table` ADD INDEX `myindex` (UPPER(`test`))',
mysql: 'ALTER TABLE `table` ADD INDEX `myindex` (UPPER(`test`))'
});
});
if (current.dialect.supports.index.using === 2) {
it('USING', () => {
expectsql(sql.addIndexQuery('table', {
fields: ['event'],
using: 'gin'
}), {
postgres: 'CREATE INDEX "table_event" ON "table" USING gin ("event")'
});
});
}
if (current.dialect.supports.index.where) {
it('WHERE', () => {
expectsql(sql.addIndexQuery('table', {
fields: ['type'],
where: {
type: 'public'
}
}), {
sqlite: 'CREATE INDEX `table_type` ON `table` (`type`) WHERE `type` = \'public\'',
postgres: 'CREATE INDEX "table_type" ON "table" ("type") WHERE "type" = \'public\'',
mssql: 'CREATE INDEX [table_type] ON [table] ([type]) WHERE [type] = N\'public\''
});
expectsql(sql.addIndexQuery('table', {
fields: ['type'],
where: {
type: {
[Op.or]: [
'group',
'private'
]
}
}
}), {
sqlite: 'CREATE INDEX `table_type` ON `table` (`type`) WHERE (`type` = \'group\' OR `type` = \'private\')',
postgres: 'CREATE INDEX "table_type" ON "table" ("type") WHERE ("type" = \'group\' OR "type" = \'private\')',
mssql: 'CREATE INDEX [table_type] ON [table] ([type]) WHERE ([type] = N\'group\' OR [type] = N\'private\')'
});
expectsql(sql.addIndexQuery('table', {
fields: ['type'],
where: {
type: {
[Op.ne]: null
}
}
}), {
sqlite: 'CREATE INDEX `table_type` ON `table` (`type`) WHERE `type` IS NOT NULL',
postgres: 'CREATE INDEX "table_type" ON "table" ("type") WHERE "type" IS NOT NULL',
mssql: 'CREATE INDEX [table_type] ON [table] ([type]) WHERE [type] IS NOT NULL'
});
});
}
if (current.dialect.supports.JSONB) {
it('operator', () => {
expectsql(sql.addIndexQuery('table', {
fields: ['event'],
using: 'gin',
operator: 'jsonb_path_ops'
}), {
postgres: 'CREATE INDEX "table_event" ON "table" USING gin ("event" jsonb_path_ops)'
});
});
}
if (current.dialect.name === 'postgres') {
it('show indexes', () => {
expectsql(sql.showIndexesQuery('table'), {
postgres: 'SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, ' +
'array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) ' +
'AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a ' +
'WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND ' +
't.relkind = \'r\' and t.relname = \'table\' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;'
});
expectsql(sql.showIndexesQuery({ tableName: 'table', schema: 'schema' }), {
postgres: 'SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, ' +
'array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) ' +
'AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a, pg_namespace s ' +
'WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND ' +
't.relkind = \'r\' and t.relname = \'table\' AND s.oid = t.relnamespace AND s.nspname = \'schema\' ' +
'GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;'
});
});
}
if (current.dialect.supports.index.operator) {
it('operator with multiple fields', () => {
expectsql(sql.addIndexQuery('table', {
fields: ['column1', 'column2'],
using: 'gist',
operator: 'inet_ops'
}), {
postgres: 'CREATE INDEX "table_column1_column2" ON "table" USING gist ("column1" inet_ops, "column2" inet_ops)'
});
});
it('operator in fields', () => {
expectsql(sql.addIndexQuery('table', {
fields: [{
name: 'column',
operator: 'inet_ops'
}],
using: 'gist'
}), {
postgres: 'CREATE INDEX "table_column" ON "table" USING gist ("column" inet_ops)'
});
});
it('operator in fields with order', () => {
expectsql(sql.addIndexQuery('table', {
fields: [{
name: 'column',
order: 'DESC',
operator: 'inet_ops'
}],
using: 'gist'
}), {
postgres: 'CREATE INDEX "table_column" ON "table" USING gist ("column" inet_ops DESC)'
});
});
it('operator in multiple fields #1', () => {
expectsql(sql.addIndexQuery('table', {
fields: [{
name: 'column1',
order: 'DESC',
operator: 'inet_ops'
}, 'column2'],
using: 'gist'
}), {
postgres: 'CREATE INDEX "table_column1_column2" ON "table" USING gist ("column1" inet_ops DESC, "column2")'
});
});
it('operator in multiple fields #2', () => {
expectsql(sql.addIndexQuery('table', {
fields: [{
name: 'path',
operator: 'text_pattern_ops'
}, 'level', {
name: 'name',
operator: 'varchar_pattern_ops'
}],
using: 'btree'
}), {
postgres: 'CREATE INDEX "table_path_level_name" ON "table" USING btree ("path" text_pattern_ops, "level", "name" varchar_pattern_ops)'
});
});
}
});
describe('removeIndex', () => {
it('naming', () => {
expectsql(sql.removeIndexQuery('table', ['column1', 'column2'], {}, 'table'), {
mariadb: 'DROP INDEX `table_column1_column2` ON `table`',
mysql: 'DROP INDEX `table_column1_column2` ON `table`',
mssql: 'DROP INDEX [table_column1_column2] ON [table]',
default: 'DROP INDEX IF EXISTS [table_column1_column2]'
});
});
});
});