Add Phase 3: account-level import #560

Open
tobi wants to merge 3 commits from phase3-account-import into phase2-polls-tags-membership
Owner

Stacked on #559 — Phase 3, the last phase of the importer rewrite: account-level import (Importer.restoreAccount), completing what #557/#558/#559 built for sites.

What's here

  • Exporter.saveAccount gets the same per-export HMAC key + accounts.json mechanism saveSite already had (PR #558) — without it, Importer.preview would have nothing to match against for an account export, and every creator/modifier would fall back to a placeholder.
  • Refactored Importer.restoreSite's per-entity logic (skins, stories, comments, images, files, polls, votes, tags, memberships) into shared Importer.restore*(rows, ctx) functions, parameterized by an asset-path prefix — saveSite zips assets under static/..., but saveAccount zips them under <originalSiteName>/... instead, so the same restore logic needs to know which convention it's reading back.
  • Importer.restoreAccount(user, tempDir, accountMap) loops those same functions over every site the account owned (per sites.json's role column — sites where the account was only a subscriber/contributor never had their metadata exported at all, so they're skipped, not recreated, since there's nothing to restore and re-creating someone else's site wouldn't make sense anyway). Site names may already be taken (restoring into the same instance the export came from, or an unrelated collision), so creation retries with a numbered suffix rather than dropping the site. A poll's own single vote (all saveAccount ever exports — no separate votes.json, unlike a site export) is replayed directly by the shared restorePolls function instead of a separate step.
  • User.prototype.import_action mirrors Site's own two-step preview/confirm flow, reusing Importer.preview/mergeOverrides/renderReviewHtml unchanged (the last one moved from Site to Importer, since both prototypes need it now). Unlike Site, User has no natural files collection to attach an uploaded archive to, so the upload handling follows Layout.prototype.import_action's simpler precedent instead (straight to a plain temp file, no File object at all).
  • Members.prototype.import_action proxy, plus adding 'import' to the same getPermission case list export/edit already use — confirmed live that "act on your own account" only works through an explicit per-action proxy to User.prototype.<action>.call(session.user), there's no generic passthrough.

A Phase 1 bug this testing happened to expose

While testing this, found that image.setMetadata(row.metadata)/file.setMetadata(row.metadata) in Importer.restoreImages/restoreFiles (from PR #558) were re-applying the export's old fileName/contentType/etc. right after Image.add/File.add's own update() had already correctly regenerated those exact fields from the actual re-uploaded bytes — silently overwriting the fresh, correct value with one that doesn't exist on disk under the new site, so every restored image/file 404s. That's a Phase 1 bug, not something Phase 3 introduced, so the fix lives in its own commit on native-site-restore (now merged into this stack) rather than bundled here — see PR #558 for detail. Caught by exporting and re-importing an account owning many sites: the same file, restored independently into several different sites across earlier test runs, ended up with every one of those distinct File rows sharing the literal same fileName in the metadata table despite genuinely different bytes on disk under each site.

Verified end-to-end (ct 3998, see ~/.claude/plans/kind-sauteeing-goblet.md)

  • Exported admin's full account (11 owned sites, including a duplicate row and several already-taken names) and re-imported it into the same account: every site was recreated under a de-conflicted name (-imported-N suffix) with its content restored, exercising the collision-retry path thoroughly.
  • Separately, since that first test reused pre-existing (already-corrupted-by-earlier-testing) data: created a fresh site with a fresh file upload, exported and imported it cleanly, and confirmed via direct DB inspection and through the running app (not just filesystem inspection) that the restored file's DB-recorded fileName correctly matches what's actually on disk, and is byte-identical to the source.

Known gaps

  • Same as #558/#559: a failed restore partway through isn't rolled back, and only tested against H2 (via tools/antclick), not the MariaDB path production actually runs.
  • Membership/tags aren't restored for account-level import at all (saveAccount doesn't export either) — acceptable, not a bug: tags aren't meaningfully scoped to one creator, and the only membership that matters (the owner) is already established when each site is created.

Test plan

  • Export an account owning multiple sites (including intentional name collisions) and re-import it into the same account
  • Verify every owned site is recreated under a de-conflicted name with content restored
  • Verify a freshly restored file's DB metadata and on-disk filename agree and are byte-identical to the source, through the running app
  • Verify cross-account restore: importing into a genuinely different, pre-existing target account correctly preserves original authorship instead of collapsing it onto the importer
  • Deploy to antville-test.de and spot-check against the real MariaDB path before merging

Update: cross-account attribution now actually tested

The original test only ever restored admin's own export back into admin itself — genuine cross-account restore (into a different, pre-existing target account) was untested. Fixed that: registered a second real account (seconduser, via a scoped SQL toggle of root.loginScope then reverted, and granted PRIVILEGED status the same way — site.getPermission('import') requires it regardless of ownership, a gap the original plan flagged but never resolved, worth a separate decision), then logged in as seconduser and imported admin's account export.

Confirmed via direct DB query: every newly-created site is owned by seconduser (correct — they triggered the restore), but the actual content underneath is attributed to admin (the only distinct creator across all of it) — original authorship is correctly preserved via name+e-mail matching, not collapsed onto whoever performs the restore. Same result confirmed for a site-level cross-account import too (PR #558's mechanism).

Stacked on #559 — Phase 3, the last phase of the importer rewrite: account-level import (`Importer.restoreAccount`), completing what #557/#558/#559 built for sites. ## What's here - **`Exporter.saveAccount`** gets the same per-export HMAC key + `accounts.json` mechanism `saveSite` already had (PR #558) — without it, `Importer.preview` would have nothing to match against for an account export, and every creator/modifier would fall back to a placeholder. - **Refactored `Importer.restoreSite`'s per-entity logic** (skins, stories, comments, images, files, polls, votes, tags, memberships) into shared `Importer.restore*(rows, ctx)` functions, parameterized by an asset-path prefix — `saveSite` zips assets under `static/...`, but `saveAccount` zips them under `<originalSiteName>/...` instead, so the same restore logic needs to know which convention it's reading back. - **`Importer.restoreAccount(user, tempDir, accountMap)`** loops those same functions over every site the account owned (per `sites.json`'s `role` column — sites where the account was only a subscriber/contributor never had their metadata exported at all, so they're skipped, not recreated, since there's nothing to restore and re-creating someone else's site wouldn't make sense anyway). Site names may already be taken (restoring into the same instance the export came from, or an unrelated collision), so creation retries with a numbered suffix rather than dropping the site. A poll's own single vote (all `saveAccount` ever exports — no separate `votes.json`, unlike a site export) is replayed directly by the shared `restorePolls` function instead of a separate step. - **`User.prototype.import_action`** mirrors `Site`'s own two-step preview/confirm flow, reusing `Importer.preview`/`mergeOverrides`/`renderReviewHtml` unchanged (the last one moved from `Site` to `Importer`, since both prototypes need it now). Unlike `Site`, `User` has no natural `files` collection to attach an uploaded archive to, so the upload handling follows `Layout.prototype.import_action`'s simpler precedent instead (straight to a plain temp file, no `File` object at all). - **`Members.prototype.import_action`** proxy, plus adding `'import'` to the same `getPermission` case list `export`/`edit` already use — confirmed live that "act on your own account" only works through an explicit per-action proxy to `User.prototype.<action>.call(session.user)`, there's no generic passthrough. ## A Phase 1 bug this testing happened to expose While testing this, found that `image.setMetadata(row.metadata)`/`file.setMetadata(row.metadata)` in `Importer.restoreImages`/`restoreFiles` (from PR #558) were re-applying the export's *old* `fileName`/`contentType`/etc. right after `Image.add`/`File.add`'s own `update()` had already correctly regenerated those exact fields from the actual re-uploaded bytes — silently overwriting the fresh, correct value with one that doesn't exist on disk under the new site, so every restored image/file 404s. That's a Phase 1 bug, not something Phase 3 introduced, so the fix lives in its own commit on `native-site-restore` (now merged into this stack) rather than bundled here — see PR #558 for detail. Caught by exporting and re-importing an account owning many sites: the same file, restored independently into several different sites across earlier test runs, ended up with every one of those distinct `File` rows sharing the literal same `fileName` in the metadata table despite genuinely different bytes on disk under each site. ## Verified end-to-end (ct 3998, see `~/.claude/plans/kind-sauteeing-goblet.md`) - Exported `admin`'s full account (11 owned sites, including a duplicate row and several already-taken names) and re-imported it into the same account: every site was recreated under a de-conflicted name (`-imported-N` suffix) with its content restored, exercising the collision-retry path thoroughly. - Separately, since that first test reused pre-existing (already-corrupted-by-earlier-testing) data: created a fresh site with a fresh file upload, exported and imported it cleanly, and confirmed via direct DB inspection *and* through the running app (not just filesystem inspection) that the restored file's DB-recorded `fileName` correctly matches what's actually on disk, and is byte-identical to the source. ## Known gaps - Same as #558/#559: a failed restore partway through isn't rolled back, and only tested against H2 (via `tools/antclick`), not the MariaDB path production actually runs. - Membership/tags aren't restored for account-level import at all (`saveAccount` doesn't export either) — acceptable, not a bug: tags aren't meaningfully scoped to one creator, and the only membership that matters (the owner) is already established when each site is created. ## Test plan - [x] Export an account owning multiple sites (including intentional name collisions) and re-import it into the same account - [x] Verify every owned site is recreated under a de-conflicted name with content restored - [x] Verify a freshly restored file's DB metadata and on-disk filename agree and are byte-identical to the source, through the running app - [x] Verify cross-account restore: importing into a genuinely different, pre-existing target account correctly preserves original authorship instead of collapsing it onto the importer - [ ] Deploy to antville-test.de and spot-check against the real MariaDB path before merging ## Update: cross-account attribution now actually tested The original test only ever restored `admin`'s own export back into `admin` itself — genuine cross-account restore (into a different, pre-existing target account) was untested. Fixed that: registered a second real account (`seconduser`, via a scoped SQL toggle of `root.loginScope` then reverted, and granted `PRIVILEGED` status the same way — `site.getPermission('import')` requires it regardless of ownership, a gap the original plan flagged but never resolved, worth a separate decision), then logged in as `seconduser` and imported `admin`'s account export. Confirmed via direct DB query: every newly-created site is owned by `seconduser` (correct — they triggered the restore), but the actual content underneath is attributed to `admin` (the only distinct creator across all of it) — original authorship is correctly preserved via name+e-mail matching, not collapsed onto whoever performs the restore. Same result confirmed for a site-level cross-account import too (PR #558's mechanism).
tobi self-assigned this 2026-07-18 17:36:22 +00:00
Extends Exporter.saveAccount with the same per-export HMAC key and
accounts.json mechanism saveSite already had — without it,
Importer.preview would have nothing to match against for an account
export, and every creator/modifier would fall back to a placeholder.

Refactors Importer.restoreSite's per-entity logic (skins, stories,
comments, images, files, polls, votes, tags, memberships) into shared
Importer.restore*(rows, ctx) functions, parameterized by an asset-path
prefix — Exporter.saveSite zips assets under "static/...", but
Exporter.saveAccount zips them under "<originalSiteName>/..." instead,
so the same restore logic needs to know which convention it's reading
back. Importer.restoreAccount then loops these same functions over every
site the account owned (per sites.json's role column — sites where the
account was only a subscriber/contributor never had their metadata
exported at all, so they're skipped, not recreated). Site names may
already be taken (restoring into the same instance the export came from,
or an unrelated collision), so site creation retries with a numbered
suffix rather than dropping the site. A poll's own single vote (all
saveAccount ever exports — no separate votes.json, unlike a site export)
is replayed directly by the shared restorePolls function instead of a
separate step.

New User.prototype.import_action mirrors Site's own two-step
preview/confirm flow, reusing Importer.preview/mergeOverrides/
renderReviewHtml (moved from Site to Importer, since both prototypes
need it now) unchanged. Unlike Site, User has no natural files
collection to attach an uploaded archive to, so the upload handling
follows Layout.prototype.import_action's simpler precedent instead
(straight to a plain temp file, no File object at all) rather than
Site's. Also required a Members.prototype.import_action proxy — actions
on "your own account" only work via an explicit proxy to
User.prototype.<action>.call(session.user) for each action name
individually (confirmed live: this is how export/edit already work,
there's no generic passthrough) — and adding 'import' to the same
getPermission case list export/edit already use.

Also includes a real bug found by this testing, worth calling out
separately even though it's bundled in the same commit as the refactor
it depends on: image.setMetadata(row.metadata)/file.setMetadata(...)
were re-applying the export's OLD fileName/contentType/etc. right after
Image.add/File.add's own update() had already correctly regenerated
those exact fields from the actual re-uploaded bytes — silently
overwriting the fresh, correct value with one that doesn't exist on disk
under the new site. Caught by exporting and re-importing an account
owning many sites: the same file, restored independently into several
different sites across earlier test runs, ended up with every one of
those distinct File rows sharing the literal same fileName in the
metadata table despite genuinely different bytes on disk under each
site. The equivalent fix for the single-site path lives in a separate
commit on native-site-restore, since that's a Phase 1 bug this testing
happened to be the first to expose, not something Phase 3 introduced.

Verified end-to-end (ct 3998, see
~/.claude/plans/kind-sauteeing-goblet.md): exported admin's full account
(11 owned sites, including intentional name collisions to exercise the
suffix-retry path) and re-imported it into the same account; every site
was recreated under a de-conflicted name with its content restored
correctly.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0177qdRkssL6kBk5ngMDHwnP
An unconfirmed ambiguous match falls back to a placeholder, but its name
is by definition already taken by a real, different account on the
target instance — that's exactly why it was ambiguous in the first
place. User.add's root.users.add(user) enforces name-uniqueness
instance-wide, so this threw "An object named X is already contained
in the collection" and aborted the whole restore. Found by importing a
real site export (with two of its real member accounts' e-mail hashes
corrupted) back into the same instance and leaving the resulting
ambiguous matches unconfirmed.
The previous fix retried User.add inside a try/catch after a collision,
but User.add's user.map({...}) persists the account row immediately —
before the later root.users.add(user) collision check even runs. That
left a stray, unblocked, orphaned account behind for every name it had
to give up on. Verified via a live re-import: the try/catch version
left 'macnearney'/'sandy' duplicate rows (status regular, not blocked)
next to the correctly-created 'macnearney-imported-1'/'sandy-imported-1'
placeholders. Checking User.getByName first avoids ever calling
User.add with a name that's going to collide.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin phase3-account-import:phase3-account-import
git switch phase3-account-import

Merge

Merge the changes and update on Forgejo.
git switch phase2-polls-tags-membership
git merge --no-ff phase3-account-import
git switch phase3-account-import
git rebase phase2-polls-tags-membership
git switch phase2-polls-tags-membership
git merge --ff-only phase3-account-import
git switch phase3-account-import
git rebase phase2-polls-tags-membership
git switch phase2-polls-tags-membership
git merge --no-ff phase3-account-import
git switch phase2-polls-tags-membership
git merge --squash phase3-account-import
git switch phase2-polls-tags-membership
git merge --ff-only phase3-account-import
git switch phase2-polls-tags-membership
git merge phase3-account-import
git push origin phase2-polls-tags-membership
Sign in to join this conversation.
No description provided.