From a5bd4c7b6a999fbee880be024331667f70adefa5 Mon Sep 17 00:00:00 2001 From: azivner Date: Sun, 22 Oct 2017 22:56:42 -0400 Subject: [PATCH] added date_modified to notes_tree --- bin/www | 4 +++ .../0004__add_date_modified_to_notes_tree.sql | 1 + routes/api/notes.js | 7 +++-- routes/api/notes_move.js | 31 +++++++++++++------ services/migration.js | 2 +- 5 files changed, 32 insertions(+), 13 deletions(-) create mode 100644 migrations/0004__add_date_modified_to_notes_tree.sql diff --git a/bin/www b/bin/www index 03df92945..af7a8c3fa 100755 --- a/bin/www +++ b/bin/www @@ -1,5 +1,9 @@ #!/usr/bin/env node +process.on('unhandledRejection', (reason, p) => { + // this makes sure that stacktrace of failed promise is printed out + console.log('Unhandled Rejection at: Promise', p, 'reason:', reason); +}); var app = require('../app'); var debug = require('debug')('node:server'); diff --git a/migrations/0004__add_date_modified_to_notes_tree.sql b/migrations/0004__add_date_modified_to_notes_tree.sql new file mode 100644 index 000000000..c914bd1c7 --- /dev/null +++ b/migrations/0004__add_date_modified_to_notes_tree.sql @@ -0,0 +1 @@ +ALTER TABLE notes_tree ADD COLUMN date_modified INTEGER NOT NULL DEFAULT 0 \ No newline at end of file diff --git a/routes/api/notes.js b/routes/api/notes.js index dcd828c09..f32ffd91c 100644 --- a/routes/api/notes.js +++ b/routes/api/notes.js @@ -153,7 +153,9 @@ router.post('/:parentNoteId/children', async (req, res, next) => { newNotePos = afterNote['note_pos'] + 1; - await sql.execute('update notes_tree set note_pos = note_pos + 1 where note_pid = ? and note_pos > ?', [parentNoteId, afterNote['note_pos']]); + const now = utils.nowTimestamp(); + + await sql.execute('update notes_tree set note_pos = note_pos + 1, date_modified = ? where note_pid = ? and note_pos > ?', [now, parentNoteId, afterNote['note_pos']]); } else { throw new ('Unknown target: ' + note['target']); @@ -179,7 +181,8 @@ router.post('/:parentNoteId/children', async (req, res, next) => { 'note_id': noteId, 'note_pid': parentNoteId, 'note_pos': newNotePos, - 'is_expanded': 0 + 'is_expanded': 0, + 'date_modified': utils.nowTimestamp() }); await sql.commit(); diff --git a/routes/api/notes_move.js b/routes/api/notes_move.js index bf540a12f..6b68552b3 100644 --- a/routes/api/notes_move.js +++ b/routes/api/notes_move.js @@ -20,9 +20,12 @@ router.put('/:noteId/moveTo/:parentId', auth.checkApiAuth, async (req, res, next else newNotePos = maxNotePos + 1; + const now = utils.nowTimestamp(); + await sql.beginTransaction(); - await sql.execute("update notes_tree set note_pid = ?, note_pos = ? where note_id = ?", [parentId, newNotePos, noteId]); + await sql.execute("update notes_tree set note_pid = ?, note_pos = ?, date_modified = ? where note_id = ?", + [parentId, newNotePos, now, noteId]); await sql.addAudit(audit_category.CHANGE_PARENT, req, noteId); @@ -37,12 +40,15 @@ router.put('/:noteId/moveBefore/:beforeNoteId', async (req, res, next) => { const beforeNote = await sql.getSingleResult("select * from notes_tree where note_id = ?", [beforeNoteId]); - if (beforeNote !== null) { + if (beforeNote) { + const now = utils.nowTimestamp(); + await sql.beginTransaction(); - await sql.execute("update notes_tree set note_pos = note_pos + 1 where note_id = ?", [beforeNoteId]); + await sql.execute("update notes_tree set note_pos = note_pos + 1, date_modified = ? where note_id = ?", [now, beforeNoteId]); - await sql.execute("update notes_tree set note_pid = ?, note_pos = ? where note_id = ?", [beforeNote['note_pid'], beforeNote['note_pos'], noteId]); + await sql.execute("update notes_tree set note_pid = ?, note_pos = ?, date_modified = ? where note_id = ?", + [beforeNote['note_pid'], beforeNote['note_pos'], now, noteId]); await sql.addAudit(audit_category.CHANGE_POSITION, req, noteId); @@ -58,12 +64,16 @@ router.put('/:noteId/moveAfter/:afterNoteId', async (req, res, next) => { const afterNote = await sql.getSingleResult("select * from notes_tree where note_id = ?", [afterNoteId]); - if (afterNote !== null) { + if (afterNote) { + const now = utils.nowTimestamp(); + await sql.beginTransaction(); - await sql.execute("update notes_tree set note_pos = note_pos + 1 where note_pid = ? and note_pos > ?", [afterNote['note_pid'], afterNote['note_pos']]); + await sql.execute("update notes_tree set note_pos = note_pos + 1, date_modified = ? where note_pid = ? and note_pos > ?", + [now, afterNote['note_pid'], afterNote['note_pos']]); - await sql.execute("update notes_tree set note_pid = ?, note_pos = ? where note_id = ?", [afterNote['note_pid'], afterNote['note_pos'] + 1, noteId]); + await sql.execute("update notes_tree set note_pid = ?, note_pos = ?, date_modified = ? where note_id = ?", + [afterNote['note_pid'], afterNote['note_pos'] + 1, now, noteId]); await sql.addAudit(audit_category.CHANGE_POSITION, req, noteId); @@ -74,10 +84,11 @@ router.put('/:noteId/moveAfter/:afterNoteId', async (req, res, next) => { }); router.put('/:noteId/expanded/:expanded', async (req, res, next) => { - let noteId = req.params.noteId; - let expanded = req.params.expanded; + const noteId = req.params.noteId; + const expanded = req.params.expanded; + const now = utils.nowTimestamp(); - await sql.execute("update notes_tree set is_expanded = ? where note_id = ?", [expanded, noteId]); + await sql.execute("update notes_tree set is_expanded = ?, date_modified = ? where note_id = ?", [expanded, now, noteId]); // no audit here, not really important diff --git a/services/migration.js b/services/migration.js index 986d5313a..70c8a9f64 100644 --- a/services/migration.js +++ b/services/migration.js @@ -2,7 +2,7 @@ const backup = require('./backup'); const sql = require('./sql'); const fs = require('fs-extra'); -const APP_DB_VERSION = 3; +const APP_DB_VERSION = 4; const MIGRATIONS_DIR = "./migrations"; async function migrate() {