-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathincremental-diff-record.js
More file actions
executable file
·116 lines (93 loc) · 2.86 KB
/
incremental-diff-record.js
File metadata and controls
executable file
·116 lines (93 loc) · 2.86 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env node
var minimist = require('minimist');
var s3urls = require('s3urls');
var Dyno = require('dyno');
var crypto = require('crypto');
var AWS = require('aws-sdk');
var s3 = new AWS.S3();
var assert = require('assert');
var joinPath = require('path.join');
var args = minimist(process.argv.slice(2));
function usage() {
console.error('');
console.error('Usage: incremental-diff-record <tableinfo> <s3url> <recordkey>');
console.error(' - tableinfo: the table where the record lives, specified as `region/tablename`');
console.error(' - s3url: s3 folder where the incremental backups live');
console.error(' - recordkey: the key for the record specified as a JSON object');
}
if (args.help) {
usage();
process.exit(0);
}
var table = args._[0];
if (!table) {
console.error('Must provide table information');
usage();
process.exit(1);
}
var region = table.split('/')[0];
table = table.split('/')[1];
var s3url = args._[1];
if (!s3url) {
console.error('Must provide an s3url');
usage();
process.exit(1);
}
s3url = s3urls.fromUrl(s3url);
var key = args._[2];
if (!key) {
console.error('Must provide a record key');
usage();
process.exit(1);
}
// Sort the attributes in the provided key
key = JSON.parse(key);
key = JSON.stringify(Object.keys(key).sort().reduce(function(keyObj, attr) {
keyObj[attr] = key[attr];
return keyObj;
}, {}));
// Converts incoming strings in wire or dyno format into dyno format
try {
var obj = Dyno.deserialize(key);
for (var k in obj) if (!obj[k]) throw new Error();
key = obj;
}
catch (err) { key = JSON.parse(key); }
s3url.Key = joinPath(
s3url.Key,
table,
crypto.createHash('md5')
.update(Dyno.serialize(key))
.digest('hex')
);
var dyno = Dyno({
region: region,
table: table
});
dyno.getItem({ Key: key }, function(err, data) {
if (err) throw err;
var dynamoRecord = data.Item;
s3.getObject(s3url, function(err, data) {
if (err && err.statusCode !== 404) throw err;
var s3data = err ? undefined : Dyno.deserialize(data.Body.toString());
console.log('DynamoDB record');
console.log('--------------');
console.log(dynamoRecord);
console.log('');
console.log('Incremental backup record (%s)', s3url.Key);
console.log('--------------');
console.log(s3data);
console.log('');
try {
assert.deepEqual(s3data, dynamoRecord);
console.log('----------------------------');
console.log('✔ The records are equivalent');
console.log('----------------------------');
}
catch (err) {
console.log('--------------------------------');
console.log('✘ The records are not equivalent');
console.log('--------------------------------');
}
});
});