mirror of
https://github.com/wekan/wekan.git
synced 2026-11-25 04:49:41 -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 !