1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

All: Fixes #593: Resource should not be auto-deleted if they've never been linked to any note

This commit is contained in:
Laurent Cozic 2018-06-17 16:59:06 +01:00
parent cf4331c5af
commit f5a72ffbaf
2 changed files with 17 additions and 1 deletions

View File

@ -96,4 +96,14 @@ describe('services_ResourceService', function() {
expect(!!(await Resource.load(resource1.id))).toBe(true);
}));
it('should not delete a resource that has never been associated with any note, because it probably means the resource came via sync, and associated note has not arrived yet', asyncTest(async () => {
const service = new ResourceService();
const resource = await shim.createResourceFromPath(__dirname + '/../tests/support/photo.jpg');
await service.indexNoteResources();
await service.deleteOrphanResources(0);
expect((await Resource.all()).length).toBe(1);
}));
});

View File

@ -1,5 +1,10 @@
const BaseModel = require('lib/BaseModel.js');
// - If is_associated = 1, note_resources indicates which note_id is currently associated with the given resource_id
// - If is_associated = 0, note_resources indicates which note_id *was* associated with the given resource_id
// - last_seen_time tells the last time that reosurce was associated with this note.
// - If last_seen_time is 0, it means the resource has never been associated with any note.
class NoteResource extends BaseModel {
static tableName() {
@ -39,7 +44,7 @@ class NoteResource extends BaseModel {
const queries = [];
for (let i = 0; i < missingResources.length; i++) {
const id = missingResources[i].id;
queries.push({ sql: 'INSERT INTO note_resources (note_id, resource_id, is_associated, last_seen_time) VALUES (?, ?, ?, ?)', params: ["", id, 0, Date.now()] });
queries.push({ sql: 'INSERT INTO note_resources (note_id, resource_id, is_associated, last_seen_time) VALUES (?, ?, ?, ?)', params: ["", id, 0, 0] });
}
await this.db().transactionExecBatch(queries);
}
@ -57,6 +62,7 @@ class NoteResource extends BaseModel {
GROUP BY resource_id
HAVING sum(is_associated) <= 0
AND last_seen_time < ?
AND last_seen_time != 0
`, [cutOffTime]);
return output.map(r => r.resource_id);
}