Files
wekan/snap-src/bin/database-merge
T
Lauri Ojansivu 073a327d62 Snap: two commands for a snap serving the older of its two copies.
#6583, comment 5280504937: "The migration and the update to 10.83 startet at
11.08.2026 at 6:35 pm and migration failed. Now we just see the old data from a
migration we did in july 2026. ... Which steps exactly could we do, to restore
the database with our most recent data?"

Everything needed to answer that already existed - db-eval evidence,
database-choose.mjs, database-merge-missing.mjs, database-autopick - and none of
it was a command anybody could run. `snap run wekan.problems` said "No problems
detected", which is true of the things it checks and no help here.

  sudo snap run wekan.database-compare    # what does each copy hold?
  sudo snap run wekan.database-merge      # bring the missing documents across

compare starts each database on a temporary port, counts its documents and finds
the newest moment its data carries, and prints both sides. The running WeKan is
not disturbed and nothing is written - it is database-autopick --dry-run, so
there is one implementation of "read both and compare" rather than a second one
that can drift. It also prints where both copies live and how to take a copy.

merge inserts the documents that exist in the MongoDB copy and not in the
FerretDB one: database-autopick --to-ferretdb. It overwrites nothing, deletes
nothing and reads the MongoDB files only, so it is safe to run without first
knowing which copy is "right" - WeKan's history is append-only, so merging can
only ADD to what a card shows, and the stranded work becomes readable in that
card's History. It asks for a copy of $SNAP_COMMON first, with the command, and
takes --dry-run.

Both are registered in snapcraft.yaml and snapcraft-core26.yaml. The removed
`snap run wekan.database` switch is gone from core26 too, where it had been left
behind when the database setting was removed.

Thanks to waltermhl, lukechao and xet7 !
2026-08-13 15:53:30 +03:00

82 lines
3.6 KiB
Bash
Executable File

#!/bin/bash
# database-merge — bring what the OTHER copy has into the one WeKan serves.
#
# snap run wekan.database-merge # do it
# snap run wekan.database-merge --dry-run # say what it would do
#
# WHAT IT DOES, and the whole of it: for every document that exists in the
# MongoDB copy and NOT in the FerretDB one, insert it. Nothing is overwritten,
# nothing is deleted, and the MongoDB files are only read.
#
# "we just see the old data from a migration we did in july 2026 ... Which
# steps exactly could we do, to restore the database with our most recent
# data" - #6583, comment 5280504937
#
# That is what this is for. A migration that re-ran, or one that was interrupted,
# can leave a snap serving a FerretDB copy older than the MongoDB it was made
# from - and the newer work is sitting in the other file, unread. WeKan's history
# is APPEND-ONLY (docs/Features/Reports/History/History.md): activities, comments
# and the change-history rows are never rewritten in place, so adding the other
# copy's rows can only ADD to what a card shows. That is why this is safe to run
# without knowing which copy is "right".
#
# WHAT IT CANNOT DO: reconcile two edits of the SAME card. The served copy's
# version stands, and the other side's edit stays where it is - in the database
# that is still on disk, and in the merged history rows that describe it.
# Merging field by field is a decision about somebody's work, not a script's.
#
# BEFORE YOU RUN IT, take a copy. It is one command and it makes this reversible:
# sudo snap stop wekan
# sudo cp -a /var/snap/wekan/common /root/wekan-common-backup-$(date +%F)
# sudo snap start wekan
set -u
SNAP="${SNAP:-/snap/${SNAP_INSTANCE_NAME:-${SNAP_NAME:-wekan}}/current}"
SNAP_COMMON="${SNAP_COMMON:-/var/snap/${SNAP_INSTANCE_NAME:-${SNAP_NAME:-wekan}}/common}"
svc="${SNAP_INSTANCE_NAME:-${SNAP_NAME:-wekan}}"
DRY_RUN=false
[ "${1:-}" = "--dry-run" ] && DRY_RUN=true
if [ ! -f "$SNAP/bin/database-autopick" ]; then
echo "This snap revision cannot merge (no bin/database-autopick). Refresh to a newer WeKan."
exit 1
fi
echo "WeKan — merge the other copy of your data into the one being served"
echo "==================================================================="
echo
if [ "$DRY_RUN" = true ]; then
echo "--dry-run: nothing will be written."
else
echo "This ADDS documents that are missing. It overwrites nothing and deletes"
echo "nothing, and both databases stay on disk in $SNAP_COMMON."
echo
echo "If you have not taken a copy yet, stop and take one first:"
echo " sudo snap stop ${svc} && sudo cp -a $SNAP_COMMON /root/wekan-common-backup-\$(date +%F) && sudo snap start ${svc}"
fi
echo
# --to-ferretdb: the destination is FerretDB, which is what WeKan runs on. The
# evidence is still read and printed - it says how far apart the two copies are -
# but it does not get to choose MongoDB, because there is nothing to choose:
# MongoDB is the copy being migrated away from.
args=(--to-ferretdb)
[ "$DRY_RUN" = true ] && args+=(--dry-run)
bash "$SNAP/bin/database-autopick" "${args[@]}"
rc=$?
echo
if [ "$rc" -eq 0 ] && [ "$DRY_RUN" != true ]; then
echo "Done. Restart WeKan so it reads the merged database:"
echo " sudo snap restart ${svc}.wekan"
echo
echo "Then look at a board that was missing work. The documents that came from"
echo "the other copy are in their cards' History as well, because that is how"
echo "WeKan records them."
else
echo "Nothing was merged (the lines above say why). Both databases are untouched."
echo "To see what each one holds: sudo snap run ${svc}.database-compare"
fi
exit "$rc"