From 3db2f6784ddec6d68ad5f1dcbca20b18b543b92c Mon Sep 17 00:00:00 2001 From: azivner Date: Sun, 18 Nov 2018 10:20:06 +0100 Subject: [PATCH] fix DB migration and consistency issues --- db/migrations/0115__images_in_notes.sql | 6 ++++++ db/migrations/0117__fix_attributes_of_deleted_notes.sql | 1 + db/migrations/0118__fix_broken_relations.sql | 1 + src/services/app_info.js | 2 +- src/services/consistency_checks.js | 5 +++-- 5 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 db/migrations/0117__fix_attributes_of_deleted_notes.sql create mode 100644 db/migrations/0118__fix_broken_relations.sql diff --git a/db/migrations/0115__images_in_notes.sql b/db/migrations/0115__images_in_notes.sql index 9755bd880..bf1695d3b 100644 --- a/db/migrations/0115__images_in_notes.sql +++ b/db/migrations/0115__images_in_notes.sql @@ -1,3 +1,9 @@ +-- first fix deleted status of existing images +UPDATE note_images SET isDeleted = 1 WHERE noteId IN (SELECT noteId FROM notes WHERE isDeleted = 1); + +-- we don't need set data to null because table is going to be dropped anyway and we want image size into attribute +UPDATE images SET isDeleted = 1 WHERE imageId NOT IN (SELECT imageId FROM note_images WHERE isDeleted = 0); + -- allow null for note content (for deleted notes) CREATE TABLE IF NOT EXISTS "notes_mig" ( `noteId` TEXT NOT NULL, diff --git a/db/migrations/0117__fix_attributes_of_deleted_notes.sql b/db/migrations/0117__fix_attributes_of_deleted_notes.sql new file mode 100644 index 000000000..a078f711f --- /dev/null +++ b/db/migrations/0117__fix_attributes_of_deleted_notes.sql @@ -0,0 +1 @@ +UPDATE attributes SET isDeleted = 1 WHERE noteId IN (SELECT noteId FROM notes WHERE isDeleted = 1); \ No newline at end of file diff --git a/db/migrations/0118__fix_broken_relations.sql b/db/migrations/0118__fix_broken_relations.sql new file mode 100644 index 000000000..797d4ae90 --- /dev/null +++ b/db/migrations/0118__fix_broken_relations.sql @@ -0,0 +1 @@ +UPDATE attributes SET isDeleted = 1 WHERE type = 'relation' AND value NOT IN (SELECT noteId FROM notes WHERE notes.isDeleted = 0); \ No newline at end of file diff --git a/src/services/app_info.js b/src/services/app_info.js index 3b75b3d80..677a75d57 100644 --- a/src/services/app_info.js +++ b/src/services/app_info.js @@ -3,7 +3,7 @@ const build = require('./build'); const packageJson = require('../../package'); -const APP_DB_VERSION = 116; +const APP_DB_VERSION = 118; const SYNC_VERSION = 2; module.exports = { diff --git a/src/services/consistency_checks.js b/src/services/consistency_checks.js index 6e6a91344..e459b83ac 100644 --- a/src/services/consistency_checks.js +++ b/src/services/consistency_checks.js @@ -275,8 +275,9 @@ async function runAllChecks() { 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`, + links.isDeleted = 0 + AND (sourceNote.noteId IS NULL + OR targetNote.noteId IS NULL)`, "Link to source/target note link is broken", errorList); await runSyncRowChecks("notes", "noteId", errorList);