Repository navigation
Commit 803f909
fix(objectql): compute on stored rows the roll-ups a package install or upgrade adds (#34282)
<!-- claude-projects-attribution: {"github_login":"zhuangjianguo"} -->
_Requested by **Steedos** · [project
thread](https://claude.ai/code/project/chan_018ETKaNwMZCNRYD7D8Q9Ubb?thread=cmsg_018ETKaNwMZCNRYD7D8Q9UbbKnsd8ZLsq9hBbRRm5JvJb2)_
Fixes #34096
Clause-②: yes (widening)
Metadata-protocol: none
## 维护者速读
- 改了什么:包安装/升级(安装门的 `syncSchemas`)现在会在已有记录上计算升级新增的汇总(roll-up)字段,只补仍为 NULL
的行。
- 为什么改:HotCRM 6.2→6.4 升级后,旧合同/客户的已开票、已收款、应收余额一律为空,新建记录却是
0,直到某个子记录被写入才会填上。
- 风险与回滚:只走启动后新出现的汇总字段;启动时已存在的不扫,二次升级零读写。回填写父记录时带
`skipAutomations`,与安装门自己的种子写入一致,不触发父对象的元数据钩子和记录变更流程,但上一层汇总照常重算。失败按 error
记日志并在下次安装时重试。回滚即回退本 PR,已写入的值本就是引擎自己会写的值。
-
先看:`packages/objectql/src/summary-install-materialise.ts`(新增字段判定与失败处理)、`engine-compatibility-api.ts`
的 `syncSchemas` 末尾、`plugin.ts` 的 `kernel:bootstrapped` 封存。
- 两分钟验证:`pnpm --filter @objectstack/objectql exec vitest run --project
local src/engine-34096-upgrade-added-summary-materialise.test.ts`,预期
10/10 通过。
- 需要你做的:确认"随重启到达的汇总字段(代码应用新版本、热注册失败后重启加载)仍由 `os migrate summary-nulls`
补"这一边界可接受。
## What changes
- **New `packages/objectql/src/summary-install-materialise.ts`.** When
the boot completes, the engine records the roll-ups it serves
(`ObjectQLPlugin` calls `ObjectQL.sealSummariesMaterialisedAtBoot()` at
`kernel:bootstrapped`, after the install-local rehydrate at
`kernel:ready`). Later, `ObjectQL.materialiseAddedSummaries()` walks
only the roll-ups served now that are not recorded. It fills them
through the existing `backfillSummaryNulls` and records them once the
walk is complete.
- **`ObjectQL.syncSchemas()`** runs it last. That is the sync every
runtime install door asks for after its hot register;
`MarketplaceInstallLocal` is the measured door. A sync before the boot
completes walks nothing, and that includes the rehydrate's sync.
- **How the walk writes.** It uses `aggregateSummaryValue`, the
child-write recompute's aggregate, and the parent `update` with `{
isSystem: true, skipAutomations: true }` and no tenant, so it covers
every organization's rows. `skipAutomations` matches the install door's
own seed writes (`SEED_OWNER_STAMP_CTX`, the seed-loader rule: end-state
data, not user events), so no metadata-bound hook or record-change flow
runs on a backfilled parent. The engine's roll-up recompute is not an
automation and still runs, so a roll-up over a backfilled parent moves
too. The walk writes no child row. Pages are keyset pages of 500,
bounded per object at 100,000. Only rows still `NULL` are written, so
the walk is idempotent.
- **New opt-in `skipAutomations` on `SummaryBackfillOptions`.** Only
`summary-install-materialise.ts` sets it. Without it, the write context
is the bare `{ isSystem: true }` it always was, so `os migrate
summary-nulls` is unchanged and still runs those automations at the time
the operator picks.
- **Each new roll-up is named in `recomputeUndefinedOnEmpty`.** The door
knows these columns are new, so new `min`/`max`/`avg` columns are
computed too.
- **Null versus 0, settled.** After the fix, an empty child set reads
`0` for `count`/`sum` and `null` for `min`/`max`/`avg`. A pre-existing
row and a new row read the same.
- **A failed or interrupted walk.** This covers a row that fails, an
object that cannot be read, or the per-object bound reached. The engine
logs it at `error` once, with the consequence and the exact `os migrate
summary-nulls --apply --recompute-undefined-on-empty OBJECT.FIELD` line.
It does not record those roll-ups, so the next install's sync in the
same process walks them again and fills only what is still `NULL`.
- **Why materialise rather than compute on read.** A value computed on
read is not the stored column. Filters, sorts, `GROUP BY` and formulas
would still read `NULL`, and every list read would pay an aggregate per
row. The install door is the one moment that knows the column is new and
has just created it.
- **Docs:** `content/docs/deployment/cli.mdx` (`os migrate
summary-nulls`) says when an install does this and when the command is
still owed.
- **Changeset:** `@objectstack/objectql` patch.
## Tests
New
`packages/objectql/src/engine-34096-upgrade-added-summary-materialise.test.ts`,
10 cases:
- **v1 → v2 upgrade.** Version 2 adds `sum`/`count`/`max` roll-ups
through `objectExtensions` over existing parents and children. Every
parent reads the computed value, and no child row is written. A parent
with no children reads `0`/`0`/`null`.
- A pre-existing parent reads exactly what a new parent with the same
children reads.
- A second upgrade (v3) that adds no roll-up makes zero driver reads and
zero writes.
- A roll-up already present when the boot completed is not walked by a
later sync.
- The parent writes are `isSystem` with no `tenantId`, and no hook of
the child object fires.
- A failed row leaves the walk unrecorded and logs once at `error`,
naming the roll-up and the command. The next sync rewrites only that
row.
- A real `ObjectKernel` boot with `ObjectQLPlugin` seals at
`kernel:bootstrapped`, then the upgrade fills.
- Each backfilled parent's write carries `skipAutomations: true`, and a
metadata-bound parent update hook (bound through `bindHooks`) does not
fire.
- A chained roll-up still recomputes: an account roll-up over the
contracts' new `invoiced_amount` reads 40000 after the walk.
- CONTROL: the operator path (`backfillSummaryNulls` with no option)
writes without `skipAutomations`, and the bound hook fires as before.
<details><summary>Red first, green on the fix, objectql full
suite</summary>
- Red, test file only, on the code of main: `Tests 4 failed | 2 passed
(6)`, `AssertionError: expected null to be 700`. The two that pass there
are the "runs nothing" pins, which nothing on main can violate.
- Round 1, green at 71f012a, before the three S1 cases were added:
`Test Files 1 passed (1)`, `Tests 7 passed (7)`. The file now holds 10
cases; see the S1 lines below.
- `pnpm --filter @objectstack/objectql exec vitest run --project local
--maxWorkers=2` at 71f012a: `Test Files 599 passed (599)`, `Tests
13287 passed (13287)`, `VERDICT command-exit 0`.
- `pnpm --filter @objectstack/objectql build && pnpm --filter
@objectstack/objectql typecheck`: exit 0, and `check:test-typecheck:
OK`.
- The round-1 head 2d39431 added only the docs commit after
71f012a.
- Review S1 (skipAutomations on the install walk), head c6ff87c:
- Red first, with the new pins against the e3a4385 code: `Tests 1
failed | 9 passed (10)`, `AssertionError: expected undefined to be
true`. The parent write carried no `skipAutomations`. The
chained-roll-up and CONTROL pins pass there, as they should.
- Green: this file plus `summary-backfill.test.ts` gives `Test Files 2
passed (2)`, `Tests 30 passed (30)`.
- These single-file runs ran outside the shared verify lock, as a
declared narrowing, because the lock queue was held by a long dogfood
run for about 30 minutes.
- `pnpm --filter @objectstack/objectql build && pnpm --filter
@objectstack/objectql typecheck` under the lock: exit 0, and
`check:test-typecheck: OK`.
- `pnpm --filter @objectstack/objectql exec vitest run --project local
--maxWorkers=2` under the lock at c6ff87c: `Test Files 599 passed
(599)`, `Tests 13331 passed (13331)`, `VERDICT command-exit 0`.
</details>
<details><summary>Ablations (each mutated through
scripts/ablation-replace.mjs, restored with git checkout HEAD and
checked by blob hash)</summary>
- A1: drop `await this.materialiseAddedSummaries()` from `syncSchemas` →
4 failed. Restored, blob 9844dc3 == HEAD.
- A2: drop `recomputeUndefinedOnEmpty: added` → 3 failed (the `max`
column stays null). Restored.
- A3: never record a completed walk → 1 failed: the second upgrade
re-walks. Restored.
- A4: the boot seal records nothing → 1 failed: a boot-present roll-up
is walked. Restored.
- A5: treat an incomplete walk as complete → 1 failed: the failed row is
never retried. Restored.
- A6: drop the plugin's `sealSummariesMaterialisedAtBoot()` call → 1
failed (the real-kernel case). Restored, blob d5b497b == HEAD.
- A7 (S1): drop `skipAutomations: true` from the install walk → 1 failed
(the skipAutomations / bound-hook pin). Restored, blob a3b8467 ==
HEAD.
The subject resolves to `src/` through relative imports, so no dist
preflight applies.
</details>
## Acceptance notes
- **Boundary: a roll-up that arrives with a restart rather than through
an install door is not walked.** That covers a code-defined app's new
version, and a marketplace install whose hot register failed and loaded
at the next restart. A boot records what it serves and computes nothing,
because a boot cannot tell "new since the last version" from "already
computed" without a persisted record. `os migrate summary-nulls --apply`
remains the backfill there. The changeset and the CLI docs say so.
- **A process that dies mid-walk** records nothing. Its next boot seals
what it serves, so the operator command is the re-run there.
- **Authored-package publish.** The single-object publish door
(`syncObjectSchema`, `protocol.installPackage`) is not wired. A batch
publish can sync a parent before its new child table exists, and the
walk would then log a false failure. Left for a decision, not guessed.
- **Automations stay off on a backfilled parent.** A field that a
parent's own update hook derives from a new roll-up is therefore not
refreshed by the install. It fills at that row's next ordinary write, or
when `os migrate summary-nulls` is run by hand. The changeset states the
same rule.
- **Review N2 is an accepted nit, not pushed.** It asks for that
consequence as one more sentence in `cli.mdx`. The head c6ff87c is
green and holds a PASS record, and the note above and the changeset
already carry the rule, so a docs-only push would only restart review.
- **Uninstall then reinstall.** The recorded set only grows, so a
reinstall does not re-walk a roll-up this process already recorded.
## Verification
<details><summary>Derived gate union at c6ff87c: dispatch-gates
--commands, 106 families</summary>
- 101 exited 0.
- 5 exited 3, PREREQUISITE NOT MET, because this worktree's install was
filtered to the objectql closure: `check:type-check-debt`,
`check:dual-build-cjs-loads`, `check:doc-frontmatter` (no `apps/docs`
install), `spec check:skill-examples`, and `check:lean-entry-closure`
(objectql's dist was not yet rebuilt when it ran). NOT MEASURED locally;
CI measures them.
- The derivation flagged its tree as stale: 17 commits behind
origin/main, and one derived-from script changed there
(`scripts/check-facade-delegation-only.mjs`). CI runs the current
script.
- Gates that read the touched files, all exit 0:
`check:test-source-alias`, `check:facade-delegation-only`,
`check:durability-log-level`, `check:system-context-census`,
`check:dts-closure`, `check:doc-authoring`, `check:nul-bytes`,
`check:changeset-no-major`, `check:adr-0087-registration`.
- CI check-runs on c6ff87c: 38 success, 15 skipped.
</details>
Not run locally, and declared to CI: `test:repo`, and the consumer
suites of `syncSchemas` (`@objectstack/cloud-connection`; none of its
tests declares a summary field).
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- signed: domain:server#5 · zhuangjianguo ·
session_01QsR2jUg7BKfRBcTPyMxbLh -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 7d06248 commit 803f909
9 files changed
Lines changed: 715 additions & 2 deletions
File tree
- .changeset
- content/docs/deployment
- packages/objectql/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1679 | 1679 | | |
1680 | 1680 | | |
1681 | 1681 | | |
| 1682 | + | |
| 1683 | + | |
| 1684 | + | |
| 1685 | + | |
| 1686 | + | |
| 1687 | + | |
| 1688 | + | |
| 1689 | + | |
| 1690 | + | |
| 1691 | + | |
| 1692 | + | |
| 1693 | + | |
| 1694 | + | |
| 1695 | + | |
| 1696 | + | |
| 1697 | + | |
| 1698 | + | |
| 1699 | + | |
| 1700 | + | |
| 1701 | + | |
| 1702 | + | |
| 1703 | + | |
| 1704 | + | |
1682 | 1705 | | |
1683 | 1706 | | |
1684 | 1707 | | |
| |||
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
| 74 | + | |
| 75 | + | |
74 | 76 | | |
75 | 77 | | |
76 | 78 | | |
| |||
120 | 122 | | |
121 | 123 | | |
122 | 124 | | |
123 | | - | |
| 125 | + | |
124 | 126 | | |
125 | 127 | | |
126 | 128 | | |
| |||
187 | 189 | | |
188 | 190 | | |
189 | 191 | | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
190 | 198 | | |
191 | 199 | | |
192 | 200 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
146 | 146 | | |
147 | 147 | | |
148 | 148 | | |
| 149 | + | |
149 | 150 | | |
150 | 151 | | |
151 | 152 | | |
| |||
4101 | 4102 | | |
4102 | 4103 | | |
4103 | 4104 | | |
| 4105 | + | |
| 4106 | + | |
| 4107 | + | |
| 4108 | + | |
| 4109 | + | |
| 4110 | + | |
| 4111 | + | |
| 4112 | + | |
| 4113 | + | |
| 4114 | + | |
| 4115 | + | |
| 4116 | + | |
| 4117 | + | |
| 4118 | + | |
| 4119 | + | |
| 4120 | + | |
| 4121 | + | |
| 4122 | + | |
| 4123 | + | |
| 4124 | + | |
| 4125 | + | |
| 4126 | + | |
| 4127 | + | |
4104 | 4128 | | |
4105 | 4129 | | |
4106 | 4130 | | |
| |||
5565 | 5589 | | |
5566 | 5590 | | |
5567 | 5591 | | |
| 5592 | + | |
5568 | 5593 | | |
5569 | 5594 | | |
5570 | 5595 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
633 | 633 | | |
634 | 634 | | |
635 | 635 | | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
636 | 639 | | |
637 | 640 | | |
638 | 641 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1382 | 1382 | | |
1383 | 1383 | | |
1384 | 1384 | | |
| 1385 | + | |
| 1386 | + | |
| 1387 | + | |
| 1388 | + | |
| 1389 | + | |
| 1390 | + | |
1385 | 1391 | | |
1386 | 1392 | | |
1387 | 1393 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
195 | 195 | | |
196 | 196 | | |
197 | 197 | | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
198 | 210 | | |
199 | 211 | | |
200 | 212 | | |
| |||
326 | 338 | | |
327 | 339 | | |
328 | 340 | | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
329 | 344 | | |
330 | 345 | | |
331 | 346 | | |
| |||
412 | 427 | | |
413 | 428 | | |
414 | 429 | | |
415 | | - | |
| 430 | + | |
416 | 431 | | |
417 | 432 | | |
418 | 433 | | |
| |||
0 commit comments