Skip to content

Commit 3bb0ddb

Browse files
committed
wip(plugin-auth): fixtures model a pre-18 store where they hold organization-less rows
Claude-Session: https://claude.ai/code/session_018EszgtA3V2KXBX5tf59Hi4 Co-authored-by: Claude <noreply@anthropic.com>
1 parent e36e855 commit 3bb0ddb

12 files changed

Lines changed: 111 additions & 0 deletions

‎packages/plugins/plugin-auth/src/account-identity-preflight.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import {
4646
SYS_ACCOUNT_OBJECT,
4747
} from './account-identity-preflight.js';
4848
import { authIdentityObjects } from './manifest.js';
49+
import { relaxEveryRegisteredTenantColumn } from './pre-18-tenant-column.testkit.js';
4950

5051
const SYSTEM = { context: { isSystem: true } } as never;
5152

@@ -105,6 +106,10 @@ async function bootEngine(objects: unknown[]): Promise<ObjectQL> {
105106
engine.registry.registerObject(object as never, '@objectstack/plugin-auth');
106107
}
107108
await engine.syncSchemas();
109+
// The organization-less rows below (global sets, grants, identities) exist
110+
// only on a store built before 18.x (ADR-0131 D1 makes a fresh tenant column
111+
// NOT NULL): this is that store.
112+
await relaxEveryRegisteredTenantColumn(engine);
108113
return engine;
109114
}
110115

‎packages/plugins/plugin-auth/src/driver-fault-auth-log-carriers.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import { ObjectQL } from '@objectstack/objectql';
4747
import { SqlDriver } from '@objectstack/driver-sql';
4848
import { AuthManager } from './auth-manager.js';
4949
import { authIdentityObjects } from './manifest.js';
50+
import { relaxEveryRegisteredTenantColumn } from './pre-18-tenant-column.testkit.js';
5051

5152
/** The caller's value. Synthetic; asserted ABSENT from every carrier. */
5253
const S = 'SENTINEL-21274-BOUND-VALUE';
@@ -252,6 +253,10 @@ for (const cell of CELLS) {
252253
engine.registry.registerObject(sysPermissionSet as never, '@objectstack/plugin-security');
253254
engine.registry.registerObject(sysUserPermissionSet as never, '@objectstack/plugin-security');
254255
await engine.syncSchemas();
256+
// The organization-less rows below (global sets, grants, identities) exist
257+
// only on a store built before 18.x (ADR-0131 D1 makes a fresh tenant column
258+
// NOT NULL): this is that store.
259+
await relaxEveryRegisteredTenantColumn(engine);
255260

256261
// The seeded holder of the sentinel, then the constraint the two writes
257262
// below will meet in the DATABASE — not in any check of ours.

‎packages/plugins/plugin-auth/src/grant-readers-by-name.golden.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { registerLastAdminGuard, type LastAdminGuardEngine } from './last-admin-
4242
import { ensureDefaultOrganization } from './ensure-default-organization.js';
4343
import { seedLegacyPlatformBucket } from './__tests__/legacy-catalog-rows.testkit.js';
4444
import { withLegacyPermissionSetTable } from './__tests__/legacy-catalog-objects.testkit.js';
45+
import { relaxEveryRegisteredTenantColumn } from './pre-18-tenant-column.testkit.js';
4546

4647
const SYS = { isSystem: true } as const;
4748
const POSTURE_ENV = 'OS_TENANCY_POSTURE';
@@ -134,6 +135,10 @@ async function standingByReader(posture: Posture): Promise<Record<string, unknow
134135
objects: withLegacyPermissionSetTable([SysUser, SysAccount, SysMember, SysOrganization, SysMetadataActivation, ...securityObjects]),
135136
} as any);
136137
await engine.syncSchemas();
138+
// The organization-less rows below (global sets, grants, identities) exist
139+
// only on a store built before 18.x (ADR-0131 D1 makes a fresh tenant column
140+
// NOT NULL): this is that store.
141+
await relaxEveryRegisteredTenantColumn(engine);
137142
await plugin.start(ctx);
138143
vi.spyOn((engine as any).logger, 'warn').mockImplementation(() => undefined);
139144
registerLastAdminGuard(engine as unknown as LastAdminGuardEngine, {

‎packages/plugins/plugin-auth/src/grant-readers-unnamed-grant.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { registerLastAdminGuard, type LastAdminGuardEngine } from './last-admin-
3636
import { ensureDefaultOrganization } from './ensure-default-organization.js';
3737
import { seedLegacyPlatformBucket } from './__tests__/legacy-catalog-rows.testkit.js';
3838
import { withLegacyPermissionSetTable } from './__tests__/legacy-catalog-objects.testkit.js';
39+
import { relaxEveryRegisteredTenantColumn } from './pre-18-tenant-column.testkit.js';
3940

4041
const SYS = { context: { isSystem: true } } as any;
4142
const GUARD_PACKAGE = 'test.grant-readers-unnamed';
@@ -113,6 +114,10 @@ async function soleGrantAnchoredAdmin(): Promise<Rig> {
113114
]),
114115
} as any);
115116
await engine.syncSchemas();
117+
// The organization-less rows below (global sets, grants, identities) exist
118+
// only on a store built before 18.x (ADR-0131 D1 makes a fresh tenant column
119+
// NOT NULL): this is that store.
120+
await relaxEveryRegisteredTenantColumn(engine);
116121
// [ADR-0131 D3/D5] The Default Organization a `single` boot creates before
117122
// `kernel:ready` (AuthPlugin's invariant); it owns the promotion's grant.
118123
await engine.insert('sys_organization', { id: 'org_default', name: 'Default Organization', slug: 'default' }, SYS);

