-
Notifications
You must be signed in to change notification settings - Fork 79
/
autoupdate.test.js
224 lines (212 loc) · 6.72 KB
/
autoupdate.test.js
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
// Copyright IBM Corp. 2014,2019. All Rights Reserved.
// Node module: loopback-connector-mssql
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
require('./init.js');
const assert = require('assert');
let ds;
before(function() {
/* global getDataSource */
ds = getDataSource();
});
describe('MS SQL server connector', function() {
it('should auto migrate/update tables', function(done) {
this.timeout(30000);
/* eslint-disable camelcase */
const schema_v1 =
{
name: 'CustomerTest',
options: {
idInjection: false,
mssql: {
schema: 'dbo',
table: 'CUSTOMER_TEST',
},
indexes: {
idEmailIndex: {
keys: {
id: 1,
email: 1,
},
options: {
unique: true,
},
columns: 'id,email',
kind: 'unique',
},
},
},
properties: {
id: {
type: 'String',
length: 20,
id: 1,
},
name: {
type: 'String',
required: false,
length: 40,
},
email: {
type: 'String',
required: true,
length: 40,
},
age: {
type: 'Number',
required: false,
},
firstName: {
type: 'String',
required: false,
},
},
};
const schema_v2 =
{
name: 'CustomerTest',
options: {
idInjection: false,
mssql: {
schema: 'dbo',
table: 'CUSTOMER_TEST',
},
indexes: {
idCityIndex: {
keys: {
id: 1,
city: 1,
},
options: {
unique: true,
},
columns: 'id,city',
kind: 'unique',
},
},
},
properties: {
id: {
type: 'String',
length: 20,
id: 1,
},
email: {
type: 'String',
required: false,
length: 60,
mssql: {
columnName: 'EMAIL',
dataType: 'nvarchar',
dataLength: 60,
nullable: 'YES',
},
},
firstName: {
type: 'String',
required: false,
length: 40,
},
lastName: {
type: 'String',
required: false,
length: 40,
},
city: {
type: 'String',
required: false,
length: 40,
index: {
unique: true,
},
},
},
};
ds.createModel(schema_v1.name, schema_v1.properties, schema_v1.options);
/* eslint-enable camelcase */
ds.automigrate(function(err) {
assert(!err);
ds.discoverModelProperties('CUSTOMER_TEST', function(err, props) {
assert(!err);
assert.equal(props.length, 5);
const names = props.map(function(p) {
return p.columnName;
});
assert.equal(props[0].nullable, 'NO');
assert.equal(props[1].nullable, 'YES');
assert.equal(props[2].nullable, 'NO');
assert.equal(props[3].nullable, 'YES');
assert.equal(names[0], 'id');
assert.equal(names[1], 'name');
assert.equal(names[2], 'email');
assert.equal(names[3], 'age');
/* eslint-disable camelcase */
ds.createModel(schema_v2.name, schema_v2.properties, schema_v2.options);
/* eslint-enable camelcase */
ds.autoupdate(function(err, result) {
ds.discoverModelProperties('CUSTOMER_TEST', function(err, props) {
assert.equal(props.length, 5);
const names = props.map(function(p) {
return p.columnName;
});
assert.equal(names[0], 'id');
assert.equal(names[1], 'email');
assert.equal(names[2], 'firstName');
assert.equal(names[3], 'lastName');
assert.equal(names[4], 'city');
const schema = "'dbo'";
const table = "'CUSTOMER_TEST'";
const sql = 'SELECT OBJECT_SCHEMA_NAME(T.[object_id],DB_ID()) AS [table_schema],' +
' T.[name] AS [Table], I.[name] AS [Key_name], AC.[name] AS [Column_name],' +
' I.[type_desc], I.[is_unique], I.[data_space_id], I.[ignore_dup_key], I.[is_primary_key],' +
' I.[is_unique_constraint], I.[fill_factor], I.[is_padded], I.[is_disabled], I.[is_hypothetical],' +
' I.[allow_row_locks], I.[allow_page_locks], IC.[is_descending_key], IC.[is_included_column]' +
' FROM sys.[tables] AS T' +
' INNER JOIN sys.[indexes] I ON T.[object_id] = I.[object_id]' +
' INNER JOIN sys.[index_columns] IC ON I.[object_id] = IC.[object_id]' +
' INNER JOIN sys.[all_columns] AC ON T.[object_id] = AC.[object_id] AND IC.[column_id] = AC.[column_id]' +
' WHERE T.[is_ms_shipped] = 0 AND I.[type_desc] <> \'HEAP\'' +
' AND OBJECT_SCHEMA_NAME(T.[object_id],DB_ID()) = ' + schema + ' AND T.[name] = ' + table +
' ORDER BY T.[name], I.[index_id], IC.[key_ordinal]';
ds.connector.execute(sql, function(err, indexes) {
let countIdEmailIndex = 0;
let countIdCityIndex = 0;
let countCityIndex = 0;
let countAgeIndex = 0;
for (let i = 0; i < indexes.length; i++) {
if (indexes[i].Key_name == 'id_email_unique_ASC_idx') {
countIdEmailIndex++;
}
if (indexes[i].Key_name == 'id_city_unique_ASC_idx') {
countIdCityIndex++;
}
if (indexes[i].Key_name == 'city_NONCLUSTERED_ASC_idx') {
countCityIndex++;
}
if (indexes[i].Key_name == 'age_NONCLUSTERED_ASC_idx') {
countAgeIndex++;
}
}
assert.equal(countIdEmailIndex, 0);
assert.equal(countAgeIndex, 0);
assert.equal(countIdCityIndex > 0, true);
assert.equal(countCityIndex > 0, true);
done(err, result);
});
});
});
});
});
});
it('should report errors for automigrate', function() {
ds.automigrate('XYZ', function(err) {
assert(err);
});
});
it('should report errors for autoupdate', function() {
ds.autoupdate('XYZ', function(err) {
assert(err);
});
});
});