-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
736 lines (604 loc) · 22.1 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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
// Directions:
// https://db-migrate.readthedocs.io/en/latest/Developers/contributing/#creating-your-own-driver
//
// Couchbase driver docs:
// http://docs.couchbase.com/sdk-api/couchbase-node-client-2.1.4/Cluster.html
//
// Conventions:
// driver's noSQL API refers to "collections" which Couchbase doesn't have. We
// use buckets to be collections.
// RDBMS "Tables" are Ottoman models.
// RDBMS "Rows" are ottoman model instances (documents)
// RDBMS "Columns" are paths into an ottoman model. Not just attributes, but paths.
// RDBMS "Databases" are Couchbase buckets.
import couchbase from 'couchbase';
import Promise from 'bluebird';
import Base from 'db-migrate-base';
import bunyan from 'bunyan';
import PrettyStream from 'bunyan-prettystream';
const ottoman = require('ottoman');
import moment from 'moment';
import deasync from 'deasync';
const prettyStdOut = new PrettyStream();
prettyStdOut.pipe(process.stdout);
/**
* contains the standard datatypes of db-migrate
*/
let type = null;
const UNSUPPORTED = 'operation not supported';
const ottomanType = '_type';
const log = bunyan.createLogger({
name: 'db-migrate-couchbase',
streams: [{
level: 'debug',
type: 'raw',
stream: prettyStdOut,
}],
});
let internalLogger = null;
const DEFAULTS = {
host: (process.env.COUCHBASE_HOST || 'couchbase://localhost'),
port: 8091,
user: 'admin',
password: 'admin001*',
};
// Escape n1ql identifiers so they are syntactically valid even if they
// contain reserved words. So foo.bar becomes `foo`.`bar`
const n1qlEscape = (param) => param.split('.').map(p => `\`${p}\``).join('.');
let singleton = null;
const CouchbaseDriver = Base.extend({
init: function (connection, internals, config) {
if (singleton) {
log.warning('You should only create one instance of the db-migrate-couchbase driver');
return singleton;
}
if (internals && internals.argv && internals.argv['sql-file']) {
throw new Error('This driver does not support the --sql-file option.');
}
if (internals && internals.migrationTable !== 'migrations') {
log.warn('Driver ignores migration table option; it uses ottoman model MigrationRun');
internals.migrationTable = 'migrations';
}
if (internals && internals.seedTable !== 'seeds') {
log.warn('Driver ignores seed table option; it uses ottoman model MigrationSeed');
internals.seedTable = 'seeds';
}
log.debug('init');
// console.log(internals);
this._super(internals);
this.connection = connection;
this.user = config.user;
this.password = config.password;
this.bucketPassword = config.bucketPassword;
this.manager = connection.manager(this.user, this.password);
this.models = {};
if (!config.migrationBucket) {
throw new Error('Configuration must specify migrationBucket');
}
const errCallback = err => {
if (err) {
throw new Error(`Could not open migration bucket: ${err}`);
}
};
this.migrationBucket = (this.bucketPassword ?
this.connection.openBucket(config.migrationBucket, this.bucketPassword, errCallback) :
this.connection.openBucket(config.migrationBucket, errCallback));
this.active = this.migrationBucket;
ottoman.bucket = this.migrationBucket;
this.migrationAdapter = new ottoman.StoreAdapter.Couchbase(this.migrationBucket);
// Property names are obligatory here; the migrate framework expects exactly these,
// "name" and "run_on"
this.MigrationRun = ottoman.model('MigrationRun', {
oid: { type: 'string', auto: 'uuid', readonly: true },
name: { type: 'string' },
run_on: { type: 'Date' },
},
{
id: 'oid',
// store: this.migrationAdapter,
});
this.MigrationSeed = ottoman.model('MigrationSeed', {
oid: { type: 'string', auto: 'uuid', readonly: true },
name: { type: 'string' },
run_on: { type: 'Date' },
ifNotExists: { type: 'boolean', default: true },
},
{
id: 'oid',
});
this.ready = false;
ottoman.ensureIndices(err => {
if (err) {
log.error('Failed to create ottoman indices', { err });
}
this.ready = true;
});
this.models.MigrationRun = this.MigrationRun;
singleton = this;
return singleton;
},
activeBucketName: function () {
return singleton.active._name;
},
runN1ql: function (query, params, callback) {
const n1ql = couchbase.N1qlQuery.fromString(query);
// Use strong consistency on all migration queries, to avoid errors or missing records
// owing from cluster synchronization.
n1ql.consistency(couchbase.N1qlQuery.Consistency.STATEMENT_PLUS);
if (!singleton.active) {
return Promise.reject('no active bucket').nodeify(callback);
}
if (singleton.internals.dryRun) {
log.info('DRY RUN', { query, params });
return Promise.resolve(true).nodeify(callback);
}
return new Promise((resolve, reject) => {
log.info(query);
return singleton.active.query(n1ql, params, (err, rows, meta) => {
if (err) {
log.error('n1ql error', { err });
return reject(err);
}
return resolve({ rows, meta });
});
}).nodeify(callback);
},
close: function (callback) {
log.info('close');
// Couchbase doesn't disconnect nicely, leaves open threads.
setTimeout(() => {
log.info('Time!');
process.exit(0);
}, 5000);
callback();
},
/**
* Provided for compatibility with the driver, but this is an alias for runN1ql
*/
runSql: function (query, params, callback) {
log.info('runSql', { query, params });
return singleton.runN1ql(query, params, callback);
},
/**
* Provided for compatibility with the driver, but this is an alias for runN1ql
*/
all: function (query, params, callback) {
log.info('all', { query, params });
return singleton.runN1ql(query, params, callback);
},
/**
* Queries the migrations table
*
* @param callback
*/
allLoadedMigrations: function (callback) {
log.info('allLoadedMigrations');
return new Promise((resolve, reject) => {
singleton.getModel('MigrationRun').find({},
{
sort: ['run_on DESC', 'name DESC'],
consistency: ottoman.Consistency.GLOBAL,
},
(err, models) => {
if (err) { return reject(err); }
return resolve(models);
});
}).nodeify(callback);
},
/**
* Deletes a migration
*
* @param migrationName - The name of the migration to be deleted
* @param callback
*/
deleteMigration: function (migrationName, callback) {
log.info('deleteMigration', { migrationName });
if (singleton.internals.dryRun) {
return Promise.resolve(true).nodeify(callback);
}
return new Promise((resolve, reject) => {
singleton.MigrationRun.find({
name: migrationName,
}, {}, (err, rows) => {
if (err) { return reject(err); }
if (rows.length === 0) {
return resolve(false);
}
const deletePromises = rows.map(migRecord => new Promise((delResolve, delReject) => {
log.info('Deleting migration record ', { migRecord });
return migRecord.remove(removeErr => {
if (removeErr) { return delReject(removeErr); }
return delResolve(true);
});
}));
return Promise.all(deletePromises).then(r => resolve(r));
});
}).nodeify(callback);
},
mapDataType: function (spec) {
const map = {};
const ottomanTypes = ['string', 'integer', 'Date', 'number', 'boolean', 'Mixed'];
// Already an ottoman type?
if (ottomanTypes.indexOf(spec.type) !== -1) {
return spec.type;
}
map[type.TEXT] = map[type.CHAR] = map[type.STRING] = 'string';
map[type.INTEGER] = map[type.SMALLINT] = 'integer';
map[type.BIGINT] = 'string';
map[type.REAL] = map[type.DECIMAL] = map[type.REAL] = 'number';
map[type.BOOLEAN] = 'boolean';
map[type.DATE] = map[type.DATE_TIME] = 'Date';
map[type.BLOB] = 'string';
map[type.TIME] = 'string';
map[type.BINARY] = 'string';
if (spec.type in map) {
return map[spec.type];
}
return 'string';
},
/**
* Unsupported; if Couchbase supported transactions, here's where
* START TRANSACTION would go.
*/
startMigration: function (cb) {
log.info('startMigration');
return Promise.resolve(true).nodeify(cb);
},
/**
* Unsupported; if Couchbase supported transactions, here's where
* COMMIT would go.
*/
endMigration: function (cb) {
log.info('endMigration');
return Promise.resolve(true).nodeify(cb);
},
/**
* Remembers a model for later reference.
* @param modelName name of the model
* @param ottomanModel the actual model
* @returns the driver instance for chaining.
*/
registerModel: function (modelName, ottomanModel) {
singleton.models[modelName] = ottomanModel;
return singleton;
},
/**
* Return a model that this migration driver knows about
* @param modelName the name of the model.
*/
getModel: function (modelName) {
return singleton.models[modelName];
},
/**
* Create table: just an alias for making an ottoman model
*/
createTable: function (ottomanModelName, schema, callback) {
if (ottomanModelName === 'migration') {
return Promise.resolve(singleton.MigrationRun).nodeify(callback);
}
if (ottomanModelName === 'seeds') {
return Promise.resolve(singleton.MigrationSeed).nodeify(callback);
}
log.info('create table / ottoman model', { ottomanModelName, schema });
// This is a special case, we store migrations under ottoman model MigrationRun.
// Also, createTable when migration starts creates a table with a field called 'id',
// which isn't permitted in ottoman.
if (ottomanModelName === 'migrations') {
return Promise.resolve(this.MigrationRun).nodeify(callback);
}
const model = ottoman.model(ottomanModelName, schema);
singleton.registerModel(ottomanModelName, model);
return Promise.resolve(singleton.getModel(ottomanModelName)).nodeify(callback);
},
/**
* Create an ottoman model; alias for createTable
*/
createOttomanModel: function (ottomanModelName, options, callback) {
return singleton.createTable(ottomanModelName, options, callback);
},
/** unsupported */
dropTable: function (tableName, options, callback) {
log.info('dropTable', { tableName, options });
return Promise.reject(UNSUPPORTED).nodeify(callback);
},
dropOttomanModel: function (ottomanModelName, options, callback) {
return singleton.dropTable(ottomanModelName, options, callback);
},
/**
* Unsupported; renaming a model is easy, but carrying with it and pruning its indexes is hard.
*/
renameTable: function (ottomanModelName, newOttomanModelName, callback) {
log.info('renameTable', { ottomanModelName, newOttomanModelName });
return Promise.reject(UNSUPPORTED).nodeify(callback);
},
renameOttomanModel: function (ottomanModelName, newOttomanModelName, callback) {
return singleton.renameTable(ottomanModelName, newOttomanModelName, callback);
},
addColumn: function (ottomanModelName, modelPath, pathSpec, callback) {
log.info('addColumn', { ottomanModelName, modelPath, pathSpec });
if (!ottomanModelName) {
return Promise.reject('missing ottoman model name').nodeify(callback);
}
if (!modelPath) {
return Promise.reject('missing model path').nodeify(callback);
}
// TODO -- default value of null won't work for all ottoman types, like
// Mixed, ref, etc. But it will work for almost all primitive types.
// const ottomanType = this.mapDataType(pathSpec);
const q = `
UPDATE \`${singleton.active._name}\`
SET ${n1qlEscape(modelPath)} = null
WHERE \`${ottomanType}\` = '${ottomanModelName}'`;
return singleton.runN1ql(q, {}, callback);
},
addOttomanPath: function (ottomanModelName, modelPath, pathSpec, callback) {
return singleton.addColumn(ottomanModelName, modelPath, pathSpec, callback);
},
removeColumn: function (ottomanModelName, modelPath, callback) {
log.info('removeColumn', { ottomanModelName, modelPath });
const q = `
UPDATE \`${singleton.active._name}\`
UNSET ${n1qlEscape(modelPath)}
WHERE \`${ottomanType}\` = '${ottomanModelName}'`;
return singleton.runN1ql(q, {}, callback);
},
removeOttomanPath: function (ottomanModelName, modelPath, callback) {
return singleton.removeColumn(ottomanModelName, modelPath, callback);
},
/**
* alias
* @see renameOttomanPath
*/
renameColumn: function (ottomanModelName, oldModelPath, newModelPath, callback) {
log.info('renameColumn', { ottomanModelName, oldModelPath, newModelPath });
const q = `
UPDATE \`${singleton.active._name}\`
SET ${n1qlEscape(newModelPath)} = ${n1qlEscape(oldModelPath)}
UNSET ${n1qlEscape(oldModelPath)}
WHERE \`${ottomanType}\` = '${ottomanModelName}'`;
log.info('renameColumn', { q });
return singleton.runN1ql(q, {}, callback);
},
/**
* Use this function with care, it does **not** adjust any indexes that might already be
* on the path, so consider dropping those before doing this.
*/
renameOttomanPath: function (ottomanModelName, oldModelPath, newModelPath, callback) {
return singleton.renameColumn(ottomanModelName, oldModelPath, newModelPath, callback);
},
changeColumn: function (ottomanModelName, ottomanPathName, pathSpec, callback) {
log.info('changeColumn', { ottomanModelName, ottomanPathName, pathSpec });
return Promise.reject(UNSUPPORTED).nodeify(callback);
},
/**
* Couchbase doesn't support this as such, because the concepts of columnNameArrays
* and valueArrays aren't really a good fit.
*/
insert: function (tableName, columnNameArray, valueArray, callback) {
log.info('insert', { tableName, columnNameArray, valueArray });
return Promise.reject(UNSUPPORTED).nodeify(callback);
},
addMigrationRecord: function (name, callback) {
log.info('addMigrationRecord', { name });
const i = new singleton.MigrationRun({
name,
run_on: moment().utc(),
});
if (singleton.internals.dryRun) {
return Promise.resolve(true).nodeify(callback);
}
return new Promise((resolve, reject) => i.save(err => {
if (err) { return reject(err); }
return resolve(true);
})).nodeify(callback);
},
/**
* Creates a bucket with a given name, and sets it to be active.
*/
createBucket: function (bucketName, options, callback) {
log.info('createBucket', { bucketName });
return new Promise((resolve, reject) =>
singleton.manager.createBucket(bucketName,
(options || {}),
err => {
if (err) { return reject(err); }
log.info('Created bucket', { bucketName, err });
singleton.active = singleton.connection.openBucket(bucketName);
return resolve(singleton.active);
})).nodeify(callback);
},
/**
* Drops a bucket. Note, due to couchbase server implementation and what it
* takes to drop a bucket, this may take quite some time to resolve (> 1 min, even in
* small cases). Do not use this unless you know what you're doing, as this function
* can destroy quite a lot very quickly.
*/
dropBucket: function (bucketName, callback) {
log.info('dropBucket', { bucketName });
if (singleton.internals.dryRun) {
return Promise.resolve(true).nodeify(callback);
}
return new Promise((resolve, reject) => singleton.manager.removeBucket(bucketName, (err) => {
if (err) { return reject(err); }
// Remove active if we just remove that one, because it's a reference that's
// no longer valid.
if (singleton.active._name === bucketName) {
singleton.active = null;
}
return resolve(true);
})).nodeify(callback);
},
changeType: function (ottomanModelName, ottomanPath, oldType, newType, callback) {
// For various conversions, store functions that perform the conversion on an
// identifier. This gets spliced into n1ql.
// Strings rather than functions are error messages back to the user about why that
// conversion may not make sense.
const supported = {
Date: {
string: 'Dates and strings are already interchangable',
number: 'not supported',
},
boolean: {
string: (i) => `TOSTRING(${i})`,
number: 'Converting booleans to numbers doesn\'t really make sense',
},
string: {
Date: 'Dates and strings are already interchangeable',
boolean: (i) => `TOBOOLEAN(${i})`,
number: (i) => `TONUMBER(${i})`,
},
number: {
string: (i) => `TOSTRING(${i})`,
boolean: (i) => `TOBOOLEAN(${i})`,
Date: 'not supported',
},
};
log.info('changeType', { ottomanModelName, ottomanPath, oldType, newType });
// Error conditions to check, before we issue a query that could damage data.
if (!ottomanModelName || !ottomanPath || !oldType || !newType) {
return Promise.reject('Missing required argument').nodeify(callback);
} else if (oldType === newType) {
return Promise.reject('Cannot convert from one type to itself').nodeify(callback);
} else if (!supported[oldType]) {
return Promise.reject(`Conversion of type ${oldType} is not supported`);
} else if (!supported[oldType][newType]) {
return Promise.reject(`Type ${oldType} does not have a supported conversion to ${newType}`);
}
const conversion = supported[oldType][newType];
if (typeof conversion === 'string') {
return Promise.reject(`Can't convert ${oldType} to ${newType}: ${conversion}`)
.nodeify(callback);
}
const p = n1qlEscape(ottomanPath);
const query = `UPDATE \`${singleton.active._name}\`
SET ${p} = ${conversion(p)}
WHERE \`${ottomanType}\`='${ottomanModelName}'`;
return singleton.runN1ql(query);
},
/**
* Adds an index to a collection
* @param ottomanModelName the name of the ottoman model
* @param indexName - The name of the index to add
* @param columns - The columns to add an index on
* @param callback
*/
addIndex: function (ottomanModelName, indexName, columns, callback) {
log.info('Add Index', { indexName, columns, ottomanModelName });
let srcCols = columns;
if (typeof columns === 'string') {
srcCols = [columns];
}
const cols = srcCols.map(c => n1qlEscape(c)).join(', ');
const n1ql = `
CREATE INDEX \`${indexName}\` ON \`${singleton.active._name}\` (
${cols}
)
WHERE \`${ottomanType}\`='${ottomanModelName}'`;
return singleton.runN1ql(n1ql, {}, callback);
},
/**
* Removes an index from a collection
*
* @param collectionName - The collection to remove the index
* @param indexName - The name of the index to remove
* @param columns
*/
removeIndex: function (indexName, callback) {
log.info('removeIndex', { indexName });
return singleton.runN1ql(`DROP INDEX \`${singleton.active._name}\`.\`${indexName}\``,
{}, callback);
},
/**
* Opens the specified bucket, sets it active, and returns it.
* @param bucketName the name of the bucket to use
* @returns self, for chaining
*/
withBucket: function (bucketName) {
let bucket = null;
return new Promise((resolve, reject) => {
const onBucketOpen = (err) => {
if (err) {
log.error('Failed to open bucket', { bucketName, err });
return reject(err);
}
singleton.active = bucket;
return resolve(singleton);
};
log.info('Opening active bucket', { bucketName });
bucket = (singleton.bucketPassword ?
singleton.connection.openBucket(bucketName, singleton.bucketPassword, onBucketOpen) :
singleton.connection.openBucket(bucketName, onBucketOpen));
});
},
getBucketNames: function (callback) {
log.info('getBucketNames');
return new Promise((resolve, reject) => singleton.manager.listBuckets((err, buckets) => {
if (err) { return reject(err); }
// log.info('Buckets', { buckets });
return resolve(buckets);
})).nodeify(callback);
},
/**
* Gets all the indexes for the active bucket.
*
* @param callback
*/
getIndexes: function (callback) {
log.info('getIndexes');
return singleton.runN1ql('SELECT * FROM system:indexes', {}, callback);
},
});
Promise.promisifyAll(CouchbaseDriver);
/**
* Gets a connection to couchbase
*
* @param config - The config to connect to couchbase
* @param callback - The callback to call with a CouchbaseDriver object
*/
const connect = (config, intern, callback) => {
console.log('CONNECT');
internalLogger = intern.mod ? (intern.mod.log || log) : log;
type = intern.mod.type;
// log.info('Connect', { config, intern, type });
let port;
if (config.port === undefined) {
port = DEFAULTS.port;
} else {
port = config.port;
}
/* Export functions to the interface that are not normally part
* of the migration interface.
*/
const exportable = [
'runN1ql', 'registerModel', 'getModel', 'createOttomanModel',
'renameOttomanModel', 'addOttomanPath', 'removeOttomanPath',
'renameOttomanPath', 'createBucket', 'dropBucket',
'withBucket', 'getBucketNames', 'getIndexes',
];
exportable.forEach(f => {
/* eslint no-param-reassign: "off" */
if (intern.interfaces && intern.interfaces.MigratorInterface) {
intern.interfaces.MigratorInterface[f] = function (...args) {
return Promise.reject('Not implemented')
.nodeify(args[args.length - 1]);
};
}
return f;
});
let cluster = null;
if (config.host === undefined) {
cluster = `${DEFAULTS.host}:${port}?detailed_errcodes=1`;
} else {
cluster = `${config.host}:${port}?detailed_errcodes=1`;
}
log.info('Connecting to cluster', { cluster });
const db = new couchbase.Cluster(cluster);
const driver = new CouchbaseDriver(db, intern, config);
deasync.loopWhile(() => !driver.ready);
if (config.bucket) {
return driver.withBucket(config.bucket)
.then(d => callback(null, d));
}
return callback(null, new CouchbaseDriver(db, intern, config));
};
module.exports = { connect };