added some consistency checks for links and attributes

This commit is contained in:
azivner
2018-11-15 13:58:14 +01:00
parent 346b8c21dd
commit 6749cbed1b
36 changed files with 244 additions and 47 deletions

View File

@@ -3,7 +3,7 @@
const build = require('./build');
const packageJson = require('../../package');
const APP_DB_VERSION = 115;
const APP_DB_VERSION = 116;
const SYNC_VERSION = 2;
module.exports = {

View File

@@ -219,6 +219,65 @@ async function runAllChecks() {
type == 'search'`,
"Search note has children", errorList);
await fixEmptyRelationTargets(errorList);
await runCheck(`
SELECT
attributeId
FROM
attributes
WHERE
type != 'label'
AND type != 'label-definition'
AND type != 'relation'
AND type != 'relation-definition'`,
"Attribute has invalid type", errorList);
await runCheck(`
SELECT
attributeId
FROM
attributes
LEFT JOIN notes ON attributes.noteId = notes.noteId AND notes.isDeleted = 0
WHERE
attributes.isDeleted = 0
AND notes.noteId IS NULL`,
"Attribute reference to the owning note is broken", errorList);
await runCheck(`
SELECT
attributeId
FROM
attributes
LEFT JOIN notes AS targetNote ON attributes.value = targetNote.noteId AND targetNote.isDeleted = 0
WHERE
attributes.type = 'relation'
AND attributes.isDeleted = 0
AND targetNote.noteId IS NULL`,
"Relation reference to the target note is broken", errorList);
await runCheck(`
SELECT
linkId
FROM
links
WHERE
type != 'image'
AND type != 'hyper'`,
"Link type is invalid", errorList);
await runCheck(`
SELECT
linkId
FROM
links
LEFT JOIN notes AS sourceNote ON sourceNote.noteId = links.noteId AND sourceNote.isDeleted = 0
LEFT JOIN notes AS targetNote ON targetNote.noteId = links.noteId AND targetNote.isDeleted = 0
WHERE
sourceNote.noteId IS NULL
OR targetNote.noteId IS NULL`,
"Link to source/target note link is broken", errorList);
await runSyncRowChecks("notes", "noteId", errorList);
await runSyncRowChecks("note_revisions", "noteRevisionId", errorList);
await runSyncRowChecks("branches", "branchId", errorList);
@@ -233,8 +292,6 @@ async function runAllChecks() {
await checkTreeCycles(errorList);
}
await fixEmptyRelationTargets(errorList);
return errorList;
}

View File

@@ -332,9 +332,19 @@ async function deleteNote(branch) {
await attribute.save();
}
for (const attribute of await note.getTargetRelations()) {
attribute.isDeleted = true;
await attribute.save();
for (const relation of await note.getTargetRelations()) {
relation.isDeleted = true;
await relation.save();
}
for (const link of await note.getLinks()) {
link.isDeleted = true;
await link.save();
}
for (const link of await note.getTargetLinks()) {
link.isDeleted = true;
await link.save();
}
}
}