Skip to content

Commit da20f30

Browse files
committed
wip(runtime): residual fixtures name their organization or model a pre-18 store
Claude-Session: https://claude.ai/code/session_018EszgtA3V2KXBX5tf59Hi4 Co-authored-by: Claude <noreply@anthropic.com>
1 parent a0dbaa9 commit da20f30

11 files changed

Lines changed: 77 additions & 4 deletions

‎packages/runtime/src/action-engine-facade-find-envelope.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import { assertEngineDeleteDispatch } from '@objectstack/metadata-core';
4646
import { ObjectQL } from '@objectstack/objectql';
4747
import { SqliteWasmDriver } from '@objectstack/driver-sqlite-wasm';
4848
import { buildActionEngineFacade, ACTION_ENGINE_FIND_ENVELOPE_PRESCRIPTION } from './action-execution.js';
49+
import { installDefaultOrganization } from './default-organization.testkit.js';
4950

5051
const deps: any = { resolveService: () => undefined, getObjectQL: async () => undefined };
5152

@@ -201,6 +202,9 @@ async function makeRealEngine() {
201202
await engine.init();
202203
engine.registry.registerObject(PROBE_OBJECT, 'test');
203204
await engine.syncSchemas?.();
205+
// A real `single` install's Default Organization: the system writes below
206+
// derive it (ADR-0131 D9), as a fresh NOT NULL tenant column requires (D1).
207+
await installDefaultOrganization(engine);
204208

205209
const ctx = { isSystem: true } as any;
206210
// t3 carries a non-null `deleted_at`, so a filter that is HONOURED returns

‎packages/runtime/src/datasource-mapping-before-schema-sync.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ function platformPlugin() {
104104
});
105105
},
106106
start: async (ctx: any) => {
107-
await ctx.getService('objectql').insert(PLATFORM_OBJECT, { title: BOOT_ROW }, { context: { isSystem: true } });
107+
// Written in an organization: a fresh tenant column is NOT NULL (ADR-0131 D1).
108+
await ctx.getService('objectql').insert(PLATFORM_OBJECT, { title: BOOT_ROW }, { context: { isSystem: true, tenantId: 'org_a' } });
108109
},
109110
};
110111
}

‎packages/runtime/src/default-datasource-plugin.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ describe('DefaultDatasourcePlugin — the default datasource as a declaration (#
193193
expect(defaultName).toBeDefined();
194194
// Identity, not equivalence: the engine's default driver IS the host's instance.
195195
expect(engine.getDriverByName?.(defaultName!)).toBe(hostBuilt);
196-
await engine.insert('note', { title: 'through-the-adopted-default' });
196+
// Written in an organization: a fresh tenant column is NOT NULL (ADR-0131 D1).
197+
await engine.insert('note', { title: 'through-the-adopted-default' }, { context: { tenantId: 'org_a' } } as never);
197198
const rows = await engine.find('note');
198199
expect(rows.map((r: any) => r.title)).toContain('through-the-adopted-default');
199200
// [commit 13a6cb4ad] The capture is a PIN, not a mute: the probe's two log lines

‎packages/runtime/src/default-organization-statements.golden.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { PlatformObjectsPlugin } from '@objectstack/platform-objects/plugin';
3737
import { AppPlugin } from './app-plugin.js';
3838
import { DefaultDatasourcePlugin } from './default-datasource-plugin.js';
3939
import { createDispatcherPlugin } from './dispatcher-plugin.js';
40+
import { relaxTenantColumns } from './pre-18-tenant-column.testkit.js';
4041

4142
const BOOT_TIMEOUT = 180_000;
4243
const ORIGIN = 'http://localhost:3000';
@@ -170,6 +171,10 @@ describe('[#24523] statements per warm request: an organization-owned admin gran
170171
});
171172

