source: Dev/trunk/src/server/config/config-couchdb.js @ 531

Last change on this file since 531 was 525, checked in by hendrikvanantwerpen, 11 years ago
  • Allow empty subcodes.
  • Use HTTPResult exclusively on server (no more q).
  • Set readonly & disabled on ourselves as well in _ComplexValueMixin
  • Split server into several modules.
  • Check codes on the variable level, not question level.
  • We can add modules in design documents now.
File size: 2.4 KB
RevLine 
[525]1var HTTPResult = require('../util/http-result')
[475]2  , _ = require('underscore')
[525]3  , CouchDB = require('../util/couch')
[479]4  , util = require('util')
5  ;
[455]6
[487]7module.exports = function(couchServerURL, dbName, designDocs) {
[479]8    var server = new CouchDB(couchServerURL);
[464]9
[487]10    if ( !designDocs ) { throw new Error("Forgot to pass design docs to checkCouch."); }
11
[464]12    console.log("Configuring CouchDB for QED");
13    console.log("Checking CouchDB version");
[475]14    return server.get('')
15    .then(function(info){
[487]16        if (info.version !== "1.4.0" ) {
[475]17            console.log("Found "+info.version+", only tested with CouchDB 1.2.0");
[464]18        } else {
[487]19            console.log("CouchDB 1.4.0 found");
[464]20        }
21    }).then(function(res){
[519]22        console.log("Checking database '"+dbName+"'");
23        return server.get(dbName)
[475]24        .then(function(db){
[519]25            console.log("Database found.");
[464]26        },function(err){
[519]27            console.log("Creating database.");
28            return server.put(dbName);
[464]29        });
30    }).then(function(){
31        console.log("Putting documents in database.");
[475]32        return _.reduce(designDocs, function(memo, doc, docUrl) {
[479]33            return memo.then(function(){
34                var configAction = doc.__configAction || "replace";
35                delete doc.__configAction;
[519]36                docUrl = dbName+'/'+docUrl;
[479]37                switch (configAction) {
38                case "ignore":
39                    console.log(docUrl+" ignored.");
40                    return null;
41                case "update":
42                    console.log(docUrl+" updating.");
43                    return server.get(docUrl)
[475]44                    .then(function(oldDoc){
45                        _.extend(oldDoc,doc);
46                        return server.put(docUrl,oldDoc);
47                    },function(){
48                        return server.put(docUrl,doc);
49                    });
[479]50                case "replace":
51                    console.log(docUrl+" replacing.");
52                    return server.get(docUrl)
[475]53                    .then(function(oldDoc){
54                        _.extend(doc,_.pick(oldDoc,'_id','_rev'));
55                        return server.put(docUrl,doc);
56                    },function(){
57                        return server.put(docUrl,doc);
58                    });
[479]59                default:
60                    console.warn("Unknown action",configAction);
61                    return null;
62                }
63            });
[525]64        }, new HTTPResult(200));
[455]65    });
[464]66};
Note: See TracBrowser for help on using the repository browser.