removed unique index again - from now on the invariant is that there's unique undeleted relationship between note and parent note

This commit is contained in:
azivner 2018-01-03 21:29:13 -05:00
parent 8d9b0db316
commit 548ecd4171
4 changed files with 68 additions and 112 deletions

View file

@ -67,9 +67,6 @@ CREATE INDEX `IDX_sync_sync_date` ON `sync` (
CREATE INDEX `IDX_notes_is_deleted` ON `notes` ( CREATE INDEX `IDX_notes_is_deleted` ON `notes` (
`is_deleted` `is_deleted`
); );
CREATE INDEX `IDX_notes_tree_note_tree_id` ON `notes_tree` (
`note_tree_id`
);
CREATE UNIQUE INDEX `IDX_notes_tree_note_id_parent_note_id` ON `notes_tree` ( CREATE UNIQUE INDEX `IDX_notes_tree_note_id_parent_note_id` ON `notes_tree` (
`note_id`, `note_id`,
`parent_note_id` `parent_note_id`

View file

@ -0,0 +1,9 @@
DROP INDEX IDX_notes_tree_note_id_parent_note_id;
CREATE INDEX `IDX_notes_tree_note_id_parent_note_id` ON `notes_tree` (
`note_id`,
`parent_note_id`
);
-- dropping this as it's just duplicate of primary key
DROP INDEX IDX_notes_tree_note_tree_id;

View file

@ -14,20 +14,8 @@ router.put('/:noteTreeId/move-to/:parentNoteId', auth.checkApiAuth, async (req,
const noteToMove = await sql.getFirst("SELECT * FROM notes_tree WHERE note_tree_id = ?", [noteTreeId]); const noteToMove = await sql.getFirst("SELECT * FROM notes_tree WHERE note_tree_id = ?", [noteTreeId]);
const existing = await getExistingNoteTree(parentNoteId, noteToMove.note_id); if (!await validateParentChild(res, parentNoteId, noteToMove.note_id, noteTreeId)) {
return;
if (existing && !existing.is_deleted && existing.note_tree_id !== noteTreeId) {
return res.send({
success: false,
message: 'This note already exists in target parent note.'
});
}
if (!await checkTreeCycle(parentNoteId, noteToMove.note_id)) {
return res.send({
success: false,
message: 'Moving note here would create cycle.'
});
} }
const maxNotePos = await sql.getFirstValue('SELECT MAX(note_position) FROM notes_tree WHERE parent_note_id = ? AND is_deleted = 0', [parentNoteId]); const maxNotePos = await sql.getFirstValue('SELECT MAX(note_position) FROM notes_tree WHERE parent_note_id = ? AND is_deleted = 0', [parentNoteId]);
@ -53,23 +41,10 @@ router.put('/:noteTreeId/move-before/:beforeNoteTreeId', auth.checkApiAuth, asyn
const noteToMove = await sql.getFirst("SELECT * FROM notes_tree WHERE note_tree_id = ?", [noteTreeId]); const noteToMove = await sql.getFirst("SELECT * FROM notes_tree WHERE note_tree_id = ?", [noteTreeId]);
const beforeNote = await sql.getFirst("SELECT * FROM notes_tree WHERE note_tree_id = ?", [beforeNoteTreeId]); const beforeNote = await sql.getFirst("SELECT * FROM notes_tree WHERE note_tree_id = ?", [beforeNoteTreeId]);
const existing = await getExistingNoteTree(beforeNote.parent_note_id, noteToMove.note_id); if (!await validateParentChild(res, beforeNote.parent_note_id, noteToMove.note_id, noteTreeId)) {
return;
if (existing && !existing.is_deleted && existing.note_tree_id !== noteTreeId) {
return res.send({
success: false,
message: 'This note already exists in target parent note.'
});
} }
if (!await checkTreeCycle(beforeNote.parent_note_id, noteToMove.note_id)) {
return res.send({
success: false,
message: 'Moving note here would create cycle.'
});
}
if (beforeNote) {
await sql.doInTransaction(async () => { await sql.doInTransaction(async () => {
// we don't change date_modified so other changes are prioritized in case of conflict // we don't change date_modified so other changes are prioritized in case of conflict
// also we would have to sync all those modified note trees otherwise hash checks would fail // also we would have to sync all those modified note trees otherwise hash checks would fail
@ -87,10 +62,6 @@ router.put('/:noteTreeId/move-before/:beforeNoteTreeId', auth.checkApiAuth, asyn
}); });
res.send({ success: true }); res.send({ success: true });
}
else {
res.status(500).send("Before note " + beforeNoteTreeId + " doesn't exist.");
}
}); });
router.put('/:noteTreeId/move-after/:afterNoteTreeId', auth.checkApiAuth, async (req, res, next) => { router.put('/:noteTreeId/move-after/:afterNoteTreeId', auth.checkApiAuth, async (req, res, next) => {
@ -101,23 +72,10 @@ router.put('/:noteTreeId/move-after/:afterNoteTreeId', auth.checkApiAuth, async
const noteToMove = await sql.getFirst("SELECT * FROM notes_tree WHERE note_tree_id = ?", [noteTreeId]); const noteToMove = await sql.getFirst("SELECT * FROM notes_tree WHERE note_tree_id = ?", [noteTreeId]);
const afterNote = await sql.getFirst("SELECT * FROM notes_tree WHERE note_tree_id = ?", [afterNoteTreeId]); const afterNote = await sql.getFirst("SELECT * FROM notes_tree WHERE note_tree_id = ?", [afterNoteTreeId]);
const existing = await getExistingNoteTree(afterNote.parent_note_id, noteToMove.note_id); if (!await validateParentChild(res, afterNote.parent_note_id, noteToMove.note_id, noteTreeId)) {
return;
if (existing && !existing.is_deleted && existing.note_tree_id !== noteTreeId) {
return res.send({
success: false,
message: 'This note already exists in target parent note.'
});
} }
if (!await checkTreeCycle(afterNote.parent_note_id, noteToMove.note_id)) {
return res.send({
success: false,
message: 'Moving note here would create cycle.'
});
}
if (afterNote) {
await sql.doInTransaction(async () => { await sql.doInTransaction(async () => {
// we don't change date_modified so other changes are prioritized in case of conflict // we don't change date_modified so other changes are prioritized in case of conflict
// also we would have to sync all those modified note trees otherwise hash checks would fail // also we would have to sync all those modified note trees otherwise hash checks would fail
@ -133,10 +91,6 @@ router.put('/:noteTreeId/move-after/:afterNoteTreeId', auth.checkApiAuth, async
}); });
res.send({ success: true }); res.send({ success: true });
}
else {
res.status(500).send("After note " + afterNoteTreeId + " doesn't exist.");
}
}); });
router.put('/:childNoteId/clone-to/:parentNoteId', auth.checkApiAuth, async (req, res, next) => { router.put('/:childNoteId/clone-to/:parentNoteId', auth.checkApiAuth, async (req, res, next) => {
@ -145,20 +99,8 @@ router.put('/:childNoteId/clone-to/:parentNoteId', auth.checkApiAuth, async (req
const prefix = req.body.prefix; const prefix = req.body.prefix;
const sourceId = req.headers.source_id; const sourceId = req.headers.source_id;
const existing = await getExistingNoteTree(parentNoteId, childNoteId); if (!await validateParentChild(res, parentNoteId, childNoteId)) {
return;
if (existing && !existing.is_deleted) {
return res.send({
success: false,
message: 'This note already exists in target parent note.'
});
}
if (!await checkTreeCycle(parentNoteId, childNoteId)) {
return res.send({
success: false,
message: 'Cloning note here would create cycle.'
});
} }
const maxNotePos = await sql.getFirstValue('SELECT MAX(note_position) FROM notes_tree WHERE parent_note_id = ? AND is_deleted = 0', [parentNoteId]); const maxNotePos = await sql.getFirstValue('SELECT MAX(note_position) FROM notes_tree WHERE parent_note_id = ? AND is_deleted = 0', [parentNoteId]);
@ -193,24 +135,8 @@ router.put('/:noteId/clone-after/:afterNoteTreeId', auth.checkApiAuth, async (re
const afterNote = await sql.getFirst("SELECT * FROM notes_tree WHERE note_tree_id = ?", [afterNoteTreeId]); const afterNote = await sql.getFirst("SELECT * FROM notes_tree WHERE note_tree_id = ?", [afterNoteTreeId]);
if (!afterNote) { if (!await validateParentChild(res, afterNote.parent_note_id, noteId)) {
return res.status(500).send("After note " + afterNoteTreeId + " doesn't exist."); return;
}
const existing = await getExistingNoteTree(afterNote.parent_note_id, noteId);
if (existing && !existing.is_deleted) {
return res.send({
success: false,
message: 'This note already exists in target parent note.'
});
}
if (!await checkTreeCycle(afterNote.parent_note_id, noteId)) {
return res.send({
success: false,
message: 'Cloning note here would create cycle.'
});
} }
await sql.doInTransaction(async () => { await sql.doInTransaction(async () => {
@ -249,8 +175,32 @@ async function loadSubTreeNoteIds(parentNoteId, subTreeNoteIds) {
} }
} }
async function validateParentChild(res, parentNoteId, childNoteId, noteTreeId = null) {
const existing = await getExistingNoteTree(parentNoteId, childNoteId);
if (existing && (noteTreeId === null || existing.note_tree_id !== noteTreeId)) {
res.send({
success: false,
message: 'This note already exists in target parent note.'
});
return false;
}
if (!await checkTreeCycle(parentNoteId, childNoteId)) {
res.send({
success: false,
message: 'Moving note here would create cycle.'
});
return false;
}
return true;
}
async function getExistingNoteTree(parentNoteId, childNoteId) { async function getExistingNoteTree(parentNoteId, childNoteId) {
return await sql.getFirst('SELECT * FROM notes_tree WHERE note_id = ? AND parent_note_id = ?', [childNoteId, parentNoteId]); return await sql.getFirst('SELECT * FROM notes_tree WHERE note_id = ? AND parent_note_id = ? AND is_deleted = 0', [childNoteId, parentNoteId]);
} }
/** /**

View file

@ -3,7 +3,7 @@
const build = require('./build'); const build = require('./build');
const packageJson = require('../package'); const packageJson = require('../package');
const APP_DB_VERSION = 61; const APP_DB_VERSION = 62;
module.exports = { module.exports = {
app_version: packageJson.version, app_version: packageJson.version,