172173
it('CONTROL — the organization-less grant: the same counts, on either slug', async () => {
174+
// An organization-less grant exists only on a store built before 18.x
175+
// (ADR-0131 D1 makes a fresh tenant column NOT NULL): this table is that
176+
// store's, relaxed through its driver's own drift apply door.
177+
await relaxTenantColumns((engine as any).getDriver('sys_user_permission_set'), ['sys_user_permission_set']);
173178
const grant = await adminGrant();
174179
const [organization] = await find('sys_organization');
175180
await engine.update('sys_user_permission_set', { id: grant.id, organization_id: null }, { context: SYS });
@@ -188,6 +193,17 @@ describe('[#24523] statements per warm request: an organization-owned admin gran
188193

189194
describe('[#24523 / #21868] a sys_organization write retires the memoised Default Organization', () => {
190195
it('⭐ the Default Organization deleted and recreated: the next request reads the new id', async () => {
196+
// Deleting an organization runs the engine's referential cascade, whose
197+
// `set_null` on every `organization_id` lookup a fresh, NOT NULL tenant
198+
// column refuses (ADR-0131 D1). This test's subject is the memo, not the
199+
// cascade, so the store is a pre-18 one: every tenant table it holds is
200+
// relaxed through its driver's own drift apply door.
201+
for (const o of (engine as any).registry.getAllObjects() as Array<{ name: string; fields?: Record<string, unknown>; tenancy?: { enabled?: boolean } }>) {
202+
if (!o.fields?.organization_id || o.tenancy?.enabled === false) continue;
203+
const driver = (engine as any).getDriver(o.name);
204+
if (!(await driver?.knex?.schema.hasTable(o.name))) continue;
205+
await relaxTenantColumns(driver, [o.name]);
206+
}
191207
const [old] = await find('sys_organization', { slug: 'default' });
192208
await standing(); // the control above wrote the organization: read it once
193209
expect(await standing()).toEqual({ standing: true, organizationReads: 0 });

‎packages/runtime/src/notification-schema-conformance.integration.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import type { ServiceObject } from '@objectstack/spec/data';
5050
import { createDispatcherPlugin } from './dispatcher-plugin.js';
5151
import { DriverPlugin } from './driver-plugin.js';
5252
import { captureExpectedReadRefusals } from './expected-read-refusal-noise.js';
53+
import { installDefaultOrganization } from './default-organization.testkit.js';
5354

5455
// One inbox per concern. The mark-read routes MUTATE read-state, so a shared
5556
// user would make these suites order-dependent — the unread fixture the gap
@@ -311,6 +312,9 @@ describe('[#5792] the notification wire bodies conform to the schemas the catalo
311312
authzEngine.registerObject(o.def, o.owner);
312313
await authzEngine.syncObjectSchema(o.def.name);
313314
}
315+
// A real `single` install's Default Organization: the system writes below
316+
// derive it (ADR-0131 D9), as a fresh NOT NULL tenant column requires (D1).
317+
await installDefaultOrganization(authzEngine);
314318

315319
const httpServer = kernel.getService<IHttpServer>('http.server');
316320
baseUrl = `http://127.0.0.1:${httpServer.getPort!()}`;

‎packages/runtime/src/notifications.hono.integration.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { DriverPlugin } from './driver-plugin.js';
1313
import { captureExpectedReadRefusals } from './expected-read-refusal-noise.js';
1414
import type { IHttpServer } from '@objectstack/spec/contracts';
1515
import type { ServiceObject } from '@objectstack/spec/data';
16+
import { installDefaultOrganization } from './default-organization.testkit.js';
1617

1718
/**
1819
* End-to-end regression for framework #3362 (`#3354 not effective on hono`).
@@ -242,6 +243,9 @@ describe('in-app notifications over a real hono server (integration, #3362)', ()
242243
authzEngine.registerObject(o.def, o.owner);
243244
await authzEngine.syncObjectSchema(o.def.name);
244245
}
246+
// A real `single` install's Default Organization: the system writes below
247+
// derive it (ADR-0131 D9), as a fresh NOT NULL tenant column requires (D1).
248+
await installDefaultOrganization(authzEngine);
245249

246250
const httpServer = kernel.getService<IHttpServer>('http.server');
247251
baseUrl = `http://127.0.0.1:${httpServer.getPort!()}`;

‎packages/runtime/src/package-uninstall-shared-object.integration.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ async function boot() {
201201
async records(name: string) {
202202
return ((await knex(name).select('title')) as Array<{ title: string }>).map((r) => r.title);
203203
},
204-
insertRecord: (name: string, title: string) => engine.insert(name, { title }, { context: { isSystem: true } } as any),
204+
// Written in an organization: a fresh tenant column is NOT NULL (ADR-0131 D1).
205+
insertRecord: (name: string, title: string) => engine.insert(name, { title }, { context: { isSystem: true, tenantId: 'org_a' } } as any),
205206
};
206207
}
207208

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* Return driver-created tables to the shape a store built BEFORE 18.x has:
5+
* the tenant column nullable.
6+
*
7+
* ADR-0131 D1: since 18.x the SQL driver creates a table's tenant column NOT
8+
* NULL. A store an earlier release built still holds the nullable column and
9+
* its organization-less legacy rows (until the organization-ownership
10+
* ceremony attributes them, or reports them). A suite whose subject is those
11+
* rows — a backfill over them, a sweep that must still reach them — builds
12+
* its tables, then calls this before seeding them. Done through the driver's
13+
* own drift apply door (`relax_not_null`), never a hand `ALTER`.
14+
*/
15+
export async function relaxTenantColumns(
16+
driver: { applyMigrationEntries(entries: never[]): Promise<{ applied: unknown[] }> },
17+
tables: readonly string[],
18+
column = 'organization_id',
19+
): Promise<void> {
20+
for (const table of tables) {
21+
const op = { type: 'relax_not_null', table, column };
22+
const { applied } = await driver.applyMigrationEntries([
23+
{ kind: 'nullability_mismatch', remoteName: table, table, column, category: 'needs_confirm', op } as never,
24+
]);
25+
if (applied.length !== 1) throw new Error(`relaxTenantColumns: ${table}.${column} was not relaxed`);
26+
}
27+
}

‎packages/runtime/src/seed-loader-null-organization-holder-real-driver.integration.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { SqlDriver } from '@objectstack/driver-sql';
4242
import { SeedLoaderRequestSchema, type EngineQueryOptions } from '@objectstack/spec/data';
4343
import type { PluginContext } from '@objectstack/core';
4444
import { AppPlugin } from './app-plugin';
45+
import { relaxTenantColumns } from './pre-18-tenant-column.testkit.js';
4546

4647
const ORG = 'org_twdefaultk3x9qfa2';
4748
const OTHER_ORG = 'org_twotherorgp4w8m2';
@@ -125,6 +126,9 @@ async function boot() {
125126
await engine.init();
126127
for (const o of OBJECTS) engine.registry.registerObject(o as any, 'com.objectstack.test.24301');
127128
await raw.initObjects(OBJECTS.map((o) => engine.registry.getObject(o.name) ?? o) as any);
129+
// The 17.x residue this file plants lands only on a store built before 18.x,
130+
// whose tenant columns are nullable (ADR-0131 D1 makes a fresh one NOT NULL).
131+
await relaxTenantColumns(raw as any, OBJECTS.filter((o) => (engine.registry.getObject(o.name) as any)?.fields?.organization_id).map((o) => o.name));
128132
cleanups.push(() => engine.destroy());
129133

130134
const systemRead: EngineQueryOptions = { context: { isSystem: true } };

‎packages/runtime/src/seed-tenancy-autonumber-split.integration.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import {
6060
resolveSeedTenancySeam,
6161
GLOBAL_TENANT,
6262
} from '@objectstack/metadata-protocol';
63+
import { relaxTenantColumns } from './pre-18-tenant-column.testkit.js';
6364

6465
const ORG_ID = 'org_mssymr19xzd645gv';
6566
const SEEDED_ROWS = 38;
@@ -113,6 +114,9 @@ async function bootInstall() {
113114
engine.registry.registerObject(CASE_OBJECT, '#8686');
114115
engine.registry.registerObject(ORG_OBJECT, '#8686');
115116
await driver.initObjects([CASE_OBJECT, ORG_OBJECT]);
117+
// The pre-ADR-0131 residue lands only on a store built before 18.x, whose
118+
// tenant column is nullable (D1 makes a fresh one NOT NULL): this is that store.
119+
await relaxTenantColumns(driver as any, [CASE_OBJECT.name]);
116120
return { driver, engine };
117121
}
118122

@@ -482,6 +486,9 @@ describe('#8686 seed/API tenancy split — autonumber scope', () => {
482486
engine.registry.registerObject(ticketObject, '#12394');
483487
engine.registry.registerObject(ORG_OBJECT, '#12394');
484488
await driver.initObjects([ticketObject, ORG_OBJECT]);
489+
// The pre-ADR-0131 residue lands only on a store built before 18.x, whose
490+
// tenant column is nullable (D1 makes a fresh one NOT NULL): this is that store.
491+
await relaxTenantColumns(driver as any, [ticketObject.name]);
485492

486493
// Seed-shaped writes: untenanted, because no organization existed yet —
487494
// the pre-ADR-0131 residue, landed through the driver call the engine made
@@ -550,6 +557,9 @@ describe('#8686 seed/API tenancy split — autonumber scope', () => {
550557
engine.registry.registerObject(sysObject, '#8686');
551558
engine.registry.registerObject(ORG_OBJECT, '#8686');
552559
await driver.initObjects([sysObject, ORG_OBJECT]);
560+
// The pre-ADR-0131 residue lands only on a store built before 18.x, whose
561+
// tenant column is nullable (D1 makes a fresh one NOT NULL): this is that store.
562+
await relaxTenantColumns(driver as any, [sysObject.name]);
553563

554564
// Land untenanted platform rows, then bring an organization into existence.
555565
// [C8 stage 8b] Straight through the driver, as `landPreC1SeedResidue` does:

0 commit comments

Comments
 (0)