-
Notifications
You must be signed in to change notification settings - Fork 7
/
patches.js
166 lines (153 loc) · 6.27 KB
/
patches.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
var async = require('async');
var orm = require('orm');
var checkColumn = function(db, table, column, cb)
{
switch(db.driver_name)
{
case 'sqlite':
db.driver.execQuery("PRAGMA table_info("+table+")", function(err, res) {
if(err) {
cb(err, res);
return;
}
var exists = res.filter(function(i) { return i.name == column; })
cb(err, exists.length > 0);
});
break;
case 'postgres':
db.driver.execQuery("SELECT COUNT(*) as col_exists FROM information_schema.columns \n\
WHERE table_name=? and column_name=?", [table, column], function(err, res) {
if(err) {
cb(err, res);
return;
}
cb(err, res[0].col_exists);
});
break;
default:
throw "Unhandled db driver";
}
};
var patches = [
{ id: "2015-12-06 14:22",
patchStructure: function(db, callback)
{
console.log("Checking user.lastVisit column...");
checkColumn(db,"user", "lastVisit", function(err, exists) {
if(err) {
console.log("Errors while checking user.lastVisit column: "+err);
console.log("please add it manually");
} else {
if(!exists) {
console.log("Haven't found. Adding column");
var query = "";
try {
switch (db.driver_name)
{
case "sqlite": query = "ALTER TABLE \"user\" ADD COLUMN \"lastVisit\" DATETIME"; break;
case "postgres" : query = "ALTER TABLE \"user\" ADD COLUMN \"lastVisit\" timestamp without time zone"; break;
default: throw "Unhandled db driver";
}
db.driver.execQuery(query, function(err, res)
{
if(err) {
console.log("Errors while adding user.lastVisit column: "+err);
console.log("please add it manually");
} else {
console.log("Adding column user.lastVisit ok");
}
callback();
});
} catch (e) {
console.log("Errors while adding user.lastVisit column: ");
console.log(e);
}
} else {
console.log("Column already exists");
callback();
}
}
});
},
patchEntity: function(db, callback)
{
var _collectNewTags = function(cb) {
console.log("Initializing tags table...");
db.models.post.find(function(err, posts) {
console.log("Found " + posts.length + "posts");
var allPosts = posts.length;
var processedPosts = 0;
var lastLoggetPercents = 0;
async.eachSeries(posts, function(post, _cb) {
var percentsDone = Math.round(processedPosts / allPosts * 100);
++processedPosts;
if(percentsDone >= lastLoggetPercents + 10)
{
lastLoggetPercents = percentsDone;
console.log("Processed "+ percentsDone + "%");
}
var externTags = [];
try{
async.eachSeries(post.tags, function(tag, __cb) {
db.models.tag.exists({tag_name: tag}, function(err, exists) {
if(!exists) {
db.models.tag.create([{ tag_name: tag }], function(err, tag) {
externTags.push(tag[0]);
__cb();
});
} else {
db.models.tag.find({tag_name: tag}, function(err, tag) {
externTags.push(tag[0]);
__cb();
});
}
} );
}, function() {post.setExternTags(externTags).save(); _cb();});
} catch(e) {
_cb();
};
}, cb);
} );
};
db.models.post.sync(function(err) {
db.models.tag.sync(function(err) {
_collectNewTags(callback);
});
});
}
}
];
var currVersion;
var onePatch = function(patch, patchFunc, db, cb)
{
console.log("Found patch "+patch.id);
console.log("Version "+ currVersion + (currVersion < patch.id ? " < " : " >= ") + patch.id);
if(currVersion < patch.id)
{
console.log("Patching to "+patch.id+"...");
patchFunc(db, function() {
console.log("Patching to "+patch.id+" ok");
currVersion = patch.id;
cb();
});
} else {
cb();
}
}
exports.patch=function(db, version, cb) {
currVersion = version;
console.log("Patching db structure ...");
async.eachSeries(patches, function(patch, _cb) {
onePatch(patch, patch.patchStructure, db, _cb);
}, function(err) {
console.log("Patching structure ok");
console.log("Patching db content");
async.eachSeries(patches, function(patch, _cb) {
currVersion = version;
onePatch(patch, patch.patchEntity, db, _cb);
}, function() {
console.log("Patching ok");
cb(currVersion);
});
});
}