forked from hackmdio/migration-to-0.5.0
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
156 lines (145 loc) · 5.14 KB
/
app.js
File metadata and controls
156 lines (145 loc) · 5.14 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
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
// app
// external modules
var util = require('util');
var async = require('async');
var LZString = require('lz-string');
// core
var config = require("./lib/config.js");
var logger = require("./lib/logger.js");
// new models
var models = require("./lib/models");
function showProgress(index, total, name) {
// show every 100 counts
if (index % 100 == 0) {
logger.info('migrate ' + name + ' processing: ' + index + '/' + total);
}
}
function isInKeyStr(str) {
return /^[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=]+$/.test(str);
}
function decompress(str) {
var _str = null;
try {
if (str) {
if (isInKeyStr(str)) {
_str = LZString.decompressFromBase64(str);
// replace null byte out (PostgreSQL text datatype does not accept them)
if (_str) _str = _str.replace(/\u0000/g, '');
// if decompress gets empty string and original string is not equal to compressed empty string
// it might means the string is already decoded
else if (str !== 'Q===') _str = str;
} else {
_str = str;
}
} else {
_str = null;
}
} catch (err) {
logger.error(str);
logger.error(err);
_str = str;
}
return _str;
}
var paging = 10000;
function migrateByModel(modelName, process, callback) {
var count = 0;
models[modelName].count().then(function (_count) {
logger.info('found ' + _count + ' ' + modelName.toLowerCase() + '!');
if (_count <= 0) return callback();
count = _count;
var processedCount = 0;
var successCount = 0;
function migrateInner() {
models[modelName].findAll({
order: [['createdAt', 'ASC']],
limit: paging,
offset: processedCount
}).then(function (items) {
async.eachOfSeries(items, function (item, key, _callback) {
var currentCount = processedCount + key + 1;
process(item, currentCount, function (err, result) {
if (err) {
logger.error('migrate ' + modelName.toLowerCase() + ' failed on: ' + item.id);
logger.error('migrate ' + modelName.toLowerCase() + ' failed: ' + err);
return _callback();
} else {
successCount++;
showProgress(currentCount, count, modelName.toLowerCase());
return _callback();
}
});
}, function (err) {
processedCount += items.length;
if (processedCount >= count) {
logger.info('migrate ' + modelName.toLowerCase() + ' success: ' + successCount + '/' + count);
return callback();
} else {
return migrateInner();
}
});
}).catch(function (err) {
return callback(err);
});
}
migrateInner();
}).catch(function (err) {
logger.error('count db ' + modelName.toLowerCase() + ' failed: ' + err);
return callback(err);
});
}
function migrateNotes(callback) {
logger.info('> migrate notes');
migrateByModel("Note", function (note, key, _callback) {
note.update({
title: decompress(note.title),
content: decompress(note.content),
authorship: decompress(note.authorship)
}).then(function (note) {
return _callback(null, note);
}).catch(function (err) {
return _callback(err, null);
});
}, callback);
}
function migrateRevisions(callback) {
logger.info('> migrate revisions');
migrateByModel("Revision", function (revision, key, _callback) {
revision.update({
patch: decompress(revision.patch),
lastContent: decompress(revision.lastContent),
content: decompress(revision.content),
authorship: decompress(revision.authorship)
}).then(function (revision) {
return _callback(null, revision);
}).catch(function (err) {
return _callback(err, null);
});
}, callback);
}
// sync new db models
models.sequelize.sync().then(function () {
logger.info('connect to db and sync success!');
logger.info('---start migration---');
async.series({
migrateNotes: migrateNotes,
migrateRevisions: migrateRevisions
}, function(err, results) {
if (err) {
throw err;
} else {
logger.info('---migration complete---');
process.exit(0);
}
});
}).catch(function (err) {
logger.error('migration failed: ' + util.inspect(err));
process.exit(1);
});
// log uncaught exception
process.on('uncaughtException', function (err) {
logger.error('An uncaught exception has occured.');
logger.error(err);
logger.error('Process will exit now.');
process.exit(1);
});