mirror of
https://github.com/wekan/wekan.git
synced 2026-11-25 04:49:41 -05:00
An import that stopped halfway - a restart, a hung writer, a failure - left a partial board nothing recorded. Each board import, copy, Trello zip and Trello API import now writes an importRuns record first, with the id its new board will have; every creator inserts the board with that id and the run's id stamped on it. A heartbeat keeps the run alive; the method deadline now stops the writer at its next step. A scan at startup and every 5 minutes flags a stopped or failed run once and records it in Admin Panel -> Problems -> Recovery, where an administrator keeps the partial board or discards it: only the board carrying the run's stamp is removed, through the board's own removal, then whatever else has its id. Discard is refused while the import's Scrum stage is still written or recovered, and the Scrum resume is refused while the import is discarded. Resume is deliberately not offered: the source file is not kept, for its size, its personal data and the anonymization it would undo; discard and import again instead. Thanks to xet7 !
49 lines
2.3 KiB
JavaScript
49 lines
2.3 KiB
JavaScript
// The source adapters decide what each document means; this writer owns the
|
|
// repeated persistence and old-id -> new-id bookkeeping.
|
|
export async function writeImportedEntity(collection, document, options = {}) {
|
|
// Every WeKan schema's createdAt autoValue replaces the value on insert, so
|
|
// a creation date carried by the source (#1992) was silently reset to the
|
|
// import time. Write it back past the schema; nothing else is touched. Read
|
|
// it BEFORE inserting: collection2 cleans the document object in place.
|
|
const createdAt = document && document.createdAt;
|
|
const id = await collection.direct.insertAsync(document);
|
|
if (createdAt instanceof Date && !Number.isNaN(createdAt.getTime())
|
|
&& typeof collection.rawCollection === 'function') {
|
|
await collection.rawCollection().updateOne({ _id: id }, { $set: { createdAt } });
|
|
}
|
|
if (options.touch) {
|
|
await collection.direct.updateAsync(id, { $set: options.touch });
|
|
}
|
|
if (options.ids && options.sourceId != null) {
|
|
options.ids[options.sourceId] = id;
|
|
}
|
|
return id;
|
|
}
|
|
|
|
// The new board's id and the import run that creates it, both chosen before
|
|
// the first write (server/lib/importRuns.js). Every creator spreads this into
|
|
// the board it inserts, so an import that stops halfway leaves a board its run
|
|
// names and that names its run - the one an administrator may later discard.
|
|
// Without a run (the client simulation) the board gets an id as before.
|
|
export function plannedBoardFields(creator) {
|
|
const run = creator && creator.importRun;
|
|
if (!run || typeof run.boardId !== 'string' || typeof run.runId !== 'string') return {};
|
|
return { _id: run.boardId, importRunId: run.runId };
|
|
}
|
|
|
|
// Run normalized source stages in order and carry the created board id into
|
|
// every later writer. Missing collection arrays normalize to empty arrays.
|
|
export async function runImportPipeline(creator, board, stages) {
|
|
if (!board || typeof board !== 'object' || Array.isArray(board)) {
|
|
throw new TypeError('Imported board must be an object');
|
|
}
|
|
let boardId;
|
|
for (const stage of stages) {
|
|
const input = stage.source ? board[stage.source] || [] : board;
|
|
const result = await creator[stage.method](input, boardId);
|
|
if (stage.createsBoard) boardId = result;
|
|
}
|
|
if (!boardId) throw new Error('Import pipeline did not create a board');
|
|
return boardId;
|
|
}
|