The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/server/startup/migrations/v227.js

65 lines
1.2 KiB

import { addMigration } from '../../lib/migrations';
import { CannedResponse } from '../../../ee/app/models/server';
function migrateCannedResponses() {
CannedResponse.tryDropIndex({ shortcut: 1 });
const responses1 = Promise.await(
CannedResponse.model
.rawCollection()
.aggregate([
{ $match: { shortcut: { $exists: true } } },
{
$group: {
_id: { shortcut: '$shortcut' },
uniqueIds: { $addToSet: '$_id' },
count: { $sum: 1 },
},
},
{ $match: { count: { $gte: 2 } } },
])
.toArray(),
);
const operations = [];
for (const response of responses1) {
const {
_id: { shortcut: key },
uniqueIds: ids,
} = response;
let currentIndex = 0;
for (const id of ids) {
if (!currentIndex) {
currentIndex += 1;
continue;
}
operations.push({
updateOne: {
filter: { _id: id },
update: {
$set: {
shortcut: `${key}-${currentIndex}`,
},
},
},
});
currentIndex += 1;
}
}
if (operations.length) {
Promise.await(CannedResponse.model.rawCollection().bulkWrite(operations, { ordered: false }));
}
CannedResponse.tryEnsureIndex({ shortcut: 1 }, { unique: true });
}
addMigration({
version: 227,
up() {
migrateCannedResponses();
},
});