Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .changeset/39393-stored-form-switch-drops-data.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
'@objectstack/metadata-protocol': patch
'@objectstack/cli': patch
---

fix(cli): `os migrate protocol --to 19 --apply` writes each moved stored form without the old `data` binding, and never switches over a form the runtime would serve invalid

Clause-②: no
Metadata-protocol: none

Who sees it: an administrator upgrading a deployment whose 18.x database holds forms edited in Studio (a view's `form` or `formViews` saved with the view).

- **A moved form no longer keeps `data`.** The protocol-19 switch moved each stored form "with its body as written", and an 18.x Studio save stores a form's object binding as `data: { provider: 'object', object }`. A `form` item refuses `data` by name, so every moved form was served `valid: false` and the `form` door refused the same body. The switch now drops a plain object binding, as `os migrate meta --from 18 --write` does for a form in source; the rest of the body moves as written.
- **No switch over a form the `form` schema refuses.** The census (`os migrate stored-forms`, and the preview of `os migrate protocol --to 19`) now judges every form body the switch would write, and every stored `form` row it keeps, with the runtime's own schema. A body it refuses is listed with the site, the schema's words and the action; `os migrate protocol --to 19` then exits 1 and writes nothing, and `--apply` records no switch while one stands. `--json` carries the list as `bodyRefusals`, and `canSwitch` is `false` beside it. This includes a form served under a name the protocol-19 naming rule refuses, such as a form a container serves as `OBJECT.CONTAINER.KEY` when the container's package (none, a customization package, or any other) is not the package that owns the object: the switch renames no form, so it no longer writes one there.
- **The fix names the package.** For a stored `form` row served invalid, the action is `PUT /api/v1/meta/form/NAME?package=PACKAGE`: the save door addresses a package's row only with `?package=`. `os migrate meta --stored` does the same: its fix line for any row saved in a package (every type) now reads `PUT /api/v1/meta/TYPE/NAME?package=PACKAGE`, where it printed a door that refused the save.
- **A form whose name cannot move says what can be done.** Such a form is refused rather than renamed. Its action names the remedy that exists on the release you are running: delete the container, re-create each of its forms as its own form named `OBJECT.KEY`, point anything that opened the old name at the new one, and run the upgrade again.

A deployment that already switched on `19.0.0-next.4` and holds moved forms with `data`: run `os migrate stored-forms` to list them, and save each again without `data` through the door it names.
65 changes: 53 additions & 12 deletions packages/cli/src/commands/migrate/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ import { describeUnloadableHostConfig } from '../../utils/schema-migration-plugi
import { absentTableReads } from '../../utils/absent-table-reads.js';
import { OCCUPANCY_STOP_HINT, probeMigrationTarget } from '../../utils/migrate-occupancy-gate.js';
import { describeOccupancy, JOURNAL_MODE_SWITCH_NOTE } from '../../utils/sqlite-occupancy.js';
import { storedFormAddressText, storedFormPlanText, storedFormRowText, storedFormRunRefusal } from './stored-forms.js';
import {
storedFormAddressText,
storedFormBodyRefusals,
storedFormBodyRefusalSummary,
storedFormBodyRefusalText,
storedFormPlanText,
storedFormRowText,
storedFormRunRefusal,
} from './stored-forms.js';

/** The protocol majors this command switches a deployment to. */
const SWITCH_MAJORS = [19] as const;
Expand All @@ -49,13 +57,21 @@ export function protocolHolds(census: StoredFormCensus): StoredFormHold[] {
}

/**
* Whether a census verifies the switch: the precheck lets it switch, and
* nothing is left to move or remove (a ceremony that committed leaves every
* moved form a stored `form` row, which the census reads as kept).
* Whether a census verifies the switch: the precheck lets it switch, nothing
* is left to move or remove (a ceremony that committed leaves every moved form
* a stored `form` row, which the census reads as kept), and no form row it
* keeps is one the protocol-19 `form` schema refuses (ADR-0159 D2): a switch
* is never recorded over a form the runtime serves invalid.
*/
export function censusVerifiesSwitch(census: StoredFormCensus): boolean {
const { canSwitch, counts } = census.precheck;
return canSwitch && counts.move === 0 && counts.remove === 0 && counts.hold === 0;
return canSwitch && counts.move === 0 && counts.remove === 0 && counts.hold === 0 && storedFormBodyRefusals(census).length === 0;
}

/** The refusal when a form body the protocol-19 `form` schema refuses stands: nothing is written. */
export function protocolBodyRefusalText(to: number, census: StoredFormCensus): string {
return `The deployment does not switch to protocol ${to}: ${storedFormBodyRefusalSummary(storedFormBodyRefusals(census))} `
+ `Take each action, then run \`os migrate protocol --to ${to}\` again. Nothing was written.`;
}

/** The refusal sentence when the deployment cannot switch, naming where each action is taken. */
Expand Down Expand Up @@ -127,6 +143,13 @@ async function confirm(question: string): Promise<boolean> {
* is taken on (the one running now). `--json` carries the same holds. No new
* protocol error code: the refusal is the command's.
*
* A form body the protocol-19 `form` schema refuses refuses the switch too,
* before anything is written (ADR-0159 D2): one the ceremony would write, or a
* stored `form` row the census keeps, which the runtime serves invalid. Each is
* listed with the site, the schema's words and the action, whose door names
* the package the row is saved in (`?package=`). A switch is never recorded
* over one.
*
* `os migrate stored-forms` stays the read-only look and has no `--apply`.
*
* Exit status: 0 when the preview finds a deployment that can switch, or when
Expand Down Expand Up @@ -282,7 +305,8 @@ export default class MigrateProtocol extends Command {
if (!apply) {
const holds = protocolHolds(before);
const runRefused = storedFormRunRefusal(before, (protocol as { engine?: unknown }).engine);
const canSwitch = before.precheck.canSwitch && runRefused === undefined;
const bodyRefusals = storedFormBodyRefusals(before);
const canSwitch = before.precheck.canSwitch && runRefused === undefined && bodyRefusals.length === 0;
if (json) {
reads.notice(true);
await emitJson({
Expand All @@ -291,6 +315,7 @@ export default class MigrateProtocol extends Command {
...(runRefused !== undefined ? { runRefused } : {}),
counts: before.precheck.counts,
holds,
bodyRefusals,
forms: before.forms,
drafts: before.drafts,
sharedSlugs: before.sharedSlugs,
Expand All @@ -309,6 +334,10 @@ export default class MigrateProtocol extends Command {
printWarning(protocolRefusalText(to, holds));
return 1;
}
if (bodyRefusals.length > 0) {
printWarning(protocolBodyRefusalText(to, before));
return 1;
}
printSuccess(`The deployment can switch to protocol ${to}. Run \`os migrate protocol --to ${to} --apply\` to switch ${chalk.dim(`(${timer.display()})`)}`);
return 0;
}
Expand All @@ -318,12 +347,13 @@ export default class MigrateProtocol extends Command {
// roll back are refused here, before the ceremony is asked.
const engine = (protocol as { engine?: unknown }).engine;
const runRefused = storedFormRunRefusal(before, engine);
if (!before.precheck.canSwitch || runRefused !== undefined) {
const bodyRefusals = storedFormBodyRefusals(before);
if (!before.precheck.canSwitch || runRefused !== undefined || bodyRefusals.length > 0) {
const holds = protocolHolds(before);
const message = runRefused ?? protocolRefusalText(to, holds);
const message = runRefused ?? (before.precheck.canSwitch ? protocolBodyRefusalText(to, before) : protocolRefusalText(to, holds));
if (json) {
reads.notice(true);
await emitJson({ ...header, switched: false, canSwitch: false, refusal: message, counts: before.precheck.counts, holds, duration: timer.elapsed() });
await emitJson({ ...header, switched: false, canSwitch: false, refusal: message, counts: before.precheck.counts, holds, bodyRefusals, duration: timer.elapsed() });
return 1;
}
this.printPreamble(stack, reads);
Expand Down Expand Up @@ -358,17 +388,19 @@ export default class MigrateProtocol extends Command {
// The ceremony committed but the re-run still finds work: no marker.
// The commit cannot be undone here, so the message says what it left.
const holds = protocolHolds(after);
const afterRefusals = storedFormBodyRefusals(after);
const message = `The stored forms have already moved: the transaction committed, and the rows listed above are form rows now. `
+ `The census run again still finds ${after.precheck.counts.move} to move, ${after.precheck.counts.remove} to remove `
+ `and ${holds.length} holding the switch, so the switch was not recorded. Do not start this deployment on the release `
+ `The census run again still finds ${after.precheck.counts.move} to move, ${after.precheck.counts.remove} to remove, `
+ `${holds.length} holding the switch and ${afterRefusals.length} form row(s) the form schema refuses, so the switch was not recorded. Do not start this deployment on the release `
+ 'it was running: that release reads a form only from a view row, so it cannot read a moved form or the switch-off it '
+ `carries. Take each action listed, then run \`os migrate protocol --to ${to} --apply --yes\` again until it records the switch.`;
if (json) {
await emitJson({ ...header, switched: false, moved: true, canSwitch: after.precheck.canSwitch, refusal: message, counts: after.precheck.counts, holds, ceremony: ceremonyJson(result), duration: timer.elapsed() });
await emitJson({ ...header, switched: false, moved: true, canSwitch: after.precheck.canSwitch, refusal: message, counts: after.precheck.counts, holds, bodyRefusals: afterRefusals, ceremony: ceremonyJson(result), duration: timer.elapsed() });
return 1;
}
this.printCeremony(result);
this.printHolds(holds);
this.printBodyRefusals(afterRefusals);
printError(message);
return 1;
}
Expand Down Expand Up @@ -428,13 +460,22 @@ export default class MigrateProtocol extends Command {
for (const d of census.drafts) console.log(` • ${d.type} "${d.name}", ${d.packageId !== undefined ? `package '${d.packageId}'` : 'no package'}`);
console.log('');
}
this.printBodyRefusals(storedFormBodyRefusals(census));
for (const shared of census.sharedSlugs) {
printInfo(`/forms/${shared.slug} is declared by ${shared.forms.map((f) => `'${f}'`).join(' and ')}.`);
}
const c = census.precheck.counts;
printInfo(`${c.keep} kept, ${c.move} to move, ${c.remove} to remove, ${c.left_in_place} left in place, ${census.precheck.holds.length} holding the switch.`);
}

/** Each form body the protocol-19 `form` schema refuses, with its site and the action for it (ADR-0159 D2). */
private printBodyRefusals(refusals: ReturnType<typeof storedFormBodyRefusals>): void {
if (refusals.length === 0) return;
console.log(chalk.bold(`${refusals.length} form body(ies) the protocol-19 form schema refuses:`));
for (const refusal of refusals) console.log(` • ${storedFormBodyRefusalText(refusal)}`);
console.log('');
}

/** What the ceremony wrote and removed: each moved row, and each removed form with its body. */
private printCeremony(result: StoredFormApplyResult | undefined): void {
const c = ceremonyJson(result);
Expand Down
26 changes: 25 additions & 1 deletion packages/cli/src/commands/migrate/stored-forms.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.

import { describe, expect, it, vi } from 'vitest';
// Paid during collection, not inside a case's clocked window (check:test-source-alias):
// `censusVerifiesSwitch`'s module reaches it through a dynamic import.
import '@objectstack/spec/migrations';
import type { StoredFormAddressPlan, StoredFormCensus } from '@objectstack/metadata-protocol';
import { storedFormRunRefusal } from './stored-forms.js';
import { storedFormBodyRefusalSummary, storedFormBodyRefusalText, storedFormRunRefusal } from './stored-forms.js';

// The report's sentence is read alone: no stack is booted here, so nothing the
// command loads to boot one is loaded (each package the boot reaches is mocked).
Expand Down Expand Up @@ -49,3 +52,24 @@ describe('[ADR-0159 D7] the census report says where the upgrade\'s run will be
expect(storedFormRunRefusal(census([plan('move')], false), engine(false))).toBeUndefined();
});
});

describe('[ADR-0159 D2] the census is never green over a form body the protocol-19 `form` schema refuses', () => {
const refusal = (writes: boolean) => ({
form: 'crm_lead.intake', address: { kind: 'environment' }, outcome: writes ? 'move' : 'keep', writes, row: ROW,
path: '', message: 'Unrecognized key(s) on this form: data', action: 'PUT /api/v1/meta/form/crm_lead.intake?package=com.acme.crm.custom',
}) as unknown as NonNullable<StoredFormCensus['bodyRefusals']>[number];
const kept = (bodyRefusals?: StoredFormCensus['bodyRefusals']): StoredFormCensus => ({ ...census([plan('keep')]), ...(bodyRefusals ? { bodyRefusals } : {}) });

it('a kept form row the schema refuses keeps the switch from being recorded (ablate: drop the verdict from censusVerifiesSwitch)', async () => {
const { censusVerifiesSwitch } = await import('./protocol.js');
expect(censusVerifiesSwitch(kept([refusal(false)]))).toBe(false);
expect(storedFormBodyRefusalSummary([refusal(false)])).toContain('1 stored form row(s)');
expect(storedFormBodyRefusalText(refusal(false))).toContain('?package=com.acme.crm.custom');
});

it('CONTROL: the same census with no refused body verifies the switch', async () => {
const { censusVerifiesSwitch } = await import('./protocol.js');
expect(censusVerifiesSwitch(kept([]))).toBe(true);
expect(censusVerifiesSwitch(kept())).toBe(true);
});
});
48 changes: 44 additions & 4 deletions packages/cli/src/commands/migrate/stored-forms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,31 @@ export function storedFormRunRefusal(census: StoredFormCensus, engine: unknown):
+ 'Run the upgrade against a database whose driver supports transactions, or stay on the release you are running.';
}

/** A body the protocol-19 `form` schema refuses, as the census reports it (ADR-0159 D2). */
export type StoredFormBodyRefusal = NonNullable<StoredFormCensus['bodyRefusals']>[number];

/** The census's verdict on the bodies it plans to write or keeps: every one the `form` schema refuses. */
export function storedFormBodyRefusals(census: StoredFormCensus): readonly StoredFormBodyRefusal[] {
return census.bodyRefusals ?? [];
}

/** One refused body: the row, the site the schema names, its words, and the action (which names the package's door). */
export function storedFormBodyRefusalText(refusal: StoredFormBodyRefusal): string {
const verb = refusal.outcome === 'move' ? 'would be written as a form row the protocol-19 form schema refuses' : 'is a form row the protocol-19 form schema refuses, so it is served invalid';
return `${storedFormRowText(refusal.row)}: '${refusal.form}' ${verb}, at ${refusal.path || '(root)'}: ${refusal.message} ${refusal.action}`;
}

/** The sentence beneath the refused bodies: the upgrade does not run, or is not clean, while one stands. */
export function storedFormBodyRefusalSummary(refusals: readonly StoredFormBodyRefusal[]): string {
const writes = refusals.filter((r) => r.writes).length;
const served = refusals.length - writes;
return [
writes > 0 ? `${writes} form(s) the upgrade would write are refused by the protocol-19 form schema, so the upgrade's run writes nothing while they stand.` : '',
served > 0 ? `${served} stored form row(s) are served invalid today.` : '',
'Each is listed above with the action for it.',
].filter((part) => part !== '').join(' ');
}

/** One row as the report names it. */
export function storedFormRowText(row: StoredFormRowRef): string {
const where = row.packageId !== undefined ? `package '${row.packageId}'` : 'no package';
Expand Down Expand Up @@ -181,9 +206,14 @@ function holdText(hold: StoredFormHold): string {
* `serve`'s two flags that move that composition, with its meaning. The boot
* is read-only (deferred DDL, no seed, no file brought into existence).
*
* Exit status: 0 when the census ran, whatever it found (`precheck.canSwitch`
* in `--json` says whether the upgrade would switch); 1 when the composition
* could not be read or booted.
* A form body the protocol-19 `form` schema refuses is listed with its site,
* the schema's words and the action (`bodyRefusals`): one the upgrade would
* write refuses the upgrade's run, and a stored `form` row the census keeps is
* served invalid today. The report is never green over one (ADR-0159 D2).
*
* Exit status: 0 when the census ran, whatever it found (`canSwitch` in
* `--json` says whether the upgrade would switch, a refused body included); 1
* when the composition could not be read or booted.
*/
export default class MigrateStoredForms extends Command {
static override description =
Expand Down Expand Up @@ -284,16 +314,18 @@ export default class MigrateStoredForms extends Command {
const census = reads.absent('sys_metadata') ? EMPTY : await listStoredFormsOf(protocol as Parameters<typeof listStoredFormsOf>[0]);
const { precheck } = census;
const runRefused = storedFormRunRefusal(census, (protocol as { engine?: unknown }).engine);
const bodyRefusals = storedFormBodyRefusals(census);

if (json) {
reads.notice(true);
await emitJson({
database: stack.dbLabel,
hostConfig: stack.composition.hostConfigPath,
canSwitch: precheck.canSwitch,
canSwitch: precheck.canSwitch && bodyRefusals.length === 0,
...(runRefused !== undefined ? { runRefused } : {}),
counts: precheck.counts,
holds: precheck.holds,
bodyRefusals,
forms: census.forms,
unreadable: census.unreadable,
drafts: census.drafts,
Expand Down Expand Up @@ -326,13 +358,21 @@ export default class MigrateStoredForms extends Command {
for (const d of census.drafts) console.log(` • ${d.type} "${d.name}", ${d.packageId !== undefined ? `package '${d.packageId}'` : 'no package'}`);
console.log('');
}
if (bodyRefusals.length > 0) {
console.log(chalk.bold(`${bodyRefusals.length} form body(ies) the protocol-19 form schema refuses:`));
for (const refusal of bodyRefusals) console.log(` • ${storedFormBodyRefusalText(refusal)}`);
console.log('');
}
for (const shared of census.sharedSlugs) {
printInfo(`/forms/${shared.slug} is declared by ${shared.forms.map((f) => `'${f}'`).join(' and ')}.`);
}
const c = precheck.counts;
printInfo(`${c.keep} kept, ${c.move} to move, ${c.remove} to remove, ${c.left_in_place} left in place, ${precheck.holds.length} holding the upgrade.`);
if (runRefused !== undefined) {
printWarning(runRefused);
} else if (bodyRefusals.length > 0) {
// Never green over a body the runtime would serve invalid (ADR-0159 D2).
printWarning(storedFormBodyRefusalSummary(bodyRefusals));
} else if (precheck.canSwitch) {
printSuccess(`The upgrade can switch: no stored row holds it ${chalk.dim(`(${timer.display()})`)}`);
} else {
Expand Down
Loading
Loading