2012-09-18 23:36:20 +08:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2012-12-08 03:20:21 +08:00
|
|
|
/*
|
|
|
|
|
|
|
|
This scripts drops all extended map_style keys in redis and regenerates
|
|
|
|
the XML caches in all the base ones to target the configured mapnik_version.
|
|
|
|
|
|
|
|
Optionally (with --convert) it also re-writes the CartoCSS if needed
|
|
|
|
to target the configured mapnik_version.
|
|
|
|
|
|
|
|
It is recommended to make a backup of the redis database before using
|
|
|
|
this script.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2012-10-09 00:06:12 +08:00
|
|
|
var path = require('path');
|
2012-09-18 23:36:20 +08:00
|
|
|
|
2012-10-09 00:06:12 +08:00
|
|
|
// Reset all styles in the store
|
|
|
|
var grainstore = require('../node_modules/grainstore/lib/grainstore');
|
|
|
|
var mapnik = require('mapnik');
|
|
|
|
var redis = require('redis');
|
2012-09-18 23:36:20 +08:00
|
|
|
|
2012-10-09 00:06:12 +08:00
|
|
|
function usage(me, exitcode) {
|
2012-12-08 03:04:31 +08:00
|
|
|
console.log("Usage: " + me + " [--convert] <environment>");
|
2012-10-09 00:06:12 +08:00
|
|
|
process.exit(exitcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
var doConvert = false;
|
|
|
|
|
|
|
|
var node_path = process.argv.shift();
|
|
|
|
var script_path = process.argv.shift();
|
|
|
|
var me = path.basename(script_path);
|
2012-12-08 03:04:31 +08:00
|
|
|
var ENV;
|
2012-10-09 00:06:12 +08:00
|
|
|
var arg;
|
|
|
|
while ( arg = process.argv.shift() ) {
|
|
|
|
if ( arg == '--convert' ) {
|
|
|
|
doConvert = true;
|
2012-12-08 03:04:31 +08:00
|
|
|
}
|
|
|
|
else if ( ! ENV ) {
|
|
|
|
ENV = arg;
|
2012-10-09 00:06:12 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
usage(me, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-08 03:04:31 +08:00
|
|
|
if ( ! ENV ) usage(me, 1);
|
2012-10-09 00:06:12 +08:00
|
|
|
|
2012-12-08 03:04:31 +08:00
|
|
|
global.environment = require('../config/environments/' + ENV);
|
|
|
|
var serverOptions = require('../lib/cartodb/server_options'); // _after_ setting global.environment
|
2012-10-09 00:06:12 +08:00
|
|
|
|
2012-12-08 03:04:31 +08:00
|
|
|
var MAPNIK_VERSION = global.environment.mapnik_version || mapnik.versions.mapnik;
|
|
|
|
|
|
|
|
console.log( (doConvert ? "Converting" : "Resetting" ) + ' all styles to target ' + MAPNIK_VERSION);
|
2012-09-18 23:36:20 +08:00
|
|
|
|
|
|
|
var dbnum = 0;
|
|
|
|
|
2012-12-08 03:04:31 +08:00
|
|
|
var mml_store = new grainstore.MMLStore(serverOptions.redis, serverOptions.grainstore);
|
2012-10-09 00:06:12 +08:00
|
|
|
|
|
|
|
var failures = [];
|
|
|
|
|
2012-12-08 03:04:31 +08:00
|
|
|
var client = redis.createClient(serverOptions.redis.port, serverOptions.redis.host);
|
2012-09-18 23:36:20 +08:00
|
|
|
client.on('connect', function() {
|
|
|
|
client.select(dbnum);
|
|
|
|
client.keys('map_style|*', function(err, matches) {
|
2012-09-21 18:58:34 +08:00
|
|
|
|
|
|
|
processNext = function() {
|
2012-10-09 00:06:12 +08:00
|
|
|
if ( ! matches.length ) process.exit(failures.length);
|
2012-09-21 18:58:34 +08:00
|
|
|
var k = matches.shift();
|
2012-10-09 00:06:12 +08:00
|
|
|
|
|
|
|
if ( /map_style\|.*\|.*\|/.test(k) ) {
|
2012-11-26 19:51:55 +08:00
|
|
|
// See https://github.com/Vizzuality/Windshaft-cartodb/issues/58
|
2012-12-08 03:20:21 +08:00
|
|
|
//console.warn("Key " + k + " is EXTENDED, dropping");
|
|
|
|
client.del(k, function(err) {
|
|
|
|
if ( err ) console.warn("Error dropping key " + k);
|
|
|
|
processNext();
|
|
|
|
});
|
2012-11-26 19:51:55 +08:00
|
|
|
return;
|
2012-10-09 00:06:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var params = RegExp(/map_style\|(.*)\|(.*)/).exec(k);
|
|
|
|
var db = params[1];
|
|
|
|
var tab = params[2];
|
|
|
|
var out = 'map_style|' + db + '|' + tab + ': ';
|
|
|
|
|
|
|
|
var mml_builder = mml_store.mml_builder({dbname:db, table:tab},
|
|
|
|
function(err, payload) {
|
|
|
|
|
|
|
|
if ( err ) { console.warn(out + err.message); failures.push(k); processNext(); }
|
|
|
|
else {
|
|
|
|
mml_builder.resetStyle(function(err, data) {
|
|
|
|
if ( err ) { console.warn(out + err.message); failures.push(k); }
|
|
|
|
else console.log(out + 'OK' + ( doConvert ? ' (converted)' : '' ));
|
|
|
|
processNext();
|
|
|
|
}, doConvert);
|
|
|
|
}
|
2012-09-18 23:36:20 +08:00
|
|
|
});
|
2012-10-09 00:06:12 +08:00
|
|
|
|
|
|
|
};
|
2012-09-21 18:58:34 +08:00
|
|
|
|
|
|
|
processNext();
|
|
|
|
|
2012-09-18 23:36:20 +08:00
|
|
|
});
|
2012-10-09 00:06:12 +08:00
|
|
|
|
2012-09-18 23:36:20 +08:00
|
|
|
});
|