add db init method, prepare for db testing

This commit is contained in:
oleg 2015-06-29 20:42:57 +03:00
parent f38275a0f9
commit cec79bdc75
6 changed files with 80 additions and 63 deletions

8
app.js
View File

@ -1,6 +1,7 @@
'use strict';
var http = require('http'),
var db = require('./db'),
http = require('http'),
nodeStatic = require('node-static'),
jade = require('jade'),
path = require('path'),
@ -72,6 +73,11 @@ Steppy(
console.log('Server config:', JSON.stringify(app.config, null, 4));
notifier.init(app.config.notify, this.slot());
db.init('path/to/db/ignored/for/memdown', {
db: require('memdown'),
valueEncoding: 'json'
}, this.slot());
},
function() {
// load all projects for the first time

120
db.js
View File

@ -2,72 +2,74 @@
var Steppy = require('twostep').Steppy,
_ = require('underscore'),
nlevel = require('nlevel'),
ldb = nlevel.db('path/to/db/ignored/for/memdown', {
db: require('memdown'),
valueEncoding: 'json'
nlevel = require('nlevel');
exports.init = function(dbPath, params, callback) {
var ldb = nlevel.db(dbPath, params, callback);
exports.builds = new nlevel.DocsSection(ldb, 'builds', {
projections: [
{key: {createDate: 1}, value: pickId},
{key: {descCreateDate: descCreateDate, id: 1}},
{key: {
projectName: pickProjectName,
descCreateDate: descCreateDate,
id: 1
}},
// note that's unordered projection (coz number is numeric)
{key: {
projectName: pickProjectName,
number: 1,
id: 1
}}
]
});
exports.builds = new nlevel.DocsSection(ldb, 'builds', {
projections: [
{key: {createDate: 1}, value: pickId},
{key: {descCreateDate: descCreateDate, id: 1}},
{key: {
projectName: pickProjectName,
descCreateDate: descCreateDate,
id: 1
}},
// note that's unordered projection (coz number is numeric)
{key: {
projectName: pickProjectName,
number: 1,
id: 1
}}
]
});
exports.builds._beforePut = function(builds, callback) {
var self = this,
build;
Steppy(
function() {
if (builds.length > 1) {
throw new Error('Build put hooks work only with single build');
}
build = builds[0];
// generate number for build
if (!build.number && build.status === 'in-progress') {
// find last build with number in the same project
self.find({
start: {projectName: build.project.name, descCreateDate: ''},
filter: function(build) {
return 'number' in build;
},
limit: 1
}, this.slot());
} else {
this.pass([]);
}
generateIds(self, builds, this.slot());
},
function(err, prevBuilds) {
var prevBuild = prevBuilds[0];
if (!build.number && build.status === 'in-progress') {
build.number = prevBuild ? prevBuild.number + 1 : 1;
}
this.pass(null);
},
callback
);
};
};
function pickProjectName(build) {
return build.project.name;
}
exports.builds._beforePut = function(builds, callback) {
var self = this,
build;
Steppy(
function() {
if (builds.length > 1) {
throw new Error('Build put hooks work only with single build');
}
build = builds[0];
// generate number for build
if (!build.number && build.status === 'in-progress') {
// find last build with number in the same project
self.find({
start: {projectName: build.project.name, descCreateDate: ''},
filter: function(build) {
return 'number' in build;
},
limit: 1
}, this.slot());
} else {
this.pass([]);
}
generateIds(self, builds, this.slot());
},
function(err, prevBuilds) {
var prevBuild = prevBuilds[0];
if (!build.number && build.status === 'in-progress') {
build.number = prevBuild ? prevBuild.number + 1 : 1;
}
this.pass(null);
},
callback
);
};
function generateIds(section, docs, callback) {
Steppy(

View File

@ -43,9 +43,9 @@ if this.state.build
| Error:
span= this.state.build.error.message
if this.state.build.error.stderr
div stderr:
pre= this.state.build.error.stderr
if this.state.build.error.stderr
div stderr:
pre= this.state.build.error.stderr
.row
.col-md-8

5
test/db/concurrent.js Normal file
View File

@ -0,0 +1,5 @@
'use strict';
describe('Db concurrency', function() {
});

3
test/db/index.js Normal file
View File

@ -0,0 +1,3 @@
'use strict';
require('./concurrent');

View File

@ -1,6 +1,7 @@
'use strict';
require('./commands');
require('./db');
require('./scm');
require('./executor');
require('./node');