mirror of
https://github.com/outline/outline.git
synced 2026-11-21 05:24:03 +03:00
* Migrate Backlink model to generic Relationship model - Create new Relationship model with type field to support different relationship types - Add database migration to create relationships table and migrate existing backlinks - Update Backlink model to delegate to Relationship model for backward compatibility - Update BacklinksProcessor to use Relationship model with backlink type - Update API routes to use new Relationship model - Update test files to use Relationship model - Maintain backward compatibility through database view and model delegation Fixes #9366 * Update migration to rename table instead of creating new one - Rename existing backlinks table to relationships instead of creating new table - Add type column with default value to existing table - Update existing rows to have type='backlink' - Avoid expensive data migration by keeping existing data in place - Maintain backward compatibility with database view - Update rollback to reverse table rename and column addition This approach is much more efficient for large datasets as it avoids copying millions of rows. * Remove unnecessary UPDATE statement from migration The UPDATE statement is not needed since defaultValue automatically applies to existing rows when adding a column with a default value. Thanks @tommoor for catching this! * Wrap up migration in transaction - Wrap all migration operations in a transaction for atomicity - Add transaction parameter to all queryInterface calls - Follow the same pattern as other migrations in the codebase - Ensures all operations succeed or fail together * Remove Backlink class entirely and use Relationship everywhere - Delete server/models/Backlink.ts - Remove Backlink export from server/models/index.ts - Remove Backlink import and association from Document model - All functionality now uses Relationship model with RelationshipType.Backlink - Maintains same API through Relationship model methods - Cleaner architecture with single relationship model * Update documents.test.ts to use RelationshipType enum instead of string - Import RelationshipType from Relationship model - Replace type: "backlink" with type: RelationshipType.Backlink - Improves type safety and consistency with enum usage * Address code review feedback - Add transaction wrapper to migration down method for safer rollback - Remove unused findByTypeForUser method from Relationship model - Method wasn't used and won't work for all relationship types (e.g., user mentions) - Clean up code structure and improve safety * Restore imports --------- Co-authored-by: codegen-sh[bot] <131295404+codegen-sh[bot]@users.noreply.github.com> Co-authored-by: Tom Moor <tom@getoutline.com>
242 lines
7.1 KiB
TypeScript
242 lines
7.1 KiB
TypeScript
import { parser } from "@server/editor";
|
|
import { Relationship } from "@server/models";
|
|
import { RelationshipType } from "@server/models/Relationship";
|
|
import { buildDocument } from "@server/test/factories";
|
|
|
|
import BacklinksProcessor from "./BacklinksProcessor";
|
|
|
|
const ip = "127.0.0.1";
|
|
|
|
describe("documents.publish", () => {
|
|
it("should create new backlink records", async () => {
|
|
const otherDocument = await buildDocument();
|
|
const document = await buildDocument({
|
|
text: `[this is a link](${otherDocument.url})`,
|
|
});
|
|
|
|
const processor = new BacklinksProcessor();
|
|
await processor.perform({
|
|
name: "documents.publish",
|
|
documentId: document.id,
|
|
collectionId: document.collectionId!,
|
|
teamId: document.teamId,
|
|
actorId: document.createdById,
|
|
data: { title: document.title },
|
|
ip,
|
|
});
|
|
const backlinks = await Relationship.findAll({
|
|
where: {
|
|
reverseDocumentId: document.id,
|
|
type: RelationshipType.Backlink,
|
|
},
|
|
});
|
|
expect(backlinks.length).toBe(1);
|
|
});
|
|
|
|
it("should not fail when linked document is destroyed", async () => {
|
|
const otherDocument = await buildDocument();
|
|
await otherDocument.destroy();
|
|
const document = await buildDocument({
|
|
version: 0,
|
|
text: `[ ] checklist item`,
|
|
});
|
|
document.content = parser
|
|
.parse(`[this is a link](${otherDocument.url})`)
|
|
?.toJSON();
|
|
await document.save();
|
|
|
|
const processor = new BacklinksProcessor();
|
|
await processor.perform({
|
|
name: "documents.publish",
|
|
documentId: document.id,
|
|
collectionId: document.collectionId!,
|
|
teamId: document.teamId,
|
|
actorId: document.createdById,
|
|
data: { title: document.title },
|
|
ip,
|
|
});
|
|
const backlinks = await Relationship.findAll({
|
|
where: {
|
|
reverseDocumentId: document.id,
|
|
type: RelationshipType.Backlink,
|
|
},
|
|
});
|
|
expect(backlinks.length).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe("documents.update", () => {
|
|
it("should not fail on a document with no previous revisions", async () => {
|
|
const otherDocument = await buildDocument();
|
|
const document = await buildDocument({
|
|
text: `[this is a link](${otherDocument.url})`,
|
|
});
|
|
|
|
const processor = new BacklinksProcessor();
|
|
await processor.perform({
|
|
name: "documents.update",
|
|
documentId: document.id,
|
|
collectionId: document.collectionId!,
|
|
teamId: document.teamId,
|
|
actorId: document.createdById,
|
|
createdAt: new Date().toISOString(),
|
|
data: { title: document.title, autosave: false, done: true },
|
|
ip,
|
|
});
|
|
const backlinks = await Relationship.findAll({
|
|
where: {
|
|
reverseDocumentId: document.id,
|
|
type: RelationshipType.Backlink,
|
|
},
|
|
});
|
|
expect(backlinks.length).toBe(1);
|
|
});
|
|
|
|
it("should not fail when previous revision is different document version", async () => {
|
|
const otherDocument = await buildDocument();
|
|
const document = await buildDocument({
|
|
version: undefined,
|
|
text: `[ ] checklist item`,
|
|
});
|
|
document.content = parser
|
|
.parse(`[this is a link](${otherDocument.url})`)
|
|
?.toJSON();
|
|
await document.save();
|
|
|
|
const processor = new BacklinksProcessor();
|
|
await processor.perform({
|
|
name: "documents.update",
|
|
documentId: document.id,
|
|
collectionId: document.collectionId!,
|
|
teamId: document.teamId,
|
|
actorId: document.createdById,
|
|
createdAt: new Date().toISOString(),
|
|
data: { title: document.title, autosave: false, done: true },
|
|
ip,
|
|
});
|
|
const backlinks = await Relationship.findAll({
|
|
where: {
|
|
reverseDocumentId: document.id,
|
|
type: RelationshipType.Backlink,
|
|
},
|
|
});
|
|
expect(backlinks.length).toBe(1);
|
|
});
|
|
|
|
it("should create new backlink records", async () => {
|
|
const otherDocument = await buildDocument();
|
|
const document = await buildDocument();
|
|
document.content = parser
|
|
.parse(`[this is a link](${otherDocument.url})`)
|
|
?.toJSON();
|
|
await document.save();
|
|
|
|
const processor = new BacklinksProcessor();
|
|
await processor.perform({
|
|
name: "documents.update",
|
|
documentId: document.id,
|
|
collectionId: document.collectionId!,
|
|
teamId: document.teamId,
|
|
actorId: document.createdById,
|
|
createdAt: new Date().toISOString(),
|
|
data: { title: document.title, autosave: false, done: true },
|
|
ip,
|
|
});
|
|
const backlinks = await Relationship.findAll({
|
|
where: {
|
|
reverseDocumentId: document.id,
|
|
type: RelationshipType.Backlink,
|
|
},
|
|
});
|
|
expect(backlinks.length).toBe(1);
|
|
});
|
|
|
|
it("should destroy removed backlink records", async () => {
|
|
const otherDocument = await buildDocument();
|
|
const yetAnotherDocument = await buildDocument();
|
|
const document = await buildDocument({
|
|
text: `[this is a link](${otherDocument.url})
|
|
|
|
[this is a another link](${yetAnotherDocument.url})`,
|
|
});
|
|
|
|
const processor = new BacklinksProcessor();
|
|
await processor.perform({
|
|
name: "documents.publish",
|
|
documentId: document.id,
|
|
collectionId: document.collectionId!,
|
|
teamId: document.teamId,
|
|
actorId: document.createdById,
|
|
data: { title: document.title },
|
|
ip,
|
|
});
|
|
document.content = parser
|
|
.parse(
|
|
`First link is gone
|
|
|
|
[this is a another link](${yetAnotherDocument.url})`
|
|
)
|
|
?.toJSON();
|
|
await document.save();
|
|
|
|
await processor.perform({
|
|
name: "documents.update",
|
|
documentId: document.id,
|
|
collectionId: document.collectionId!,
|
|
teamId: document.teamId,
|
|
actorId: document.createdById,
|
|
createdAt: new Date().toISOString(),
|
|
data: { title: document.title, autosave: false, done: true },
|
|
ip,
|
|
});
|
|
const backlinks = await Relationship.findAll({
|
|
where: {
|
|
reverseDocumentId: document.id,
|
|
type: RelationshipType.Backlink,
|
|
},
|
|
});
|
|
expect(backlinks.length).toBe(1);
|
|
expect(backlinks[0].documentId).toBe(yetAnotherDocument.id);
|
|
});
|
|
});
|
|
|
|
describe("documents.delete", () => {
|
|
it("should destroy related backlinks", async () => {
|
|
const otherDocument = await buildDocument();
|
|
const document = await buildDocument();
|
|
document.content = parser
|
|
.parse(`[this is a link](${otherDocument.url})`)
|
|
?.toJSON();
|
|
await document.save();
|
|
|
|
const processor = new BacklinksProcessor();
|
|
await processor.perform({
|
|
name: "documents.update",
|
|
documentId: document.id,
|
|
collectionId: document.collectionId!,
|
|
teamId: document.teamId,
|
|
actorId: document.createdById,
|
|
createdAt: new Date().toISOString(),
|
|
data: { title: document.title, autosave: false, done: true },
|
|
ip,
|
|
});
|
|
|
|
await processor.perform({
|
|
name: "documents.delete",
|
|
documentId: document.id,
|
|
collectionId: document.collectionId!,
|
|
teamId: document.teamId,
|
|
actorId: document.createdById,
|
|
data: { title: document.title },
|
|
ip,
|
|
});
|
|
const backlinks = await Relationship.findAll({
|
|
where: {
|
|
reverseDocumentId: document.id,
|
|
type: RelationshipType.Backlink,
|
|
},
|
|
});
|
|
expect(backlinks.length).toBe(0);
|
|
});
|
|
});
|