-
Notifications
You must be signed in to change notification settings - Fork 17
/
db.js
162 lines (136 loc) · 4.5 KB
/
db.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
"use strict";
var config = require('./config').config.db;
exports.init = function() {
// mongo abstraction layer
var MongoDB = function() {
var mongojs = require('mongojs')
this.connection = mongojs(config.auth.host + ':' + config.auth.port + '/' + config.database, config.collections);
};
// pull methods
MongoDB.prototype.findPull = function(pull_number, callback) {
this.connection.pulls.findOne({ _id: pull_number }, callback);
};
MongoDB.prototype.updatePull = function(pull_number, update_columns) {
this.connection.pulls.update({ _id: pull_number }, { $set: update_columns });
};
MongoDB.prototype.insertPull = function(pull, callback) {
this.connection.pulls.insert({
_id: pull.number,
number: pull.number,
repo: pull.repo,
created_at: pull.created_at,
updated_at: pull.updated_at,
head: pull.head.sha,
files: pull.files
}, callback);
};
MongoDB.prototype.findPullsByJobStatus = function(statuses, callback) {
this.connection.collection('pulls').find({ 'jobs.status': { $in: statuses }}).forEach(callback);
};
// job methods
MongoDB.prototype.insertJob = function(pull, job) {
if (typeof pull.jobs == 'undefined') {
pull.jobs = [];
}
pull.jobs.push(job);
this.updatePull(pull.number, { jobs: pull.jobs});
};
MongoDB.prototype.updateJobStatus = function(job_id, status) {
this.connection.pulls.update({ 'jobs.id': job_id }, { $set: { 'jobs.$.status': status }});
};
// inline status methods
MongoDB.prototype.insertLineStatus = function(pull, filename, line_number) {
this.connection.pulls.update({ _id: pull.number, 'files.filename': filename }, { $push: { 'files.$.reported': line_number } });
};
// MySQL abstraction layer
var MySQL = function() {
this.connection = require('mysql').createConnection({
host: config.auth.host,
user: config.auth.user,
password: config.auth.pass,
database: config.database
});
};
// pull methods
MySQL.prototype.findPull = function(pull_number, callback) {
this.connection.query('SELECT pulls.*, jobs.status, jobs.id as job_id, jobs.head as jobs_head FROM pulls LEFT JOIN jobs ON pulls.id = jobs.pull_number WHERE pulls.id = ?', [ pull_number ], function (err, result){
var response;
if (result.length) {
response = result[0];
response.files = JSON.parse(response.files);
response.jobs = [];
result.forEach(function(row) {
response.jobs.push({
id: row.job_id,
status: row.status,
head: row.head
});
});
}
callback(err, response);
});
};
MySQL.prototype.updatePull = function(pull_number, update_columns) {
if (update_columns.files && typeof update_columns.files !== 'string'){
update_columns.files = JSON.stringify(update_columns.files);
}
this.connection.query('UPDATE pulls SET ? WHERE id = ?', [ update_columns, pull_number ]);
};
MySQL.prototype.insertPull = function(pull, callback) {
this.connection.query('INSERT INTO pulls SET ?', {
id: pull.number,
number: pull.number,
repo: pull.repo,
created_at: pull.created_at,
updated_at: pull.updated_at,
head: pull.head.sha,
files: JSON.stringify(pull.files)
}, callback);
};
MySQL.prototype.findPullsByJobStatus = function(statuses, callback) {
this.connection.query('SELECT pulls.*, jobs.status, jobs.id as job_id, jobs.head as job_head FROM pulls LEFT JOIN jobs ON pulls.id = jobs.pull_number WHERE jobs.status IN (?) ORDER BY pulls.id ASC', [ statuses ], function (err, result){
if (!result.length){
return;
}
var new_result = [],
new_row,
cur_pull;
result.forEach(function(row) {
if(cur_pull != row.number){
if(new_row){
new_result.push(new_row);
}
new_row = row;
new_row.jobs = [];
cur_pull = row.number;
}
new_row.jobs.push({
id: row.job_id,
status: row.status,
head: row.job_head
});
});
new_result.push(new_row);
new_result.forEach(function(row){
callback(err, row);
});
});
};
// job methods
MySQL.prototype.insertJob = function(pull, job, callback) {
job.pull_number = pull.number;
this.connection.query('INSERT INTO jobs SET ?', [ job ], callback);
};
MySQL.prototype.updateJobStatus = function(job_id, status) {
this.connection.query('UPDATE jobs SET status = ? WHERE id = ?', [ status, job_id ]);
};
// inline status methods
MySQL.prototype.insertLineStatus = function(/* pull_number, filename, line_number */) {
// todo: implement
};
if (config.type === 'mongo') {
return new MongoDB();
} else if (config.type === 'mysql') {
return new MySQL();
}
};