forked from db-migrate/pg
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
679 lines (599 loc) · 17.8 KB
/
index.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
var util = require('util');
var mssql = require('mssql');
var Base = require('db-migrate-base');
var Promise = require('bluebird');
var MssqlDriver = Base.extend({
init: function (connection, schema, intern) {
this.log = intern.mod.log;
this.type = intern.mod.type;
this._escapeString = "'";
this._super(intern);
this.internals = intern;
this.connection = connection;
this.schema = schema || 'dbo';
},
createColumnDef: function (name, spec, options, tableName) {
var type =
spec.primaryKey && spec.autoIncrement ? '' : this.mapDataType(spec.type);
var len = spec.length ? util.format('(%s)', spec.length) : '';
var constraint = this.createColumnConstraint(
spec,
options,
tableName,
name
);
if (name.charAt(0) !== '[') {
name = `[${name}]`;
}
return {
foreignKey: constraint.foreignKey,
callbacks: constraint.callbacks,
constraints: [name, type, len, constraint.constraints].join(' ')
};
},
escapeDDL: function (strOrArray) {
// if its an array we're going to assume it's: [schemaName, tableName]
if (Array.isArray(strOrArray)) {
if (strOrArray.length === 2) {
return `[${strOrArray[0]}].[${strOrArray[1]}]`;
}
return `[${strOrArray[0]}]`;
}
return `[${strOrArray}]`;
},
_translateSpecialDefaultValues: function (
spec,
options,
tableName,
columnName
) {
switch (spec.defaultValue.special) {
case 'CURRENT_TIMESTAMP':
spec.defaultValue.prep = 'CURRENT_TIMESTAMP';
break;
default:
this.super(spec, options, tableName, columnName);
break;
}
},
mapDataType: function (str) {
switch (str) {
case 'json':
case 'jsonb':
return str.toUpperCase();
case this.type.STRING:
return 'NVARCHAR';
case this.type.DATE_TIME:
return 'DATETIME2';
case this.type.BLOB:
return 'VARBINARY(MAX)';
}
return this._super(str);
},
createDatabase: function (dbName, options, callback) {
var spec = '';
if (typeof options === 'function') callback = options;
this.runSql(
util.format('CREATE DATABASE %s %s', this.escapeDDL(dbName), spec),
callback
);
},
dropDatabase: function (dbName, options, callback) {
var ifExists = '';
if (typeof options === 'function') callback = options;
else {
ifExists = options.ifExists === true ? 'IF EXISTS' : '';
}
this.runSql(
util.format('DROP DATABASE %s %s', ifExists, this.escapeDDL(dbName)),
callback
);
},
createSequence: function (sqName, options, callback) {
var spec = '';
var temp = '';
if (typeof options === 'function') callback = options;
else {
temp = options.temp === true ? 'TEMP' : '';
}
this.runSql(
util.format('CREATE %s SEQUENCE [%s] %s', temp, sqName, spec),
callback
);
},
switchDatabase: function (options, callback) {
if (typeof options === 'object') {
if (typeof options.database === 'string') {
this.log.info(
'Ignore database option, not available with postgres. Use schema instead!'
);
this.runSql(
util.format('USE [%s]', options.database),
callback
);
}
} else if (typeof options === 'string') {
this.runSql(util.format('USE [%s]', options), callback);
} else callback(null);
},
dropSequence: function (dbName, options, callback) {
var ifExists = '';
var rule = '';
if (typeof options === 'function') callback = options;
else {
ifExists = options.ifExists === true ? 'IF EXISTS' : '';
if (options.cascade === true) rule = 'CASCADE';
else if (options.restrict === true) rule = 'RESTRICT';
}
this.runSql(
util.format('DROP SEQUENCE %s [%s] %s', ifExists, dbName, rule),
callback
);
},
createMigrationsTable: function (callback) {
var options = {
columns: {
id: {
type: this.type.INTEGER,
notNull: true,
primaryKey: true,
autoIncrement: true
},
name: { type: this.type.STRING, length: 255, notNull: true },
run_on: { type: this.type.DATE_TIME, notNull: true }
},
ifNotExists: false
};
return this.all(`
SELECT table_name
FROM information_schema.tables
WHERE table_name = '${this.internals.migrationTable}'
AND table_schema = '${this.schema}'`
)
.then(
function (result) {
if (result && result.length < 1) {
return this.createTable([this.schema, this.internals.migrationTable], options);
} else {
return Promise.resolve();
}
}.bind(this)
)
.nodeify(callback);
},
createSeedsTable: function (callback) {
var options = {
columns: {
id: {
type: this.type.INTEGER,
notNull: true,
primaryKey: true,
autoIncrement: true
},
name: { type: this.type.STRING, length: 255, notNull: true },
run_on: { type: this.type.DATE_TIME, notNull: true }
},
ifNotExists: false
};
return this.all(`
SELECT table_name
FROM information_schema.tables
WHERE table_name = '${this.internals.seedTable}'
AND table_schema = '${this.schema}'`
)
.then(
function (result) {
if (result && result.length < 1) {
return this.createTable([this.schema, this.internals.seedTable], options);
} else {
return Promise.resolve();
}
}.bind(this)
)
.nodeify(callback);
},
createColumnConstraint: function (spec, options, tableName, columnName) {
var constraint = [];
var callbacks = [];
var cb;
if (spec.primaryKey) {
if (spec.autoIncrement) {
constraint.push('INT IDENTITY');
}
if (options.emitPrimaryKey) {
constraint.push('PRIMARY KEY');
}
}
if (spec.notNull === true) {
constraint.push('NOT NULL');
}
if (spec.unique) {
constraint.push('UNIQUE');
}
if (spec.defaultValue !== undefined) {
constraint.push('DEFAULT');
if (typeof spec.defaultValue === 'string') {
constraint.push("'" + spec.defaultValue + "'");
} else if (typeof spec.defaultValue.prep === 'string') {
constraint.push(spec.defaultValue.prep);
} else {
constraint.push(spec.defaultValue);
}
}
// keep foreignKey for backward compatiable, push to callbacks in the future
if (spec.foreignKey) {
cb = this.bindForeignKey([this.schema, tableName], columnName, spec.foreignKey);
}
return {
foreignKey: cb,
callbacks: callbacks,
constraints: constraint.join(' ')
};
},
renameTable: function (tableName, newTableName, callback) {
var sql = util.format(
'ALTER TABLE [%s].[%s] RENAME TO [%s]',
this.schema,
tableName,
newTableName
);
return this.runSql(sql).nodeify(callback);
},
addColumn: function(tableName, columnName, columnSpec, callback) {
var columnSpec = this.normalizeColumnSpec(columnSpec);
this._prepareSpec(columnName, columnSpec, {}, tableName);
var def = this.createColumnDef(columnName, columnSpec, {}, tableName);
var extensions = '';
var self = this;
if (typeof this._applyAddColumnExtension === 'function') {
extensions = this._applyAddColumnExtension(def, tableName, columnName);
}
var sql = util.format(
'ALTER TABLE %s ADD %s %s',
this.escapeDDL(tableName),
def.constraints,
extensions
);
return this.runSql(sql)
.then(function() {
var callbacks = def.callbacks || [];
if (def.foreignKey) callbacks.push(def.foreignKey);
return self.recurseCallbackArray(callbacks);
})
.nodeify(callback);
},
removeColumn: function (tableName, columnName, callback) {
var sql = util.format(
'ALTER TABLE [%s].[%s] DROP COLUMN [%s]',
this.schema,
tableName,
columnName
);
return this.runSql(sql).nodeify(callback);
},
removeIndex: function (tableName, indexName, callback) {
var sql = util.format(
'DROP INDEX [%s] ON [%s].[%s]',
indexName,
this.schema,
tableName,
);
return this.runSql(sql).nodeify(callback);
},
renameColumn: function (tableName, oldColumnName, newColumnName, callback) {
var sql = `
IF EXISTS (
SELECT 1
FROM sys.columns
WHERE name = '${oldColumnName}'
AND object_name(object_id) = '${this.escapeDDL(tableName)}'
) AND NOT EXISTS (
SELECT 1
FROM sys.columns
WHERE name = '${newColumnName}'
AND object_name(object_id) = '${this.escapeDDL(tableName)}'
)
EXEC sp_RENAME
'${this.escapeDDL(this.schema)}.${this.escapeDDL(tableName)}.${oldColumnName}',
'${newColumnName}',
'COLUMN';
`;
return this.runSql(sql).nodeify(callback);
},
changeColumn: function (tableName, columnName, columnSpec, callback) {
return setNotNull.call(this);
function setNotNull () {
var setOrDrop = columnSpec.notNull === true ? 'SET' : 'DROP';
var sql = util.format(
'ALTER TABLE [%s].[%s] ALTER COLUMN [%s] %s NOT NULL',
this.schema,
tableName,
columnName,
setOrDrop
);
return this.runSql(sql).nodeify(setUnique.bind(this));
}
function setUnique (err) {
if (err) {
return Promise.reject(err);
}
var sql;
var constraintName = tableName + '_' + columnName + '_key';
if (columnSpec.unique === true) {
sql = util.format(
'ALTER TABLE [%s].[%s] ADD CONSTRAINT [%s] UNIQUE ([%s])',
this.schema,
tableName,
constraintName,
columnName
);
return this.runSql(sql).nodeify(setDefaultValue.bind(this));
} else if (columnSpec.unique === false) {
sql = util.format(
'ALTER TABLE [%s].[%s] DROP CONSTRAINT [%s]',
this.schema,
tableName,
constraintName
);
return this.runSql(sql).nodeify(setDefaultValue.bind(this));
} else {
return setDefaultValue.call(this);
}
}
function setDefaultValue (err) {
if (err) {
return Promise.reject(err).nodeify(callback);
}
var sql;
if (columnSpec.defaultValue !== undefined) {
var defaultValue = null;
if (typeof columnSpec.defaultValue === 'string') {
defaultValue = "'" + columnSpec.defaultValue + "'";
} else {
defaultValue = columnSpec.defaultValue;
}
sql = util.format(
'ALTER TABLE [%s].[%s] ALTER COLUMN [%s] SET DEFAULT %s',
this.schema,
tableName,
columnName,
defaultValue
);
} else {
sql = util.format(
'ALTER TABLE [%s].[%s] ALTER COLUMN [%s] DROP DEFAULT',
this.schema,
tableName,
columnName
);
}
return this.runSql(sql)
.then(setType.bind(this))
.nodeify(callback);
}
function setType () {
if (columnSpec.type !== undefined) {
var using =
columnSpec.using !== undefined
? columnSpec.using
: util.format(
'USING [%s]::%s',
columnName,
this.mapDataType(columnSpec.type)
);
var len = columnSpec.length
? util.format('(%s)', columnSpec.length)
: '';
var sql = util.format(
'ALTER TABLE [%s].[%s] ALTER COLUMN [%s] TYPE %s %s %s',
this.schema,
tableName,
columnName,
this.mapDataType(columnSpec.type),
len,
using
);
return this.runSql(sql);
}
}
},
addForeignKey: function (
tableName,
referencedTableName,
keyName,
fieldMapping,
rules,
callback
) {
if (arguments.length === 5 && typeof rules === 'function') {
callback = rules;
rules = {};
}
var columns = Object.keys(fieldMapping);
var referencedColumns = columns.map(function (key) {
return '"' + fieldMapping[key] + '"';
});
var sql = util.format(
'ALTER TABLE %s ADD CONSTRAINT [%s] FOREIGN KEY (%s) REFERENCES [%s] (%s) ON DELETE %s ON UPDATE %s',
this.escapeDDL(tableName),
keyName,
this.escapeDDL(columns),
referencedTableName,
this.escapeDDL(referencedColumns),
rules.onDelete || 'NO ACTION',
rules.onUpdate || 'NO ACTION'
);
return this.runSql(sql).nodeify(callback);
},
removeForeignKey: function (tableName, keyName, callback) {
var sql = util.format(
'ALTER TABLE [%s].[%s] DROP CONSTRAINT [%s]',
this.schema,
tableName,
keyName
);
return this.runSql(sql).nodeify(callback);
},
insert: function () {
var index = 1;
if (arguments.length > 3) {
index = 2;
}
arguments[index] = arguments[index].map(function (value) {
return typeof value === 'string' ? value : JSON.stringify(value);
});
return this._super.apply(this, arguments);
},
runSql: function () {
var callback;
var minLength = 1;
var params;
if (typeof arguments[arguments.length - 1] === 'function') {
minLength = 2;
callback = arguments[arguments.length - 1];
}
params = arguments;
if (params.length > minLength) {
// We have parameters, but db-migrate uses "?" for param substitutions.
// MSSQL uses "@paramname1", "@paramname2", etc so fix up the "?" into "$1", etc
var param = params[0].split('?');
var newParam = [];
for (var i = 0; i < param.length - 1; i++) {
newParam.push(param[i], '@input_' + (i + 1));
}
newParam.push(param[param.length - 1]);
params[0] = newParam.join('');
}
this.log.sql.apply(null, params);
if (this.internals.dryRun) {
return Promise.resolve().nodeify(callback);
}
return new Promise(
function (resolve, reject) {
if (this.internals.notransactions) {
const request = new this.connection.Request();
if (params[1]) {
for (let i = 0; i < params[1].length; i++) {
request.input(`input_${i + 1}`, params[1][i]);
}
}
request.query(params[0]).then(result => {
resolve(result.recordset);
}, reject);
}
else {
const transaction = new this.connection.Transaction();
transaction.begin(() => {
const request = new this.connection.Request(transaction);
if (params[1]) {
for (let i = 0; i < params[1].length; i++) {
request.input(`input_${i + 1}`, params[1][i]);
}
}
request.query(params[0]).then(result => {
transaction.commit();
resolve(result.recordset);
}, (error) => {
transaction.rollback(err => {
if (err) {
this.log.error('Unable to rollback transaction.');
this.log.error(err);
}
});
reject(error);
});
})
}
}.bind(this)
).nodeify(callback);
},
all: function () {
var params = arguments;
this.log.sql.apply(null, params);
return new Promise(
function (resolve, reject) {
const request = new this.connection.Request();
request.query(params[0]).then(result => {
resolve(result.recordset);
}, reject);
}.bind(this)
).nodeify(params[1]);
},
close: function (callback) {
const promise = new Promise((resolve) => {
// circumvent Idle connections not being closed by the pool #457
// https://github.com/tediousjs/node-mssql/issues/457
setTimeout(resolve, 200);
}).then(() => {
return this.connection.close();
});
if (typeof callback === 'function') {
return Promise.resolve(promise).nodeify(callback);
} else return Promise.resolve(promise);
},
/**
* Extend Base because they do not let schema to be passed
*/
addMigrationRecord: function (name, callback) {
this.runSql(
'INSERT INTO ' +
this.escapeDDL([this.schema, this.internals.migrationTable]) +
' (' +
this.escapeDDL('name') +
', ' +
this.escapeDDL('run_on') +
') VALUES (?, ?)',
[name, new Date()],
callback
);
},
addSeedRecord: function (name, callback) {
this.runSql(
'INSERT INTO ' +
this.escapeDDL([this.schema, this.internals.seedTable]) +
' (' +
this.escapeDDL('name') +
', ' +
this.escapeDDL('run_on') +
') VALUES (?, ?)',
[name, new Date()],
callback
);
},
allLoadedMigrations: function (callback) {
var sql =
'SELECT * FROM ' +
this.escapeDDL([this.schema, this.internals.migrationTable]) +
' ORDER BY run_on DESC, name DESC';
return this.all(sql, callback);
},
allLoadedSeeds: function (callback) {
var sql =
'SELECT * FROM ' +
this.escapeDDL([this.schema, this.internals.seedTable]) +
' ORDER BY run_on DESC, name DESC';
return this.all(sql, callback);
},
deleteMigration: function (migrationName, callback) {
var sql =
'DELETE FROM ' +
this.escapeDDL([this.schema, this.internals.migrationTable]) +
' WHERE name = ?';
this.runSql(sql, [migrationName], callback);
}
});
Promise.promisifyAll(MssqlDriver);
exports.connect = function (config, intern, callback) {
if (!config.database) {
config.database = 'mssql';
}
config.server = config.server || config.host;
mssql.connect(config)
.then(pool => {
callback(null, new MssqlDriver(mssql, config.schema, intern));
})
.catch(err => {
callback(err);
});
};
exports.base = MssqlDriver;