@@ -69,7 +69,8 @@ const FIXTURE_APP = [
6969 {
7070 // S1 — per-organization unique field. Two organizations may each hold
7171 // `a@b.com`; a duplicate WITHIN one may not. S8's mixed population rides
72- // along: NULL-organization rows form one platform bucket (D3).
72+ // along: NULL-organization rows (pre-18 databases only) are exempt from
73+ // the plain organization key part (ADR-0131 D1).
7374 name : 'crm_contact' ,
7475 fields : {
7576 organization_id : { type : 'string' } ,
@@ -152,22 +153,23 @@ const FIXTURE_APP = [
152153
153154/**
154155 * The expected unique key parts, per table — the SAME map for every posture.
155- * `COALESCE(col)` is the literal-agnostic reading of the NULL-safe organization
156- * key part, matching the identity the drift differ compares on.
156+ * The organization key part is the PLAIN column (ADR-0131 D1): a legacy
157+ * NULL-safe part would read here as `COALESCE(col)` (literal elided, the
158+ * differ's identity) and fail the map, so no posture can bring one back.
157159 */
158160const EXPECTED_UNIQUE_KEY_PARTS : Record < string , Record < string , string [ ] > > = {
159- crm_contact : { uniq_crm_contact_organization_id_email : [ 'COALESCE( organization_id) ' , 'email' ] } ,
161+ crm_contact : { uniq_crm_contact_organization_id_email : [ 'organization_id' , 'email' ] } ,
160162 crm_device : { uniq_crm_device_serial : [ 'serial' ] } ,
161163 // Note the index NAME: the deterministic name is derived from the RESOLVED
162164 // columns, so a declared `'organization'` index carries the organization
163165 // column in its name even though the author never listed it. That is the
164166 // differ's identity too, which is why declaring and detecting cannot drift.
165167 crm_case : {
166- uniq_crm_case_organization_id_department_code : [ 'COALESCE( organization_id) ' , 'department' , 'code' ] ,
168+ uniq_crm_case_organization_id_department_code : [ 'organization_id' , 'department' , 'code' ] ,
167169 } ,
168170 crm_delivery : { uniq_crm_delivery_source_dedup_key : [ 'source' , 'dedup_key' ] } ,
169171 crm_team : { uniq_crm_team_name_organization_id : [ 'name' , 'organization_id' ] } ,
170- crm_product : { uniq_crm_product_organization_id_code : [ 'COALESCE( organization_id) ' , 'code' ] } ,
172+ crm_product : { uniq_crm_product_organization_id_code : [ 'organization_id' , 'code' ] } ,
171173 crm_registry : { uniq_crm_registry_slug : [ 'slug' ] } ,
172174} ;
173175
@@ -279,21 +281,29 @@ describe('ADR-0120 — one app package, three tenancy postures (S13/S14 acceptan
279281 . rejects . toThrow ( REJECTED ) ;
280282 } ) ;
281283
282- it ( 'S7/S8 — the NULL-organization bucket is a real bucket, in every posture' , async ( ) => {
283- // THE #5030 headline, and the reason `'organization'` could ship at all:
284- // before D3 this pair inserted twice on a single-organization stack
285- // (SQL UNIQUE is NULL-distinct), so every field-level unique was a
286- // silent no-op there. It must now be enforced under `single` exactly as
287- // under `isolated` — the posture is not what makes a constraint real.
288- // The NULL bucket exists only on a database built before 18.x (ADR-0131
289- // D1 makes a fresh tenant column NOT NULL).
284+ it ( 'S7/S8 — NULL organization: refused on a fresh table, exempt on a pre-18 one, in every posture' , async ( ) => {
285+ // THE #5030 headline: before ADR-0120 D3 a NULL-organization pair
286+ // inserted twice on a single-organization stack (SQL UNIQUE is
287+ // NULL-distinct), so every field-level unique was a silent no-op there.
288+ // ADR-0131 D1 closes that at the COLUMN, not the index: a fresh tenant
289+ // column is NOT NULL, so the row cannot be written at all — under
290+ // `single` exactly as under `isolated`; the posture is not what makes a
291+ // constraint real.
292+ await expect ( driver . create ( 'crm_contact' , { email : 'platform@b.com' } ) )
293+ . rejects . toThrow ( / N O T N U L L c o n s t r a i n t f a i l e d | n u l l v a l u e i n c o l u m n / ) ;
294+ // A database built before 18.x keeps its nullable column; there the
295+ // plain key part is NULL-distinct, so NULL-organization rows are exempt
296+ // (the D3 platform bucket is retired, not rebuilt)…
290297 await asPre18TenantColumn ( driver , 'crm_contact' ) ;
291298 await driver . create ( 'crm_contact' , { email : 'platform@b.com' } ) ;
292299 await expect ( driver . create ( 'crm_contact' , { email : 'platform@b.com' } ) )
293- . rejects . toThrow ( REJECTED ) ;
294- // …while an organization-owned row with the same value still fits (S8).
300+ . resolves . toBeTruthy ( ) ;
301+ // …while an organization-owned row with the same value still fits (S8),
302+ // once — the organization stays constrained.
295303 await expect ( driver . create ( 'crm_contact' , { organization_id : 'org_a' , email : 'platform@b.com' } ) )
296304 . resolves . toBeTruthy ( ) ;
305+ await expect ( driver . create ( 'crm_contact' , { organization_id : 'org_a' , email : 'platform@b.com' } ) )
306+ . rejects . toThrow ( REJECTED ) ;
297307 } ) ;
298308
299309 it ( 'S2 — a platform-wide unique field crosses organizations, in every posture' , async ( ) => {
@@ -308,12 +318,15 @@ describe('ADR-0120 — one app package, three tenancy postures (S13/S14 acceptan
308318 . resolves . toBeTruthy ( ) ;
309319 await expect ( driver . create ( 'crm_case' , { organization_id : 'org_a' , department : 'ops' , code : 'C1' } ) )
310320 . rejects . toThrow ( REJECTED ) ;
311- // …and the NULL bucket is constrained too (D3 reaches declared indexes).
312- // The NULL bucket exists only on a database built before 18.x (ADR-0131
313- // D1 makes a fresh tenant column NOT NULL ).
321+ // …and NULL-organization rows, which exist only on a database built
322+ // before 18.x, are exempt from the declared composite exactly as from
323+ // the field-level one: its organization key part is plain (ADR-0131 D1 ).
314324 await asPre18TenantColumn ( driver , 'crm_case' ) ;
315325 await driver . create ( 'crm_case' , { department : 'ops' , code : 'C9' } ) ;
316326 await expect ( driver . create ( 'crm_case' , { department : 'ops' , code : 'C9' } ) )
327+ . resolves . toBeTruthy ( ) ;
328+ // The relaxation left the composite in force for organizations.
329+ await expect ( driver . create ( 'crm_case' , { organization_id : 'org_a' , department : 'ops' , code : 'C1' } ) )
317330 . rejects . toThrow ( REJECTED ) ;
318331 } ) ;
319332
@@ -340,10 +353,11 @@ describe('ADR-0120 — one app package, three tenancy postures (S13/S14 acceptan
340353 . rejects . toThrow ( REJECTED ) ;
341354 await expect ( driver . create ( 'crm_team' , { organization_id : 'org_b' , name : 'Core' } ) )
342355 . resolves . toBeTruthy ( ) ;
343- // The un-closed NULL hole, pinned as the honest status quo (D5c nudges
344- // toward the respelling that closes it; nothing forces it).
345- // The NULL bucket exists only on a database built before 18.x (ADR-0131
346- // D1 makes a fresh tenant column NOT NULL).
356+ // NULL-organization rows are exempt, pinned as the honest status quo.
357+ // Since ADR-0131 D1 the respelling D5c nudges toward is plain too, so
358+ // this is no longer a hole one spelling has and the other closes: such
359+ // rows exist only on a database built before 18.x (a fresh tenant
360+ // column is NOT NULL).
347361 await asPre18TenantColumn ( driver , 'crm_team' ) ;
348362 await driver . create ( 'crm_team' , { name : 'Platform' } ) ;
349363 await expect ( driver . create ( 'crm_team' , { name : 'Platform' } ) ) . resolves . toBeTruthy ( ) ;
0 commit comments