Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order keys for s3 #96

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,13 @@ function incrementalBackup(event, context, callback) {

changes.forEach(function(change) {
q.defer(function(next) {
var unordered = change.dynamodb.Keys;
var ordered = Object.keys(unordered).sort().reduce(function (ordered, key) {
ordered[key] = unordered[key];
return ordered
}, {});
var id = crypto.createHash('md5')
.update(JSON.stringify(change.dynamodb.Keys))
.update(JSON.stringify(ordered))
.digest('hex');

var table = change.eventSourceARN.split('/')[1];
Expand Down
2 changes: 1 addition & 1 deletion s3-backfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function backfill(config, done) {

var keys = data.Table.KeySchema.map(function(schema) {
return schema.AttributeName;
});
}).sort(); // ensure keys are sorted by name and not declaration order which DDB requires to be HASH first

var count = 0;
var starttime = Date.now();
Expand Down
79 changes: 65 additions & 14 deletions test/fixtures/events/adjust-many.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
"eventSource":"aws:dynamodb",
"dynamodb": {
"NewImage":{
"range": {
"data": {
"S": "data-1"
},
"arange": {
"N": "1"
},
"id": {
Expand All @@ -19,6 +22,9 @@
"Keys":{
"id": {
"S": "record-1"
},
"arange": {
"N": "1"
}
}
},
Expand All @@ -32,8 +38,11 @@
"eventSource":"aws:dynamodb",
"dynamodb": {
"NewImage":{
"range": {
"N": "11"
"data": {
"S": "data-2"
},
"arange": {
"N": "1"
},
"id": {
"S": "record-1"
Expand All @@ -43,7 +52,10 @@
"StreamViewType":"NEW_AND_OLD_IMAGES",
"SequenceNumber":"222",
"OldImage":{
"range": {
"data": {
"S": "data-1"
},
"arange": {
"N": "1"
},
"id": {
Expand All @@ -53,6 +65,9 @@
"Keys":{
"id": {
"S": "record-1"
},
"arange": {
"N": "1"
}
}
},
Expand All @@ -66,7 +81,10 @@
"eventSource":"aws:dynamodb",
"dynamodb": {
"NewImage":{
"range": {
"data": {
"S": "data-1"
},
"arange": {
"N": "1"
},
"id": {
Expand All @@ -79,6 +97,9 @@
"Keys":{
"id": {
"S": "record-3"
},
"arange": {
"N": "1"
}
}
},
Expand All @@ -92,7 +113,10 @@
"eventSource":"aws:dynamodb",
"dynamodb": {
"NewImage":{
"range": {
"data": {
"S": "data-1"
},
"arange": {
"N": "1"
},
"id": {
Expand All @@ -105,6 +129,9 @@
"Keys":{
"id": {
"S": "record-2"
},
"arange": {
"N": "1"
}
}
},
Expand All @@ -118,8 +145,11 @@
"eventSource":"aws:dynamodb",
"dynamodb": {
"NewImage":{
"range": {
"N": "22"
"data": {
"S": "data-2"
},
"arange": {
"N": "1"
},
"id": {
"S": "record-2"
Expand All @@ -129,7 +159,10 @@
"StreamViewType":"NEW_AND_OLD_IMAGES",
"SequenceNumber":"222",
"OldImage":{
"range": {
"data": {
"S": "data-1"
},
"arange": {
"N": "1"
},
"id": {
Expand All @@ -139,6 +172,9 @@
"Keys":{
"id": {
"S": "record-2"
},
"arange": {
"N": "1"
}
}
},
Expand All @@ -155,8 +191,11 @@
"StreamViewType":"NEW_AND_OLD_IMAGES",
"SequenceNumber":"333",
"OldImage":{
"range": {
"N": "11"
"data": {
"S": "data-2"
},
"arange": {
"N": "1"
},
"id": {
"S": "record-1"
Expand All @@ -165,6 +204,9 @@
"Keys":{
"id": {
"S": "record-1"
},
"arange": {
"N": "1"
}
}
},
Expand All @@ -178,8 +220,11 @@
"eventSource":"aws:dynamodb",
"dynamodb": {
"NewImage":{
"range": {
"N": "33"
"data": {
"S": "data-2"
},
"arange": {
"N": "1"
},
"id": {
"S": "record-3"
Expand All @@ -189,7 +234,10 @@
"StreamViewType":"NEW_AND_OLD_IMAGES",
"SequenceNumber":"222",
"OldImage":{
"range": {
"data": {
"S": "data-1"
},
"arange": {
"N": "1"
},
"id": {
Expand All @@ -199,6 +247,9 @@
"Keys":{
"id": {
"S": "record-3"
},
"arange": {
"N": "1"
}
}
},
Expand Down
5 changes: 4 additions & 1 deletion test/fixtures/events/insert-buffer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"eventSource":"aws:dynamodb",
"dynamodb": {
"NewImage":{
"range": {
"arange": {
"N": "1"
},
"id": {
Expand Down Expand Up @@ -44,6 +44,9 @@
"Keys":{
"id": {
"S": "record-1"
},
"arange": {
"N": "1"
}
}
},
Expand Down
33 changes: 27 additions & 6 deletions test/fixtures/events/insert-modify-delete.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
"eventSource":"aws:dynamodb",
"dynamodb": {
"NewImage":{
"range": {
"data": {
"S": "data-1"
},
"arange": {
"N": "1"
},
"id": {
Expand All @@ -19,6 +22,9 @@
"Keys":{
"id": {
"S": "record-1"
},
"arange": {
"N": "1"
}
}
},
Expand All @@ -32,8 +38,11 @@
"eventSource":"aws:dynamodb",
"dynamodb": {
"NewImage":{
"range": {
"N": "2"
"data": {
"S": "data-2"
},
"arange": {
"N": "1"
},
"id": {
"S": "record-1"
Expand All @@ -43,7 +52,10 @@
"StreamViewType":"NEW_AND_OLD_IMAGES",
"SequenceNumber":"222",
"OldImage":{
"range": {
"data": {
"S": "data-1"
},
"arange": {
"N": "1"
},
"id": {
Expand All @@ -53,6 +65,9 @@
"Keys":{
"id": {
"S": "record-1"
},
"arange": {
"N": "1"
}
}
},
Expand All @@ -69,8 +84,11 @@
"StreamViewType":"NEW_AND_OLD_IMAGES",
"SequenceNumber":"333",
"OldImage":{
"range": {
"N": "2"
"data": {
"S": "data-2"
},
"arange": {
"N": "1"
},
"id": {
"S": "record-1"
Expand All @@ -79,6 +97,9 @@
"Keys":{
"id": {
"S": "record-1"
},
"arange": {
"N": "1"
}
}
},
Expand Down
23 changes: 19 additions & 4 deletions test/fixtures/events/insert-modify.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
"eventSource":"aws:dynamodb",
"dynamodb": {
"NewImage":{
"range": {
"data": {
"S": "data-1"
},
"arange": {
"N": "1"
},
"id": {
Expand All @@ -19,6 +22,9 @@
"Keys":{
"id": {
"S": "record-1"
},
"arange": {
"N": "1"
}
}
},
Expand All @@ -32,8 +38,11 @@
"eventSource":"aws:dynamodb",
"dynamodb": {
"NewImage":{
"range": {
"N": "2"
"data": {
"S": "data-2"
},
"arange": {
"N": "1"
},
"id": {
"S": "record-1"
Expand All @@ -43,7 +52,10 @@
"StreamViewType":"NEW_AND_OLD_IMAGES",
"SequenceNumber":"222",
"OldImage":{
"range": {
"data": {
"S": "data-1"
},
"arange": {
"N": "1"
},
"id": {
Expand All @@ -53,6 +65,9 @@
"Keys":{
"id": {
"S": "record-1"
},
"arange": {
"N": "1"
}
}
},
Expand Down
5 changes: 4 additions & 1 deletion test/fixtures/events/insert.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"eventSource":"aws:dynamodb",
"dynamodb": {
"NewImage":{
"range": {
"arange": {
"N": "1"
},
"id": {
Expand All @@ -19,6 +19,9 @@
"Keys":{
"id": {
"S": "record-1"
},
"arange": {
"N": "1"
}
}
},
Expand Down
Loading