-
Notifications
You must be signed in to change notification settings - Fork 64
/
20181907164000-endpoint-specific-logschannel.js
69 lines (67 loc) · 1.63 KB
/
20181907164000-endpoint-specific-logschannel.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
import Bluebird from 'bluebird';
import fs from 'fs';
const configJsonPath = process.env.CONFIG_MOUNT_POINT;
exports.up = function (knex) {
return new Bluebird((resolve) => {
if (!configJsonPath) {
console.log(
'Unable to locate config.json! Things may fail unexpectedly!',
);
resolve({});
return;
}
fs.readFile(configJsonPath, (err, data) => {
if (err) {
console.log(
'Failed to read config.json! Things may fail unexpectedly!',
);
resolve({});
return;
}
try {
const parsed = JSON.parse(data.toString());
resolve(parsed);
} catch {
console.log(
'Failed to parse config.json! Things may fail unexpectedly!',
);
resolve({});
}
});
})
.tap(() => {
// take the logsChannelSecret, and the apiEndpoint config field,
// and store them in a new table
return knex.schema.hasTable('logsChannelSecret').then((exists) => {
if (!exists) {
return knex.schema.createTable('logsChannelSecret', (t) => {
t.string('backend');
t.string('secret');
});
}
});
})
.then((config) => {
return knex('config')
.where({ key: 'logsChannelSecret' })
.select('value')
.then((results) => {
if (results.length === 0) {
return { config, secret: null };
}
return { config, secret: results[0].value };
});
})
.then(({ config, secret }) => {
return knex('logsChannelSecret').insert({
backend: config.apiEndpoint || '',
secret,
});
})
.then(() => {
return knex('config').where('key', 'logsChannelSecret').del();
});
};
exports.down = function () {
return Promise.reject(new Error('Not Implemented'));
};