adapt-authoring-content 3.0.4 → 3.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/migrations/3.0.5.js +40 -0
- package/package.json +1 -1
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { ObjectId } from 'mongodb'
|
|
2
|
+
|
|
3
|
+
export default function (migration) {
|
|
4
|
+
migration.describe('Normalise content._shareWithUsers entries to ObjectId form')
|
|
5
|
+
migration.runCommand(normaliseShareWithUsers)
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
async function normaliseShareWithUsers (db, log) {
|
|
9
|
+
const content = db.collection('content')
|
|
10
|
+
const docs = await content
|
|
11
|
+
.find({ _shareWithUsers: { $elemMatch: { $type: 'string' } } }, { projection: { _id: 1, _shareWithUsers: 1 } })
|
|
12
|
+
.toArray()
|
|
13
|
+
if (!docs.length) {
|
|
14
|
+
log('info', 'migrations', 'No content documents with string _shareWithUsers entries, skipping')
|
|
15
|
+
return
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const ops = []
|
|
19
|
+
let unconvertible = 0
|
|
20
|
+
for (const doc of docs) {
|
|
21
|
+
const next = []
|
|
22
|
+
for (const u of doc._shareWithUsers) {
|
|
23
|
+
if (u instanceof ObjectId) {
|
|
24
|
+
next.push(u)
|
|
25
|
+
} else if (typeof u === 'string' && ObjectId.isValid(u)) {
|
|
26
|
+
next.push(new ObjectId(u))
|
|
27
|
+
} else {
|
|
28
|
+
next.push(u)
|
|
29
|
+
unconvertible++
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
ops.push({ updateOne: { filter: { _id: doc._id }, update: { $set: { _shareWithUsers: next } } } })
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
for (let i = 0; i < ops.length; i += 500) {
|
|
36
|
+
await content.bulkWrite(ops.slice(i, i + 500), { ordered: false })
|
|
37
|
+
}
|
|
38
|
+
log('info', 'migrations', `normalised _shareWithUsers on ${docs.length} content document(s)`)
|
|
39
|
+
if (unconvertible) log('warn', 'migrations', `${unconvertible} _shareWithUsers entr(ies) could not be coerced to ObjectId and were left as-is`)
|
|
40
|
+
}
|