‎packages/plugins/plugin-auth/src/last-admin-guard.activation-ledger.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { resetPlatformAdminEmailMemo } from '@objectstack/core';
2121

2222
import { registerLastAdminGuard, type LastAdminGuardEngine } from './last-admin-guard.js';
2323
import { bindTestSecurityCatalog } from './__tests__/security-catalog.testkit.js';
24+
import { relaxEveryRegisteredTenantColumn } from './pre-18-tenant-column.testkit.js';
2425

2526
const SYSTEM = { context: { isSystem: true } } as const;
2627
const text = { type: 'text' as const };
@@ -116,6 +117,10 @@ async function boot(opts: Boot = { permissions: [ADMIN_FULL_ACCESS] }): Promise<
116117
await engine.init();
117118
for (const o of objects) engine.registry.registerObject(o as never);
118119
await engine.syncSchemas();
120+
// The organization-less rows below (global sets, grants, identities) exist
121+
// only on a store built before 18.x (ADR-0131 D1 makes a fresh tenant column
122+
// NOT NULL): this is that store.
123+
await relaxEveryRegisteredTenantColumn(engine);
119124
const guardEngine = opts.readThrough?.(engine) ?? (engine as unknown as LastAdminGuardEngine);
120125
if (opts.permissions) {
121126
const catalog = { permissions: opts.permissions.map((name) => ({ name })) };

‎packages/plugins/plugin-auth/src/last-admin-guard.config-anchor.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import { resetPlatformAdminEmailMemo } from '@objectstack/core';
5252

5353
import { registerLastAdminGuard, USER_STANDING_KEYS, type LastAdminGuardEngine } from './last-admin-guard.js';
5454
import { bindTestSecurityCatalog } from './__tests__/security-catalog.testkit.js';
55+
import { relaxEveryRegisteredTenantColumn } from './pre-18-tenant-column.testkit.js';
5556

5657
const ENV = 'OS_PLATFORM_OWNER_EMAIL';
5758
/**
@@ -205,6 +206,10 @@ async function boot(opts: { unguarded?: boolean } = {}): Promise<ObjectQL> {
205206
engine.registry.registerObject(o as never);
206207
}
207208
await engine.syncSchemas();
209+
// The organization-less rows below (global sets, grants, identities) exist
210+
// only on a store built before 18.x (ADR-0131 D1 makes a fresh tenant column
211+
// NOT NULL): this is that store.
212+
await relaxEveryRegisteredTenantColumn(engine);
208213
// [ADR-0131 D3/D4] The set a grant names is the catalog's definition.
209214
bindTestSecurityCatalog(engine, { permissions: [{ name: ADMIN_FULL_ACCESS }] });
210215
if (!opts.unguarded) {

‎packages/plugins/plugin-auth/src/last-admin-guard.re-pricing.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import { resetPlatformAdminEmailMemo } from '@objectstack/core';
4040

4141
import { registerLastAdminGuard, type LastAdminGuardEngine } from './last-admin-guard.js';
4242
import { bindTestSecurityCatalog } from './__tests__/security-catalog.testkit.js';
43+
import { relaxEveryRegisteredTenantColumn } from './pre-18-tenant-column.testkit.js';
4344

4445
/** [ADR-0131 D3, ADR-0126 §4] The activation ledger a permission set is switched off in. */
4546
const sysMetadataActivation = {
@@ -172,6 +173,10 @@ async function boot(): Promise<ObjectQL> {
172173
engine.registry.registerObject(o as never);
173174
}
174175
await engine.syncSchemas();
176+
// The organization-less rows below (global sets, grants, identities) exist
177+
// only on a store built before 18.x (ADR-0131 D1 makes a fresh tenant column
178+
// NOT NULL): this is that store.
179+
await relaxEveryRegisteredTenantColumn(engine);
175180
// [ADR-0131 D3/D4] The set a grant names is the catalog's definition.
176181
bindTestSecurityCatalog(engine, { permissions: [{ name: ADMIN_FULL_ACCESS }] });
177182
registerLastAdminGuard(engine as unknown as LastAdminGuardEngine, { packageId: 'test.last-admin-guard-re-pricing' });

‎packages/plugins/plugin-auth/src/last-admin-guard.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import { bindTestSecurityCatalog } from './__tests__/security-catalog.testkit.js
6666
import { sqliteEnginePool, type BootedSqliteEngine } from './__tests__/sqlite-engine-pool.testkit.js';
6767
import { admin } from 'better-auth/plugins/admin';
6868
import { organization } from 'better-auth/plugins/organization';
69+
import { relaxEveryRegisteredTenantColumn } from './pre-18-tenant-column.testkit.js';
6970

7071
// ---------------------------------------------------------------------------
7172
// Fixtures
@@ -290,6 +291,10 @@ async function bootEngine(opts: BootOptions, track?: ObjectQL[]): Promise<Booted
290291
engine.registry.registerObject(o as never);
291292
}
292293
await engine.syncSchemas();
294+
// The organization-less rows below (global sets, grants, identities) exist
295+
// only on a store built before 18.x (ADR-0131 D1 makes a fresh tenant column
296+
// NOT NULL): this is that store.
297+
await relaxEveryRegisteredTenantColumn(engine);
293298
const names = new Set<string>();
294299
catalogs.set(engine, names);
295300
const catalog = () => ({ permissions: [...names].map((name) => ({ name })) });
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* Return engine-synced tables to the shape a store built BEFORE 18.x has: the
5+
* 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 rows — the holdings, grants and seeds this package's
10+
* NULL readers still answer for until the organization-ownership ceremony has
11+
* attributed them. A suite whose subject is those rows calls this after
12+
* `syncSchemas()` and before seeding them. Done through each table's driver
13+
* and its own drift apply door (`relax_not_null`), never a hand `ALTER`.
14+
*/
15+
export async function relaxTenantColumns(
16+
engine: { getDriverForObject(object: string): unknown },
17+
tables: readonly string[],
18+
column = 'organization_id',
19+
): Promise<void> {
20+
for (const table of tables) {
21+
const driver = engine.getDriverForObject(table) as {
22+
applyMigrationEntries(entries: never[]): Promise<{ applied: unknown[] }>;
23+
};
24+
const op = { type: 'relax_not_null', table, column };
25+
const { applied } = await driver.applyMigrationEntries([
26+
{ kind: 'nullability_mismatch', remoteName: table, table, column, category: 'needs_confirm', op } as never,
27+
]);
28+
if (applied.length !== 1) throw new Error(`relaxTenantColumns: ${table}.${column} was not relaxed`);
29+
}
30+
}
31+
32+
/**
33+
* {@link relaxTenantColumns} over every object of an app that carries a tenant
34+
* column as the engine registered it — the shape a whole pre-18 store has.
35+
*/
36+
export async function relaxEveryTenantColumn(
37+
engine: { getDriverForObject(object: string): unknown; getSchema(object: string): unknown },
38+
objects: readonly { name: string }[],
39+
): Promise<void> {
40+
const tenant = objects
41+
.map((o) => o.name)
42+
.filter((name) => {
43+
const schema = engine.getSchema(name) as { fields?: Record<string, unknown>; tenancy?: { enabled?: boolean } } | undefined;
44+
return !!schema?.fields?.organization_id && schema.tenancy?.enabled !== false;
45+
});
46+
await relaxTenantColumns(engine, tenant);
47+
}
48+
49+
/** {@link relaxEveryTenantColumn} over every object the engine's registry holds. */
50+
export async function relaxEveryRegisteredTenantColumn(engine: {
51+
getDriverForObject(object: string): unknown;
52+
getSchema(object: string): unknown;
53+
registry: { getAllObjects(): readonly { name: string }[] };
54+
}): Promise<void> {
55+
await relaxEveryTenantColumn(engine, engine.registry.getAllObjects());
56+
}

‎packages/plugins/plugin-auth/src/scim-deactivation-reconcile-user.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ import '@better-auth/core/context';
9999
import '@better-auth/core/error';
100100
import '@better-auth/oauth-provider';
101101
import '@better-auth/scim';
102+
import { relaxEveryRegisteredTenantColumn } from './pre-18-tenant-column.testkit.js';
102103

103104
/**
104105
* [#23184] Every sign-in below verifies the password the case attached, which
@@ -241,6 +242,10 @@ async function bootEngine(): Promise<BootedSqliteEngine> {
241242
engine.registry.registerObject(object as never, '@objectstack/plugin-auth');
242243
}
243244
await engine.syncSchemas();
245+
// The organization-less rows below (global sets, grants, identities) exist
246+
// only on a store built before 18.x (ADR-0131 D1 makes a fresh tenant column
247+
// NOT NULL): this is that store.
248+
await relaxEveryRegisteredTenantColumn(engine);
244249
// [ADR-0131 D3/D4] `admin_full_access` is a catalog definition — what the
245250
// break-glass guard, like the resolver, resolves a grant's set name to.
246251
bindTestSecurityCatalog(engine, { permissions: [{ name: ADMIN_FULL_ACCESS }] });

0 commit comments

Comments
 (0)