fixup: migration

This commit is contained in:
David Arnold 2020-10-21 09:39:00 -05:00 committed by Denis Perov
parent 2288c8ac15
commit 06b812b590

View file

@ -24,6 +24,8 @@ import Triggers from '../models/triggers';
import UnsavedEdits from '../models/unsavedEdits'; import UnsavedEdits from '../models/unsavedEdits';
import Users from '../models/users'; import Users from '../models/users';
const fs = require('fs');
// Anytime you change the schema of one of the collection in a non-backward // Anytime you change the schema of one of the collection in a non-backward
// compatible way you have to write a migration in this file using the following // compatible way you have to write a migration in this file using the following
// API: // API:
@ -1134,7 +1136,9 @@ Migrations.add('migrate-attachments-collectionFS-to-ostrioFiles', () => {
// This directory must be writable on server, so a test run first // This directory must be writable on server, so a test run first
// We are going to copy the files locally, then move them to S3 // We are going to copy the files locally, then move them to S3
const fileName = `./assets/app/uploads/attachments/${fileObj.name()}`; const fileName = `./assets/app/uploads/attachments/${
fileObj._id
}-${fileObj.name()}`;
const newFileName = fileObj.name(); const newFileName = fileObj.name();
// This is "example" variable, change it to the userId that you might be using. // This is "example" variable, change it to the userId that you might be using.
@ -1199,7 +1203,9 @@ Migrations.add('migrate-avatars-collectionFS-to-ostrioFiles', () => {
// This directory must be writable on server, so a test run first // This directory must be writable on server, so a test run first
// We are going to copy the files locally, then move them to S3 // We are going to copy the files locally, then move them to S3
const fileName = `./assets/app/uploads/avatars/${fileObj.name()}`; const fileName = `./assets/app/uploads/avatars/${
fileObj._id
}-${fileObj.name()}`;
const newFileName = fileObj.name(); const newFileName = fileObj.name();
// This is "example" variable, change it to the userId that you might be using. // This is "example" variable, change it to the userId that you might be using.
@ -1240,14 +1246,30 @@ Migrations.add('migrate-avatars-collectionFS-to-ostrioFiles', () => {
if (err) { if (err) {
console.log(err); console.log(err);
} else { } else {
console.log('File Inserted: ', fileRef._id); console.log('File Inserted: ', newFileName, fileRef._id);
// Set the userId again // Set the userId again
Avatars.update({ _id: fileRef._id }, { $set: { userId } }); Avatars.update({ _id: fileRef._id }, { $set: { userId } });
Users.find().forEach(user => {
const old_url = fileObj.url();
new_url = Avatars.findOne({ _id: fileRef._id }).link(
'original',
'/',
);
if (user.profile.avatarUrl.startsWith(old_url)) {
// Set avatar url to new url
Users.direct.update(
{ _id: user._id },
{ $set: { 'profile.avatarUrl': new_url } },
noValidate,
);
console.log('User avatar updated: ', user._id, new_url);
}
});
fileObj.remove(); fileObj.remove();
} }
}, },
true, true, // proceedAfterUpload
); // proceedAfterUpload );
}); });
readStream.on('error', error => { readStream.on('error', error => {