fix: Error in backlinks service when updating an old document (pre v1)

This commit is contained in:
Tom Moor
2020-09-13 01:03:39 -07:00
parent 450d6b7e42
commit 56551d1ab3
2 changed files with 34 additions and 0 deletions

View File

@@ -44,6 +44,14 @@ export default class Backlinks {
order: [["createdAt", "desc"]],
limit: 2,
});
// before parsing document text we must make sure it's been migrated to
// the latest version or the parser may fail on version differences
await currentRevision.migrateVersion();
if (previousRevision) {
await previousRevision.migrateVersion();
}
const previousLinkIds = previousRevision
? parseDocumentIds(previousRevision.text)
: [];

View File

@@ -32,6 +32,32 @@ describe("documents.update", () => {
expect(backlinks.length).toBe(1);
});
test("should not fail when previous revision is different document version", async () => {
const otherDocument = await buildDocument();
const document = await buildDocument({
version: null,
text: `[ ] checklist item`,
});
document.text = `[this is a link](${otherDocument.url})`;
await document.save();
await Backlinks.on({
name: "documents.update",
documentId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
actorId: document.createdById,
data: { autosave: false },
});
const backlinks = await Backlink.findAll({
where: { reverseDocumentId: document.id },
});
expect(backlinks.length).toBe(1);
});
test("should create new backlink records", async () => {
const otherDocument = await buildDocument();
const document = await buildDocument();