chore: documentStructure database locking (#3254)

This commit is contained in:
Tom Moor
2022-03-18 08:59:11 -07:00
committed by GitHub
parent c98c397fa8
commit 5e655e42f6
7 changed files with 566 additions and 433 deletions

View File

@@ -1,6 +1,6 @@
import invariant from "invariant";
import { Transaction } from "sequelize";
import { sequelize } from "@server/database/sequelize";
import { ValidationError } from "@server/errors";
import { APM } from "@server/logging/tracing";
import { User, Document, Collection, Pin, Event } from "@server/models";
import pinDestroyer from "./pinDestroyer";
@@ -12,6 +12,7 @@ type Props = {
parentDocumentId?: string | null;
index?: number;
ip: string;
transaction?: Transaction;
};
type Result = {
@@ -28,8 +29,8 @@ async function documentMover({
// convert undefined to null so parentId comparison treats them as equal
index,
ip,
transaction,
}: Props): Promise<Result> {
let transaction: Transaction | undefined;
const collectionChanged = collectionId !== document.collectionId;
const previousCollectionId = document.collectionId;
const result: Result = {
@@ -38,154 +39,165 @@ async function documentMover({
collectionChanged,
};
if (document.template) {
if (!collectionChanged) {
return result;
}
if (document.template && !collectionChanged) {
return result;
}
if (document.template) {
document.collectionId = collectionId;
document.parentDocumentId = null;
document.lastModifiedById = user.id;
document.updatedBy = user;
await document.save();
await document.save({ transaction });
result.documents.push(document);
} else {
try {
transaction = await sequelize.transaction();
// Load the current and the next collection upfront and lock them
const collection = await Collection.findByPk(document.collectionId, {
transaction,
lock: Transaction.LOCK.UPDATE,
paranoid: false,
});
// remove from original collection
const collection = await Collection.findByPk(document.collectionId, {
let newCollection = collectionChanged
? await Collection.findByPk(collectionId, {
transaction,
lock: Transaction.LOCK.UPDATE,
})
: collection;
invariant(newCollection, "collection should exist");
// Remove the document from the current collection
const response = await collection?.removeDocumentInStructure(document, {
transaction,
});
const documentJson = response?.[0];
const fromIndex = response?.[1] || 0;
if (!documentJson) {
throw ValidationError("The document was not found in the collection");
}
// if we're reordering from within the same parent
// the original and destination collection are the same,
// so when the initial item is removed above, the list will reduce by 1.
// We need to compensate for this when reordering
const toIndex =
index !== undefined &&
document.parentDocumentId === parentDocumentId &&
document.collectionId === collectionId &&
fromIndex < index
? index - 1
: index;
// Update the properties on the document record
document.collectionId = collectionId;
document.parentDocumentId = parentDocumentId;
document.lastModifiedById = user.id;
document.updatedBy = user;
// Add the document and it's tree to the new collection
await newCollection.addDocumentToStructure(document, toIndex, {
documentJson,
transaction,
});
if (collection) {
result.collections.push(collection);
}
// If the collection has changed then we also need to update the properties
// on all of the documents children to reflect the new collectionId
if (collectionChanged) {
// Reload the collection to get relationship data
newCollection = await Collection.scope({
method: ["withMembership", user.id],
}).findByPk(collectionId, {
transaction,
});
invariant(newCollection, "collection should exist");
result.collections.push(newCollection);
// Efficiently find the ID's of all the documents that are children of
// the moved document and update in one query
const childDocumentIds = await document.getChildDocumentIds();
await Document.update(
{
collectionId: newCollection.id,
},
{
transaction,
where: {
id: childDocumentIds,
},
}
);
// We must reload from the database to get the relationship data
const documents = await Document.findAll({
where: {
id: childDocumentIds,
},
transaction,
paranoid: false,
});
const response = await collection?.removeDocumentInStructure(document, {
save: false,
document.collection = newCollection;
result.documents.push(
...documents.map((document) => {
if (newCollection) {
document.collection = newCollection;
}
return document;
})
);
// If the document was pinned to the collection then we also need to
// automatically remove the pin to prevent a confusing situation where
// a document is pinned from another collection. Use the command to ensure
// the correct events are emitted.
const pin = await Pin.findOne({
where: {
documentId: document.id,
collectionId: previousCollectionId,
},
transaction,
lock: Transaction.LOCK.UPDATE,
});
const documentJson = response?.[0];
const fromIndex = response?.[1] || 0;
// if we're reordering from within the same parent
// the original and destination collection are the same,
// so when the initial item is removed above, the list will reduce by 1.
// We need to compensate for this when reordering
const toIndex =
index !== undefined &&
document.parentDocumentId === parentDocumentId &&
document.collectionId === collectionId &&
fromIndex < index
? index - 1
: index;
// if the collection is the same then it will get saved below, this
// line prevents a pointless intermediate save from occurring.
if (collectionChanged) {
await collection?.save({
if (pin) {
await pinDestroyer({
user,
pin,
ip,
transaction,
});
}
// add to new collection (may be the same)
document.collectionId = collectionId;
document.parentDocumentId = parentDocumentId;
document.lastModifiedById = user.id;
document.updatedBy = user;
const newCollection = collectionChanged
? await Collection.scope({
method: ["withMembership", user.id],
}).findByPk(collectionId, {
transaction,
})
: collection;
invariant(newCollection, "collection should exist");
await newCollection?.addDocumentToStructure(document, toIndex, {
documentJson,
});
if (collection) {
result.collections.push(collection);
}
// if collection does not remain the same loop through children and change their
// collectionId and move any attachments they may have too. This includes
// archived children, otherwise their collection would be wrong once restored.
if (collectionChanged) {
result.collections.push(newCollection);
const loopChildren = async (documentId: string) => {
const childDocuments = await Document.findAll({
where: {
parentDocumentId: documentId,
},
});
await Promise.all(
childDocuments.map(async (child) => {
await loopChildren(child.id);
child.collectionId = collectionId;
await child.save();
if (newCollection) {
child.collection = newCollection;
}
result.documents.push(child);
})
);
};
await loopChildren(document.id);
const pin = await Pin.findOne({
where: {
documentId: document.id,
collectionId: previousCollectionId,
},
});
if (pin) {
await pinDestroyer({
user,
pin,
ip,
});
}
}
await document.save({
transaction,
});
if (newCollection) {
document.collection = newCollection;
}
result.documents.push(document);
await transaction.commit();
} catch (err) {
if (transaction) {
await transaction.rollback();
}
throw err;
}
}
await Event.create({
name: "documents.move",
actorId: user.id,
documentId: document.id,
collectionId,
teamId: document.teamId,
data: {
title: document.title,
collectionIds: result.collections.map((c) => c.id),
documentIds: result.documents.map((d) => d.id),
await document.save({ transaction });
result.documents.push(document);
await Event.create(
{
name: "documents.move",
actorId: user.id,
documentId: document.id,
collectionId,
teamId: document.teamId,
data: {
title: document.title,
collectionIds: result.collections.map((c) => c.id),
documentIds: result.documents.map((d) => d.id),
},
ip,
},
ip,
});
{
transaction,
}
);
// we need to send all updated models back to the client
return result;