Skip to content

Commit

Permalink
Merge pull request RocketChat#321 from assistify/fix/RocketChat#288-e…
Browse files Browse the repository at this point in the history
…rroneous-subscriptions

Migration to remove obsolete subscriptions
  • Loading branch information
ThomasRoehl committed Apr 20, 2018
2 parents 2f95cf2 + f7dd9a6 commit cc7c67a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 40 deletions.
40 changes: 0 additions & 40 deletions packages/assistify-ai/server/migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,3 @@ Up to now, there's no "DB version" stored for assistify.
Until we've got expensive of contradicting migrations, we'll just use this file to write functions running
on startup which migrate data - ignoring the actual version
*/

const settingsDbsToAssistify = function() {

const renameSetting = function(oldId, newId) {
const oldSetting = RocketChat.models.Settings.findById(oldId).fetch()[0];
if (oldSetting) {
RocketChat.models.Settings.removeById(oldSetting._id);

// there has been some problem with upsert() when changing the complete doc, so decide explicitly for insert or update
let newSetting = RocketChat.models.Settings.findById(newId).fetch()[0];
if (newSetting) {
RocketChat.models.Settings.updateValueById(newId, oldSetting.value);
} else {
newSetting = oldSetting;
newSetting._id = newId;
delete newSetting.$loki;
RocketChat.models.Settings.insert(newSetting);
}
}
};

renameSetting('DBS_AI_Enabled', 'Assistify_AI_Enabled');
renameSetting('DBS_AI_Redlink_URL', 'Assistify_AI_Smarti_Base_URL');
renameSetting('DBS_AI_Redlink_Auth_Token', 'Assistify_AI_Smarti_Auth_Token');
renameSetting('DBS_AI_Redlink_Hook_Token', 'Assistify_AI_RocketChat_Webhook_Token');
renameSetting('DBS_AI_Redlink_Domain', 'Assistify_AI_Smarti_Domain');

// the next ones got new defaults, so just remove the old one
RocketChat.models.Settings.removeById('DBS_AI_Source');
RocketChat.models.Settings.removeById('reload_Assistify');

// remove obsolete settings
RocketChat.models.Settings.removeById('Assistify_AI_DBSearch_Suffix');
};

Meteor.startup(() => {

settingsDbsToAssistify();

});
3 changes: 3 additions & 0 deletions packages/assistify-help-request/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ Package.onUse(function(api) {
api.addFiles('server/methods/isValidExpertise.js', 'server');
api.addFiles('server/methods/expertiseList.js', 'server');

// we have all migrations in one single file
api.addFiles('server/migrations.js', 'server');

// Hooks
api.addFiles('server/hooks/sendMessageToKnowledgeAdapter.js', 'server');

Expand Down
31 changes: 31 additions & 0 deletions packages/assistify-help-request/server/migrations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Up to now, there's no "DB version" stored for assistify.
Until we've got expensive of contradicting migrations, we'll just use this file to write functions running
on startup which migrate data - ignoring the actual version
*/

const removeObsoleteSubscriptions = function() {
let counter = 0;
RocketChat.models.Subscriptions.model.aggregate([
{
'$lookup': {
'from': 'users',
'localField': 'u._id',
'foreignField': '_id',
'as': 'users'
}},
{ '$match': { 'users._id': { '$exists': false } } }
]).forEach(function(errSubscription) {
// we can bypass the cached collection as this will affect obsolete values
RocketChat.models.Subscriptions.model.remove({ _id: errSubscription._id.toString() });
counter++;
});
return counter;
};

Meteor.startup(() => {
const count = removeObsoleteSubscriptions();
if (count) {
console.log('Removed', count, 'erroneous subscriptions');
}
});

0 comments on commit cc7c67a

Please sign in to comment.