mirror of
https://github.com/wekan/wekan.git
synced 2026-11-26 14:45:11 -05:00
visible until logout/login.** The `board` publication batched checklists,
checklist items, comments and attachments into board-level cursors filtered by
`cardId: { $in: cardIds }`, where `cardIds` was a one-time snapshot. In
`reywood:publish-composite` a child cursor only re-runs when its parent (the
board) document changes, so the snapshot never refreshed when a card was added
— a checklist on a card created after subscribing matched no published card and
only appeared on the next subscribe (logout/login). This was a regression from
the "Optimized board loading" change, which had replaced the original reactive
per-card child cursors with these batched snapshots. Fixed by **denormalizing a
`boardId` field onto `Checklists` and `ChecklistItems`** so they can be
published with a single board-level cursor filtered by `boardId` — one cursor
per collection (keeping the load optimization) that still reacts to checklists
on newly added cards, because a new checklist is created already carrying the
board's id. `boardId` is set on insert (server `before.insert` hooks, plus
explicitly in the Trello/WeKan board importers, which use `direct.insert` and
bypass hooks), re-derived when a checklist/item or its card moves to another
card (`before.update` on `cardId`) or the card moves to another board
(`Cards.after.update` cascade), and backfilled for existing data by an
idempotent startup migration. New `{ boardId: 1 }` indexes were added on both
collections. Comments and attachments instead remain reactive as per-card
children of the cards cursor. Assigned-only board members still only receive
checklists for cards assigned to them (the board-level cursor falls back to the
assigned cards' ids for those roles).
Thanks to ahlgrimma and Claude !
Fixes #6361
45 lines
1.9 KiB
JavaScript
45 lines
1.9 KiB
JavaScript
import Cards from '/models/cards';
|
|
|
|
// ============================================================================
|
|
// Memory-safe, idempotent backfill of a denormalized board id onto card-related
|
|
// collections (checklists, checklist items, comments).
|
|
//
|
|
// It must NOT load every document into memory (an early version used
|
|
// `.fetchAsync()` over all rows plus `ReactiveCache.getCard()` per row, which
|
|
// cached every card and ran the server out of heap on large databases). Instead
|
|
// it streams the Cards collection one document at a time and issues a single
|
|
// multi-update per card for the rows that are still missing the board id, so
|
|
// peak memory is one card document regardless of database size.
|
|
//
|
|
// Idempotent: a cheap `findOne` guard skips the whole pass once every row has a
|
|
// board id, and the `{ [boardField]: { $exists: false } }` filter makes each
|
|
// per-card update a no-op after the first run.
|
|
// ============================================================================
|
|
export async function backfillBoardIdFromCard(
|
|
collection,
|
|
{ cardField = 'cardId', boardField = 'boardId', label = 'collection' } = {},
|
|
) {
|
|
try {
|
|
const anyMissing = await collection.findOneAsync(
|
|
{ [boardField]: { $exists: false } },
|
|
{ fields: { _id: 1 } },
|
|
);
|
|
if (!anyMissing) return; // already fully backfilled — nothing to do
|
|
|
|
let processedCards = 0;
|
|
const cursor = Cards.find({}, { fields: { _id: 1, boardId: 1 } });
|
|
await cursor.forEachAsync(async card => {
|
|
if (!card.boardId) return;
|
|
await collection.direct.updateAsync(
|
|
{ [cardField]: card._id, [boardField]: { $exists: false } },
|
|
{ $set: { [boardField]: card.boardId } },
|
|
{ multi: true },
|
|
);
|
|
processedCards += 1;
|
|
});
|
|
console.log(`[${label}] Backfilled ${boardField} (scanned ${processedCards} cards).`);
|
|
} catch (e) {
|
|
console.error(`[${label}] Failed to backfill ${boardField}:`, e);
|
|
}
|
|
}
|