forked from eclipse-ditto/ditto
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
eclipse-ditto#537 added migration script
Signed-off-by: Johannes Schneider <johannes.schneider@bosch-si.com>
- Loading branch information
Johannes Schneider
committed
Nov 8, 2019
1 parent
94980c9
commit ebc3463
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
documentation/src/main/resources/files/migration_mongodb_1.0.0.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
function insertDocuments(collection, documents) { | ||
const ids = []; | ||
const bulkWrites = []; | ||
|
||
documents.forEach(document => { | ||
const {_id} = document; | ||
|
||
ids.push(_id); | ||
bulkWrites.push({ | ||
replaceOne: { | ||
filter: {_id}, | ||
replacement: document, | ||
upsert: true | ||
} | ||
}) | ||
}); | ||
|
||
if (bulkWrites.length > 0) { | ||
collection.bulkWrite(bulkWrites, {ordered: false}); | ||
} | ||
|
||
return ids; | ||
} | ||
|
||
function copyDocuments(sourceCollection, targetCollection, filter) { | ||
const sourceDocs = sourceCollection.find(filter); | ||
print(`Copy ${sourceDocs.count()} documents from ${sourceCollection} to ${targetCollection} ...`); | ||
insertDocuments(targetCollection, sourceDocs); | ||
print('Done!'); | ||
} | ||
|
||
function migratePolicies() { | ||
const db = Mongo().getDB("policies"); | ||
const targetJournal = db.getCollection('policies_journal'); | ||
db.getCollectionNames() | ||
.filter(name => name.includes('policies_journal@')) | ||
.forEach(collectionName => { | ||
const sourceJournal = db.getCollection(collectionName); | ||
copyDocuments(sourceJournal, targetJournal); | ||
}); | ||
|
||
const targetSnaps = db.getCollection('policies_snaps'); | ||
db.getCollectionNames() | ||
.filter(name => name.includes('policies_snaps@')) | ||
.forEach(collectionName => { | ||
const sourceSnaps = db.getCollection(collectionName); | ||
copyDocuments(sourceSnaps, targetSnaps); | ||
}); | ||
} | ||
|
||
function migrateThings() { | ||
const db = Mongo().getDB("things"); | ||
const targetJournal = db.getCollection('things_journal'); | ||
db.getCollectionNames() | ||
.filter(name => name.includes('things_journal@')) | ||
.forEach(collectionName => { | ||
const sourceJournal = db.getCollection(collectionName); | ||
copyDocuments(sourceJournal, targetJournal); | ||
}); | ||
|
||
const targetSnaps = db.getCollection('things_snaps'); | ||
db.getCollectionNames() | ||
.filter(name => name.includes('things_snaps@')) | ||
.forEach(collectionName => { | ||
const sourceSnaps = db.getCollection(collectionName); | ||
copyDocuments(sourceSnaps, targetSnaps); | ||
}); | ||
} | ||
|
||
migratePolicies(); | ||
migrateThings(); |