mirror of
https://github.com/wekan/wekan.git
synced 2026-11-25 10:48:06 -05:00
An email report, on an instance being served the older of its two copies: "some users are unable to log in (error: 'user not found'), and boards created after mid-July appear to be missing." That is what serving a copy looks like from the outside - the accounts and boards made after the copy was taken simply are not there. Until now the snap handled that by telling the admin to sort it out. #6583 taught it not to switch on a guess, so when both MongoDB and the migrated FerretDB have been written to since the migration it printed the two `snap set wekan database=...` commands and stayed where it was. That message is in `snap logs`; most people never see it, and their site meanwhile shows the wrong copy. A file timestamp cannot answer "which copy holds the work" - an mtime says when a file was touched, and starting a database touches its files. But both copies can be READ, so ask them. bin/db-eval.mjs gains an `evidence` command (per-collection counts, plus the newest timestamp any document carries) and bin/database-choose.mjs decides from the two answers: one holds everything the other holds, and is not older -> serve it, nothing to merge; both hold work the other does not, and one is clearly newer -> serve the newer one and copy across what only exists in the other; too close to call, or nothing readable -> change NOTHING and keep the existing message, because that case is a decision about somebody's work. "Clearly newer" is a margin, not a tie-break: six hours by default, so the writes the migration itself makes on both sides cannot decide anything. WHY THE MERGE IS SAFE, and it is WeKan's own design that makes it safe: the history is APPEND-ONLY (docs/Features/Reports/History/History.md, which gains a section 9a on exactly this). Activities, comments and the coming changeHistory rows are never rewritten in place, so they can only be ADDED to. bin/database- merge-missing.mjs therefore inserts every document whose _id is ABSENT from the chosen database and does nothing else: * a document that IS there is never overwritten - a card edited on both sides keeps the version in the copy being served, which is the newer one; * nothing is deleted, on either side; * the copy that was not chosen stays on disk, so `snap set wekan database=<the other one>` is still the way back - now a choice rather than a repair; * the work done on the copy that is not being served becomes readable in the served copy's card History instead of sitting in a database nobody opens. For the reported symptoms that means the missing users and boards are inserted rather than left behind: a user who could not log in exists again, with their own _id, and their boards with them. A three-way merge - reconciling two edits of one field - is not attempted and will not be from a script. Where the evidence cannot separate the copies, the snap still says so and hands the decision to a person. WEKAN_AUTOPICK=false turns the whole thing off, `database-autopick --dry-run` shows what it would do, and AUTOPICK_MERGE_LIMIT bounds how much it copies. tests/snapDatabaseAutopick.test.cjs covers the decision (both superset directions, the both-diverged case, one side empty or unreadable, too-close, no timestamps, and a BIGGER but older copy that must not win on size) and runs the merge against an in-memory pair of databases: what is missing is inserted, what exists is not overwritten, nothing is deleted on either side, --dry-run writes nothing, a limit is not an error, and the merge script contains no update or delete operation at all. tests/ferretdbMigrationStale.test.cjs keeps its rule - no switch on a guess - with the assertion updated to match what changed: this branch may not choose from the timestamps it has, it delegates to the one thing that can choose, and when even that cannot tell the copies apart the admin still gets both commands. Verified by running both suites and the whole node suite (381 suites, 0 failed). The snap itself cannot be built or refreshed in this sandbox, so the orchestration that starts each database on a temporary port is verified from the code and from the shapes migration-control already uses, not from a running snap. Thanks to xet7 !
206 lines
8.2 KiB
JavaScript
206 lines
8.2 KiB
JavaScript
#!/usr/bin/env node
|
|
// database-merge-missing.mjs — copy the documents that exist in one database and
|
|
// NOT in the other, without changing anything that is already there.
|
|
//
|
|
// This is the second half of bin/database-choose.mjs. When a snap ends up with two
|
|
// copies of its data that have both been written to - a migration that was resumed
|
|
// late, a `snap revert` that ran for a month, a switch back and forth between
|
|
// MongoDB and FerretDB - one of them is chosen to be served, and the work done on
|
|
// the other one would otherwise sit in a database nobody opens.
|
|
//
|
|
// THE ONLY OPERATION IS INSERT, and only where the _id is absent from the target:
|
|
//
|
|
// * a document that exists in the chosen database is never overwritten, so the
|
|
// copy being served keeps its own version of every card, list and board - it
|
|
// is the newer one, which is why it was chosen;
|
|
// * nothing is ever deleted, on either side;
|
|
// * the source database is opened read-only in the sense that matters: this
|
|
// writes to the target only, and the source's files are untouched;
|
|
// * a document that only ever existed on the other side is added, so it stops
|
|
// being invisible.
|
|
//
|
|
// WHY THAT IS ENOUGH, and it is WeKan's own design that makes it enough: the
|
|
// history is APPEND-ONLY (docs/Features/Reports/History/History.md). Activities,
|
|
// comments and the change-history rows are never rewritten in place, so merging
|
|
// them can only ADD to what a card's Activity feed shows. The work done on the
|
|
// copy that is not being served becomes readable in the history of the copy that
|
|
// is - which is the difference between "your data is in a database you cannot
|
|
// reach" and "your data is in the history".
|
|
//
|
|
// WHAT IT DOES NOT DO. It does not reconcile two edits of the same field: if a
|
|
// card was edited on both sides, the version in the served copy stands and the
|
|
// other side's edit remains where it is - in the source database, still on disk,
|
|
// and in the merged history rows that describe it. Merging field by field is a
|
|
// decision about somebody's work and is not a script's to make.
|
|
//
|
|
// Usage:
|
|
// database-merge-missing.mjs <source-url> <target-url> [--limit N] [--dry-run]
|
|
// Prints a JSON summary: { inserted: {collection: n}, skipped, errors, dryRun }
|
|
|
|
import { createRequire } from 'node:module';
|
|
import { pathToFileURL } from 'node:url';
|
|
import { MERGE_COLLECTIONS } from './database-choose.mjs';
|
|
|
|
// Same driver resolution as db-eval.mjs: Node's ESM loader ignores NODE_PATH, and
|
|
// the snap points NODE_PATH at the WeKan bundle's node_modules.
|
|
const _roots = [];
|
|
const _push = (u) => { if (u) { try { _roots.push(new URL(u)); } catch { /* ignore */ } } };
|
|
if (process.env.SNAP) _push(pathToFileURL(process.env.SNAP + '/'));
|
|
if (process.env.NODE_PATH) {
|
|
_push(pathToFileURL(process.env.NODE_PATH.split(':')[0]
|
|
.replace(/\/programs\/server\/node_modules\/?$/, '') + '/'));
|
|
}
|
|
_push(new URL('../', import.meta.url));
|
|
const _subPaths = [
|
|
'programs/server/npm/node_modules/meteor/npm-mongo/node_modules/_.cjs',
|
|
'programs/server/npm/node_modules/_.cjs',
|
|
'programs/server/node_modules/_.cjs',
|
|
'_.cjs',
|
|
];
|
|
const _requires = [];
|
|
for (const root of _roots) {
|
|
for (const sub of _subPaths) {
|
|
try { _requires.push(createRequire(new URL(sub, root))); } catch { /* ignore */ }
|
|
}
|
|
}
|
|
function requireAny(spec) {
|
|
for (const req of _requires) { try { return req(spec); } catch { /* next */ } }
|
|
return null;
|
|
}
|
|
|
|
const BATCH = 200;
|
|
|
|
// How many ids to ask the target about at once. A card board can hold tens of
|
|
// thousands of activities, and `$in` with all of them is a query no database
|
|
// enjoys.
|
|
const ID_PROBE = 500;
|
|
|
|
async function mergeMissing(sourceDb, targetDb, options = {}) {
|
|
const collections = options.collections || MERGE_COLLECTIONS;
|
|
const limit = Number.isFinite(options.limit) ? options.limit : Infinity;
|
|
const dryRun = !!options.dryRun;
|
|
const log = options.log || (() => {});
|
|
const summary = { inserted: {}, skipped: 0, errors: [], dryRun };
|
|
let budget = limit;
|
|
|
|
for (const name of collections) {
|
|
if (budget <= 0) break;
|
|
let cursor;
|
|
try {
|
|
cursor = sourceDb.collection(name).find({});
|
|
} catch (e) {
|
|
summary.errors.push(`${name}: ${e.message}`);
|
|
continue;
|
|
}
|
|
const target = targetDb.collection(name);
|
|
let pending = [];
|
|
let insertedHere = 0;
|
|
|
|
const flush = async () => {
|
|
if (pending.length === 0) return;
|
|
const batch = pending;
|
|
pending = [];
|
|
// Ask the target which of these it already has, and insert only the rest.
|
|
// The insert is `ordered: false` as well, so a duplicate that appears
|
|
// between the probe and the write cannot stop the ones behind it.
|
|
let known = new Set();
|
|
try {
|
|
const ids = batch.map(doc => doc._id);
|
|
const found = await target.find({ _id: { $in: ids } }, { projection: { _id: 1 } }).toArray();
|
|
known = new Set(found.map(doc => String(doc._id)));
|
|
} catch (e) {
|
|
summary.errors.push(`${name}: ${e.message}`);
|
|
return;
|
|
}
|
|
const missing = batch.filter(doc => !known.has(String(doc._id)));
|
|
summary.skipped += batch.length - missing.length;
|
|
if (missing.length === 0) return;
|
|
if (dryRun) {
|
|
insertedHere += missing.length;
|
|
budget -= missing.length;
|
|
return;
|
|
}
|
|
try {
|
|
await target.insertMany(missing, { ordered: false });
|
|
insertedHere += missing.length;
|
|
budget -= missing.length;
|
|
} catch (e) {
|
|
// A duplicate key here is the good failure: the document arrived by
|
|
// another route. Anything the driver did insert is counted by result.
|
|
const inserted = (e && e.result && e.result.insertedCount) || 0;
|
|
insertedHere += inserted;
|
|
budget -= inserted;
|
|
if (!/duplicate key/i.test(e.message || '')) summary.errors.push(`${name}: ${e.message}`);
|
|
}
|
|
};
|
|
|
|
try {
|
|
for await (const doc of cursor) {
|
|
pending.push(doc);
|
|
if (pending.length >= Math.min(BATCH, ID_PROBE)) {
|
|
await flush();
|
|
if (budget <= 0) break;
|
|
}
|
|
}
|
|
await flush();
|
|
} catch (e) {
|
|
summary.errors.push(`${name}: ${e.message}`);
|
|
} finally {
|
|
try { await cursor.close(); } catch { /* ignore */ }
|
|
}
|
|
|
|
if (insertedHere > 0) {
|
|
summary.inserted[name] = insertedHere;
|
|
log(`${name}: ${insertedHere} document(s) that existed only in the other database`);
|
|
}
|
|
}
|
|
return summary;
|
|
}
|
|
|
|
export { mergeMissing, BATCH, ID_PROBE };
|
|
|
|
// CLI. Wrapped in a function rather than run with top-level `await`: a module
|
|
// with top-level await is an "async graph", and Node refuses to require() one -
|
|
// which would stop tests/snapDatabaseAutopick.test.cjs (a .cjs suite) from
|
|
// exercising mergeMissing at all.
|
|
async function main() {
|
|
const [sourceUrl, targetUrl, ...rest] = process.argv.slice(2);
|
|
if (!sourceUrl || !targetUrl) {
|
|
console.error('usage: database-merge-missing.mjs <source-url> <target-url> [--limit N] [--dry-run]');
|
|
return 2;
|
|
}
|
|
const limitAt = rest.indexOf('--limit');
|
|
const options = {
|
|
dryRun: rest.includes('--dry-run'),
|
|
limit: limitAt === -1 ? Infinity : Number(rest[limitAt + 1]),
|
|
log: (message) => console.error('[merge] ' + message),
|
|
};
|
|
const mongodb = requireAny('mongodb');
|
|
if (!mongodb || typeof mongodb.MongoClient !== 'function') {
|
|
console.error('[merge] could not resolve the mongodb driver from the WeKan bundle');
|
|
return 2;
|
|
}
|
|
const { MongoClient } = mongodb;
|
|
const source = new MongoClient(sourceUrl, { serverSelectionTimeoutMS: 10000 });
|
|
const target = new MongoClient(targetUrl, { serverSelectionTimeoutMS: 10000 });
|
|
let code = 0;
|
|
try {
|
|
await source.connect();
|
|
await target.connect();
|
|
const summary = await mergeMissing(source.db('wekan'), target.db('wekan'), options);
|
|
console.log(JSON.stringify(summary));
|
|
if (summary.errors.length) code = 1;
|
|
} catch (e) {
|
|
console.error('[merge] ' + (e && e.message ? e.message : String(e)));
|
|
code = 1;
|
|
} finally {
|
|
try { await source.close(true); } catch { /* ignore */ }
|
|
try { await target.close(true); } catch { /* ignore */ }
|
|
}
|
|
return code;
|
|
}
|
|
|
|
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].split('/').pop())) {
|
|
main().then(code => process.exit(code)).catch(() => process.exit(1));
|
|
}
|