From 4001e756cbe41384507115cdcae9bea0d2f13ae7 Mon Sep 17 00:00:00 2001 From: ansuz Date: Thu, 15 Sep 2016 12:15:27 +0200 Subject: [PATCH] add import script so admins can migrate lvl to files --- import | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100755 import diff --git a/import b/import new file mode 100755 index 000000000..1bf1d5de9 --- /dev/null +++ b/import @@ -0,0 +1,65 @@ +#!/usr/bin/env node +/* globals process */ + +var Config = require("./config"); +var Fs = require("fs"); +var Storage = require(Config.storage); + +var args = process.argv.slice(2); + +if (!args.length) { + console.log("Insufficient arguments!"); + console.log("Pass a path to a database backup!"); + process.exit(); +} + +var dump = Fs.readFileSync(args[0], 'utf-8'); + +var ready = function (store) { + var lock = 0; + dump.split(/\n/) + .filter(function (line) { + return line; + }) + .forEach(function (line, i) { + lock++; + var parts; + + var channel; + var msg; + + line.replace(/^(.*?)\|(.*)$/, function (all, c, m) { + channel = c; + msg = m; + return ''; + }); + + if (!channel || !msg) { + console.log("BAD LINE on line %s", i); + return; + } + + try { + JSON.parse(msg); + } catch (err) { + console.log("BAD LINE on line %s", i); + console.log(msg); + console.log(); + } + + store.message(channel, msg, function () { + console.log(line); + lock--; + if (!lock) { + console.log("DONE"); + process.exit(0); + } + }); + }); +}; + +Storage.create(Config, function (store) { + console.log("READY"); + ready(store); +}); +