forked from chrisleekr/binance-trading-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1629606507543-migrate-orders.js
104 lines (86 loc) · 2.6 KB
/
1629606507543-migrate-orders.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
/* eslint-disable no-await-in-loop */
/* eslint-disable guard-for-in */
/* eslint-disable no-restricted-syntax */
const _ = require('lodash');
const path = require('path');
const { logger: rootLogger, mongo, cache } = require('../app/helpers');
module.exports.up = async () => {
const logger = rootLogger.child({
gitHash: process.env.GIT_HASH || 'unspecified',
migration: path.basename(__filename)
});
const database = await mongo.connect(logger);
logger.info('Start migration');
let keys;
let result;
// Create index
const gridTradeOrderscollection = database.collection(
'trailing-trade-grid-trade-orders'
);
try {
result = await gridTradeOrderscollection.dropIndex(
`trailing-trade-grid-trade-orders-key-idx`
);
} catch (e) {
logger.warn(
`Index 'trailing-trade-grid-trade-orders-key-idx' is not found. It's ok.`
);
}
result = await gridTradeOrderscollection.createIndex(
{ key: 1 },
{ name: `trailing-trade-grid-trade-orders-key-idx` }
);
logger.info({ result }, 'Created index for orders');
// Migreate cache to mongo
keys = await cache.keys('*grid-trade-last*');
if (keys && keys.length > 0) {
for (const key of keys) {
// Get cache
const order = JSON.parse(await cache.get(key)) || {};
await mongo.insertOne(logger, 'trailing-trade-grid-trade-orders', {
key,
order
});
await cache.del(key);
}
}
// Create index
const manualOrdersCollection = database.collection(
'trailing-trade-manual-orders'
);
try {
result = await manualOrdersCollection.dropIndex(
`trailing-trade-manual-orders-key-idx`
);
} catch (e) {
logger.warn(
`Index 'trailing-trade-manual-orders-key-idx' is not found. It's ok.`
);
}
result = await manualOrdersCollection.createIndex(
{ symbol: 1, orderId: 1 },
{ name: `trailing-trade-manual-orders-key-idx` }
);
logger.info({ result }, 'Created index for manual orders');
keys = await cache.keys('trailing-trade-manual-order*');
if (keys && keys.length > 0) {
for (const key of keys) {
const manualOrders = await cache.hgetall(key);
if (_.isEmpty(manualOrders) === false) {
for (const rawOrder of Object.values(manualOrders)) {
const order = JSON.parse(rawOrder);
await mongo.insertOne(logger, 'trailing-trade-manual-orders', {
symbol: order.symbol,
orderId: order.orderId,
order
});
}
}
await cache.del(key);
}
}
logger.info({ result }, 'Finish migration');
};
module.exports.down = next => {
next();
};