-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4899 from FlowFuse/remove-notifications-delete-in…
…stances Remove notifications for deleted instances
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
forge/db/migrations/20241209-01-remove-dead-instance-notifications.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* Remove Notifications for deleted Instances | ||
*/ | ||
|
||
const deletedProjects = [] | ||
const foundProjects = [] | ||
|
||
module.exports = { | ||
/** | ||
* @param {QueryInterface} queryInterface Sequelize.QueryInterface | ||
*/ | ||
up: async (context) => { | ||
const deleteNotification = async function (id) { | ||
const deleteQuery = `DELETE FROM "Notifications" WHERE "id" = ${id};` | ||
await context.sequelize.query(deleteQuery) | ||
} | ||
|
||
const lookup = 'SELECT "id", "reference" from "Notifications" ' + | ||
'WHERE "type" = \'instance-crashed\' OR "type" = \'instance-safe-mode\';' | ||
const [notifications] = await context.sequelize.query(lookup) | ||
for (const notification of notifications) { | ||
const projectId = notification.reference.split(':')[1] | ||
if (deletedProjects.includes(projectId)) { | ||
// already found project to be gone, delete | ||
deleteNotification(notification.id) | ||
} else { | ||
if (!foundProjects.includes(projectId)) { | ||
const projectQuery = `SELECT count(*) as c from "Projects" WHERE "id" = '${projectId}' LIMIT 1;` | ||
const [[projectCount]] = await context.sequelize.query(projectQuery) | ||
const count = parseInt(projectCount.c) | ||
if (count === 0) { | ||
// delete notification | ||
deletedProjects.push(projectId) | ||
deleteNotification(notification.id) | ||
} else { | ||
foundProjects.push(projectId) | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
down: async (context) => {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters