-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataMigration.js
More file actions
61 lines (48 loc) · 1.36 KB
/
dataMigration.js
File metadata and controls
61 lines (48 loc) · 1.36 KB
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
'use strict';
let fs = require('fs');
let mongo = require('./mongo');
let compareVersion = (a, b) => {
a = a.split('.');
b = b.split('.');
for (let i = 0; i < Math.max(a.length, b.length); i++) {
let diff = Number(a[i]) - Number(b[i]);
if (diff) return diff;
}
return 0;
}
let migration = async () => {
await mongo.prepare();
let current_version;
try {
current_version = fs.readFileSync('./current_version').toString();
} catch (err) {
current_version = require('./package.json').version || '0.0.0';
}
let todoList = scriptList.filter(script => compareVersion(script.version, current_version) > 0);
for (let todoItem of todoList) {
await todoItem.script();
console.log(`脚本: ${todoItem.description} 已执行完毕.`)
}
if (todoList.length) {
console.log('所有脚本都已执行完毕.')
}
process.exit();
}
migration();
//#region 待执行的叫本列表
let damage_user_id_to_number = async () => {
let damageList = await mongo.Damage.find({}).toArray();
for (let damage of damageList) {
if (typeof (damage.user_id) === 'string') {
await mongo.Damage.updateOne({ _id: damage._id }, { $set: { user_id: Number(damage.user_id) } });
}
}
}
//#endregion
let scriptList = [
{
version: '1.1.0',
description: 'damage 表 user_id 格式错误',
script: damage_user_id_to_number,
}
]