hedgedoc/bin/cleanup
Sheogorath f42304c967
Clean up all foreign-key constraints
This patch cleans up the remaining possible foreign-key constraint. This
case seem to appear, when notes are deleted, but due to missing database
contraints not their authroships.

This function should clean that up as well and complete the preparation
for the new db contraints.

Signed-off-by: Sheogorath <sheogorath@shivering-isles.com>
2020-03-21 21:05:52 +01:00

100 lines
2.5 KiB
JavaScript
Executable file

#!/usr/bin/env node
const logger = require("../lib/logger");
const models = require('../lib/models')
logger.info("Cleaning up notes that should already have been removed. Sorry.")
async function cleanup() {
await models.Note.findAll({
include: [{
model: models.User,
as: 'owner',
attributes: ['id']
}],
attributes: ['id', 'ownerId']
}).then(async function(notes) {
for(let i =0, noteCount = notes.length; i< noteCount; i++) {
const item = notes[i]
if(item.ownerId != null && !item.owner) {
await models.Note.destroy({
where: {
id: item.id
}})
await models.Revision.destroy({
where: {
noteId: item.id
}
})
await models.Author.destroy({
where: {
noteId: item.id
}
})
logger.info(`Deleted note ${item.id} from user ${item.ownerId}`)
}
}
})
await models.Author.findAll({
include: [{
model: models.User,
as: 'user',
attributes: ['id']
}],
attributes: ['id', 'userId']
}).then(async function(authors) {
for(let i =0, authorCount = authors.length; i< authorCount; i++) {
const item = authors[i]
if(item.userId != null && !item.user) {
await models.Author.destroy({
where: {
id: item.id
}})
logger.info(`Deleted authorship ${item.id} from user ${item.userId}`)
}
}
})
await models.Author.findAll({
include: [{
model: models.Note,
as: 'note',
attributes: ['id']
}],
attributes: ['id', 'noteId']
}).then(async function(authors) {
for(let i =0, authorCount = authors.length; i< authorCount; i++) {
const item = authors[i]
if(item.noteId != null && !item.note) {
await models.Author.destroy({
where: {
id: item.id
}})
logger.info(`Deleted authorship ${item.id} from note ${item.noteId}`)
}
}
})
await models.Revision.findAll({
include: [{
model: models.Note,
as: 'note',
attributes: ['id']
}],
attributes: ['id', 'noteId']
}).then(async function(revisions) {
for(let i =0, revisionCount = revisions.length; i< revisionCount; i++) {
const item = revisions[i]
if(item.noteId != null && !item.note) {
await models.Revision.destroy({
where: {
id: item.id
}})
logger.info(`Deleted revision ${item.id} from note ${item.userId}`)
}
}
})
process.exit(0)
}
cleanup()