This commit is contained in:
syuilo
2017-11-07 21:04:32 +09:00
parent 307f0389ac
commit a85d5ac4bb
4 changed files with 51 additions and 5 deletions

View File

@ -0,0 +1,44 @@
// for Node.js interpret
const { default: DriveFile } = require('../../built/api/models/drive-file')
const migrate = async (doc) => {
const result = await DriveFile.update(doc._id, {
$set: {
contentType: doc.metadata.type
},
$unset: {
'metadata.type': ''
}
})
return result.ok === 1
}
async function main() {
let i = 0;
const count = await db.get('drive_files').count({});
const iterate = async () => {
if (i == count) return true;
console.log(`${i} / ${count}`);
const doc = (await db.get('drive_files').find({}, { limit: 1, skip: i }))[0]
const res = await migrate(doc);
if (!res) {
return false;
} else {
i++
return await iterate();
}
}
const res = await iterate();
if (res) {
return 'ok';
} else {
throw 'something happened';
}
}
main().then(console.dir).catch(console.error)

View File

@ -22,6 +22,7 @@ const migrateToGridFS = async (doc) => {
const buffer = doc.data ? doc.data.buffer : Buffer.from([0x00]) // アップロードのバグなのか知らないけどなぜか data が存在しない drive_file ドキュメントがまれにあることがわかったので
const created_at = doc.created_at
const name = doc.name
const type = doc.type
delete doc._id
delete doc.created_at
@ -29,9 +30,10 @@ const migrateToGridFS = async (doc) => {
delete doc.hash
delete doc.data
delete doc.name
delete doc.type
const bucket = await getGridFSBucket()
const added = await writeToGridFS(bucket, buffer, id, name, { metadata: doc })
const added = await writeToGridFS(bucket, buffer, id, name, { contentType: type, metadata: doc })
const result = await DriveFile.update(id, {
$set: {