forked from share/igor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate-oplog.coffee
57 lines (45 loc) · 1.5 KB
/
migrate-oplog.coffee
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
redis = require 'redis'
async = require 'async'
backend = require './backend'
LiveDbMongo = require 'livedb-mongo'
argv = require('optimist').argv
exports = module.exports
exports.migrate = (options = {}, callback) ->
oplog = new LiveDbMongo backend.createMongo options
client = backend.createRedis options
client.keys '* ops', (err, results) ->
return callback? err if err
iterator = (k, callback) ->
client.lrange k, 0, -1, (err, ops) ->
return callback "Could not migrate #{k}: #{err}" if err
path = k.split(" ops")[0]
[cName, docName] = path.split '.'
console.log cName, docName
ops = for op, v in ops
op = JSON.parse op
op.v = v
op
async.each ops, (op, callback) ->
oplog.writeOp cName, docName, op, (err) ->
console.warn "Error writing op #{cName} #{docName}: #{err}" if err
callback()
, (err) ->
return callback err if err
# Delete the ops from redis.
if migrate.del
client.del k, callback
else
callback()
async.each results, iterator, callback
# Should the script also delete the original ops in redis when it copies them in?
#
# This defaults to not delete - livedb can deal with junk in the oplog.
migrate.del = argv.d
if require.main == module
exports.migrate null, (err) ->
if err
console.log "ERROR! NOT FINISHED!"
console.log err
else
console.log "ALL DONE"
process.exit()