fix: CRDT creation touches document updated timestamp (#3482)
fix: Race condition in collaboration document persistence
This commit is contained in:
@@ -54,6 +54,7 @@ export default class PersistenceExtension implements Extension {
|
|||||||
state: Buffer.from(state),
|
state: Buffer.from(state),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
silent: true,
|
||||||
hooks: false,
|
hooks: false,
|
||||||
transaction,
|
transaction,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import invariant from "invariant";
|
|||||||
import { uniq } from "lodash";
|
import { uniq } from "lodash";
|
||||||
import { Node } from "prosemirror-model";
|
import { Node } from "prosemirror-model";
|
||||||
import * as Y from "yjs";
|
import * as Y from "yjs";
|
||||||
|
import { sequelize } from "@server/database/sequelize";
|
||||||
import { schema, serializer } from "@server/editor";
|
import { schema, serializer } from "@server/editor";
|
||||||
import { Document, Event } from "@server/models";
|
import { Document, Event } from "@server/models";
|
||||||
|
|
||||||
@@ -15,7 +16,17 @@ export default async function documentCollaborativeUpdater({
|
|||||||
ydoc: Y.Doc;
|
ydoc: Y.Doc;
|
||||||
userId?: string;
|
userId?: string;
|
||||||
}) {
|
}) {
|
||||||
const document = await Document.scope("withState").findByPk(documentId);
|
return sequelize.transaction(async (transaction) => {
|
||||||
|
const document = await Document.unscoped()
|
||||||
|
.scope("withState")
|
||||||
|
.findByPk(documentId, {
|
||||||
|
transaction,
|
||||||
|
lock: {
|
||||||
|
of: Document,
|
||||||
|
level: transaction.LOCK.UPDATE,
|
||||||
|
},
|
||||||
|
paranoid: false,
|
||||||
|
});
|
||||||
invariant(document, "document not found");
|
invariant(document, "document not found");
|
||||||
|
|
||||||
const state = Y.encodeStateAsUpdate(ydoc);
|
const state = Y.encodeStateAsUpdate(ydoc);
|
||||||
@@ -34,21 +45,18 @@ export default async function documentCollaborativeUpdater({
|
|||||||
const existingIds = document.collaboratorIds;
|
const existingIds = document.collaboratorIds;
|
||||||
const collaboratorIds = uniq([...pudIds, ...existingIds]);
|
const collaboratorIds = uniq([...pudIds, ...existingIds]);
|
||||||
|
|
||||||
await Document.scope(["withDrafts", "withState"]).update(
|
await document.update(
|
||||||
{
|
{
|
||||||
text,
|
text,
|
||||||
state: Buffer.from(state),
|
state: Buffer.from(state),
|
||||||
updatedAt: isUnchanged ? document.updatedAt : new Date(),
|
|
||||||
lastModifiedById:
|
lastModifiedById:
|
||||||
isUnchanged || !userId ? document.lastModifiedById : userId,
|
isUnchanged || !userId ? document.lastModifiedById : userId,
|
||||||
collaboratorIds,
|
collaboratorIds,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
silent: true,
|
transaction,
|
||||||
|
silent: isUnchanged,
|
||||||
hooks: false,
|
hooks: false,
|
||||||
where: {
|
|
||||||
id: documentId,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -67,4 +75,5 @@ export default async function documentCollaborativeUpdater({
|
|||||||
title: document.title,
|
title: document.title,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user