|
| 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 | +} |
0 commit comments