Skip to content

Commit

Permalink
Merge pull request #4899 from FlowFuse/remove-notifications-delete-in…
Browse files Browse the repository at this point in the history
…stances

Remove notifications for deleted instances
  • Loading branch information
knolleary authored Dec 13, 2024
2 parents 6bdabe5 + 3344565 commit d636c16
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
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) => {}
}
10 changes: 10 additions & 0 deletions forge/db/models/Project.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,16 @@ module.exports = {
ProjectId: project.id
}
})
await M.Notification.destroy({
where: {
type: {
[Op.in]: ['instance-crashed', 'instance-safe-mode']
},
reference: {
[Op.in]: [`instance-crashed:${project.id}`, `instance-safe-mode:${project.id}`]
}
}
})
}
}
},
Expand Down

0 comments on commit d636c16

Please sign in to comment.