@@ -391,3 +391,319 @@ describe('SeedLoadResult.insertedIds — the landed rows this load created (#303
391391 for ( const r of dryRun . results ) expect ( r . insertedIds ) . toEqual ( [ ] ) ;
392392 } ) ;
393393} ) ;
394+
395+ /**
396+ * #31021 — the record of which seed rows landed in an organization
397+ * (`sys_seed_landed_key`), and the rule it carries: a keyed seed row the load
398+ * cannot find is inserted only when its key never landed in that organization.
399+ *
400+ * Before it, the create-only rule (#30081) inserted every declared row it did
401+ * not find, so a row a person deleted came back under a new id at the next
402+ * replay — a reboot, or an upgrade that changes the seed union. Pinned here,
403+ * each case scoped to an organization:
404+ *
405+ * - a deleted seed row is not inserted again by a reboot or by a seed-union
406+ * change, while a row the upgrade adds still lands;
407+ * - an edited row is left as the person left it — renamed included;
408+ * - a reference to a removed row is left alone, never reported unresolved;
409+ * - the deliberate re-seed (`landedKeys: 'reseed'`) inserts it again;
410+ * - the record is the organization's: another organization's replay lands it.
411+ *
412+ * The record object is registered in the metadata double the way
413+ * `@objectstack/platform-objects` registers it in a real composition.
414+ */
415+ describe ( 'the record of landed seed rows: a row a person removed is not inserted again (#31021)' , ( ) => {
416+ const LEDGER = 'sys_seed_landed_key' ;
417+ const ORG = 'org_a' ;
418+
419+ const LEDGER_OBJECT = {
420+ name : LEDGER ,
421+ fields : {
422+ object_name : { type : 'text' } ,
423+ seed_key : { type : 'textarea' } ,
424+ organization_id : { type : 'lookup' , reference : 'sys_organization' } ,
425+ } ,
426+ } ;
427+ const ledgerMetadata = ( extra : Record < string , any > = { } , opts : { registered ?: boolean } = { } ) => createMetadata ( {
428+ ...( opts . registered === false ? { } : { [ LEDGER ] : LEDGER_OBJECT } ) ,
429+ lk_account : { name : 'lk_account' , fields : { name : { type : 'text' } , tier : { type : 'text' } } } ,
430+ lk_contact : {
431+ name : 'lk_contact' ,
432+ fields : {
433+ email : { type : 'text' } ,
434+ title : { type : 'text' } ,
435+ account : { type : 'lookup' , reference : 'lk_account' } ,
436+ accounts : { type : 'lookup' , reference : 'lk_account' , multiple : true } ,
437+ } ,
438+ } ,
439+ ...extra ,
440+ } ) ;
441+
442+ // `mode` omitted parses to `ignore` (the create-only default, #30081).
443+ const seeds = ( extraAccounts : any [ ] = [ ] ) => [
444+ {
445+ object : 'lk_account' ,
446+ externalId : 'name' ,
447+ mode : 'ignore' ,
448+ records : [ { name : 'Acme' , tier : 'gold' } , { name : 'Globex' , tier : 'silver' } , ...extraAccounts ] ,
449+ } ,
450+ {
451+ object : 'lk_contact' ,
452+ externalId : 'email' ,
453+ mode : 'ignore' ,
454+ records : [
455+ { email : 'ann@acme.test' , title : 'CEO' , account : 'Acme' } ,
456+ { email : 'gus@globex.test' , title : 'CTO' , account : 'Globex' } ,
457+ ] ,
458+ } ,
459+ ] as any [ ] ;
460+
461+ const orgConfig = ( organizationId = ORG ) => ( { ...CONFIG , defaultMode : 'ignore' , organizationId } ) ;
462+ const loadInto = (
463+ engine : IDataEngine ,
464+ metadata : IMetadataService ,
465+ datasets : any [ ] ,
466+ opts : { organizationId ?: string ; landedKeys ?: 'honour' | 'reseed' ; logger ?: ReturnType < typeof createLogger > } = { } ,
467+ ) => new SeedLoaderService ( engine , metadata , opts . logger ?? createLogger ( ) , { landedKeys : opts . landedKeys ?? 'honour' } )
468+ . load ( { seeds : datasets , config : orgConfig ( opts . organizationId ) } ) ;
469+
470+ const names = ( rows : any [ ] | undefined , org = ORG ) => ( rows ?? [ ] ) . filter ( ( r ) => r . organization_id === org ) . map ( ( r ) => r . name ) . sort ( ) ;
471+ const removeRow = ( store : Record < string , any [ ] > , object : string , match : ( r : any ) => boolean ) => {
472+ store [ object ] = store [ object ] . filter ( ( r ) => ! match ( r ) ) ;
473+ } ;
474+
475+ it ( 'records the key of every row it lands, under the organization it seeds' , async ( ) => {
476+ const { engine, store } = createFaithfulEngine ( ) ;
477+ const result = await loadInto ( engine , ledgerMetadata ( ) , seeds ( ) ) ;
478+
479+ expect ( result . success ) . toBe ( true ) ;
480+ const recorded = ( store [ LEDGER ] ?? [ ] ) . map ( ( r ) => [ r . organization_id , r . object_name , r . seed_key ] ) ;
481+ expect ( recorded ) . toEqual ( [
482+ [ ORG , 'lk_account' , '[["name","Acme"]]' ] ,
483+ [ ORG , 'lk_account' , '[["name","Globex"]]' ] ,
484+ [ ORG , 'lk_contact' , '[["email","ann@acme.test"]]' ] ,
485+ [ ORG , 'lk_contact' , '[["email","gus@globex.test"]]' ] ,
486+ ] ) ;
487+
488+ // A replay of the same seed records nothing twice.
489+ await loadInto ( engine , ledgerMetadata ( ) , seeds ( ) ) ;
490+ expect ( store [ LEDGER ] ) . toHaveLength ( 4 ) ;
491+ } ) ;
492+
493+ it ( 'a deleted seed row is not inserted again by a reboot, nor by an upgrade that changes the seed union — whose new row still lands' , async ( ) => {
494+ const { engine, store } = createFaithfulEngine ( ) ;
495+ const metadata = ledgerMetadata ( ) ;
496+ await loadInto ( engine , metadata , seeds ( ) ) ;
497+ const globexId = store . lk_account . find ( ( r ) => r . name === 'Globex' ) ! . id ;
498+ // The person deletes the row (its contact keeps a link to nothing, as a
499+ // `set_null` delete leaves it).
500+ removeRow ( store , 'lk_account' , ( r ) => r . id === globexId ) ;
501+ store . lk_contact . find ( ( r ) => r . email === 'gus@globex.test' ) ! . account = null ;
502+
503+ const logger = createLogger ( ) ;
504+ const reboot = await loadInto ( engine , metadata , seeds ( ) , { logger } ) ;
505+ expect ( names ( store . lk_account ) ) . toEqual ( [ 'Acme' ] ) ;
506+ expect ( reboot . results . find ( ( r ) => r . object === 'lk_account' ) ! ) . toMatchObject ( { inserted : 0 , skipped : 2 } ) ;
507+ // Said once, at `info`: the intended outcome of a replay, never a warning.
508+ expect ( logger . info . mock . calls . map ( ( c : any [ ] ) => String ( c [ 0 ] ) ) . filter ( ( m : string ) => m . includes ( 'not inserted again' ) ) ) . toHaveLength ( 1 ) ;
509+ expect ( logger . warn ) . not . toHaveBeenCalled ( ) ;
510+
511+ // The upgrade: a new account joins the seed.
512+ const upgrade = await loadInto ( engine , metadata , seeds ( [ { name : 'Initech' , tier : 'bronze' } ] ) ) ;
513+ expect ( names ( store . lk_account ) ) . toEqual ( [ 'Acme' , 'Initech' ] ) ;
514+ expect ( upgrade . results . find ( ( r ) => r . object === 'lk_account' ) ! ) . toMatchObject ( { inserted : 1 , skipped : 2 } ) ;
515+ expect ( upgrade . success ) . toBe ( true ) ;
516+ } ) ;
517+
518+ it ( 'under `upsert` too: a deleted row stays deleted while the rows still there are kept current' , async ( ) => {
519+ const { engine, store } = createFaithfulEngine ( ) ;
520+ const metadata = ledgerMetadata ( ) ;
521+ const upsertSeeds = ( ) => seeds ( ) . map ( ( d ) => ( { ...d , mode : 'upsert' } ) ) ;
522+ await loadInto ( engine , metadata , upsertSeeds ( ) ) ;
523+ removeRow ( store , 'lk_account' , ( r ) => r . name === 'Globex' ) ;
524+ store . lk_account . find ( ( r ) => r . name === 'Acme' ) ! . tier = 'edited' ;
525+
526+ await loadInto ( engine , metadata , upsertSeeds ( ) ) ;
527+
528+ expect ( names ( store . lk_account ) ) . toEqual ( [ 'Acme' ] ) ;
529+ // Positive control: this was the upsert path — the surviving row was rewritten.
530+ expect ( store . lk_account . find ( ( r ) => r . name === 'Acme' ) ! . tier ) . toBe ( 'gold' ) ;
531+ } ) ;
532+
533+ it ( 'an edited row is untouched — a changed column, and a renamed key, which is not inserted again under its seed name' , async ( ) => {
534+ const { engine, store } = createFaithfulEngine ( ) ;
535+ const metadata = ledgerMetadata ( ) ;
536+ await loadInto ( engine , metadata , seeds ( ) ) ;
537+ const acme = store . lk_account . find ( ( r ) => r . name === 'Acme' ) ! ;
538+ acme . tier = 'platinum' ;
539+ const globex = store . lk_account . find ( ( r ) => r . name === 'Globex' ) ! ;
540+ globex . name = 'Globex Corporation' ;
541+
542+ const replay = await loadInto ( engine , metadata , seeds ( ) ) ;
543+
544+ expect ( replay . success ) . toBe ( true ) ;
545+ expect ( store . lk_account ) . toHaveLength ( 2 ) ;
546+ expect ( store . lk_account . find ( ( r ) => r . id === acme . id ) ) . toMatchObject ( { name : 'Acme' , tier : 'platinum' } ) ;
547+ expect ( store . lk_account . find ( ( r ) => r . id === globex . id ) ) . toMatchObject ( { name : 'Globex Corporation' , tier : 'silver' } ) ;
548+ } ) ;
549+
550+ it ( 'a reference to a removed row is left as it is: no error, no deferral, and the rest of the row still loads' , async ( ) => {
551+ const { engine, store } = createFaithfulEngine ( ) ;
552+ const metadata = ledgerMetadata ( ) ;
553+ await loadInto ( engine , metadata , seeds ( ) ) ;
554+ removeRow ( store , 'lk_account' , ( r ) => r . name === 'Globex' ) ;
555+ store . lk_contact . find ( ( r ) => r . email === 'gus@globex.test' ) ! . account = null ;
556+ // The contact whose account is gone is removed too, and a new contact
557+ // naming the removed account joins the seed.
558+ removeRow ( store , 'lk_contact' , ( r ) => r . email === 'gus@globex.test' ) ;
559+ const next = seeds ( ) ;
560+ next [ 1 ] . records . push ( { email : 'new@globex.test' , title : 'CFO' , account : 'Globex' , accounts : [ 'Acme' , 'Globex' ] } ) ;
561+
562+ const logger = createLogger ( ) ;
563+ const replay = await loadInto ( engine , metadata , next , { logger } ) ;
564+
565+ expect ( replay . success ) . toBe ( true ) ;
566+ expect ( replay . errors ) . toEqual ( [ ] ) ;
567+ expect ( logger . error ) . not . toHaveBeenCalled ( ) ;
568+ const contacts = replay . results . find ( ( r ) => r . object === 'lk_contact' ) ! ;
569+ expect ( contacts ) . toMatchObject ( { inserted : 1 , skipped : 2 , referencesDeferred : 0 , referencesDropped : 0 , errored : 0 } ) ;
570+ const acmeId = store . lk_account . find ( ( r ) => r . name === 'Acme' ) ! . id ;
571+ // The new contact lands without the single link, and keeps the elements
572+ // of the multi-value link that still resolve.
573+ const fresh = store . lk_contact . find ( ( r ) => r . email === 'new@globex.test' ) ! ;
574+ expect ( fresh . account ?? null ) . toBeNull ( ) ;
575+ expect ( fresh . accounts ) . toEqual ( [ acmeId ] ) ;
576+ // The removed contact stays removed.
577+ expect ( store . lk_contact . map ( ( r ) => r . email ) . sort ( ) ) . toEqual ( [ 'ann@acme.test' , 'new@globex.test' ] ) ;
578+ } ) ;
579+
580+ it ( 'a row keyed on a reference to a removed row is withdrawn with it' , async ( ) => {
581+ const { engine, store } = createFaithfulEngine ( ) ;
582+ const metadata = ledgerMetadata ( ) ;
583+ const withMembership = ( ) => [
584+ { object : 'lid_team' , externalId : 'name' , mode : 'ignore' , records : [ { name : 'Experience' } , { name : 'Platform' } ] } ,
585+ { object : 'lid_membership' , externalId : 'team' , mode : 'ignore' , records : [ { team : 'Experience' , role : 'lead' } , { team : 'Platform' , role : 'member' } ] } ,
586+ ] as any [ ] ;
587+ await loadInto ( engine , metadata , withMembership ( ) ) ;
588+ const experience = store . lid_team . find ( ( r ) => r . name === 'Experience' ) ! . id ;
589+ removeRow ( store , 'lid_team' , ( r ) => r . id === experience ) ;
590+ removeRow ( store , 'lid_membership' , ( r ) => r . team === experience ) ;
591+
592+ const replay = await loadInto ( engine , metadata , withMembership ( ) ) ;
593+
594+ expect ( replay . success ) . toBe ( true ) ;
595+ expect ( replay . errors ) . toEqual ( [ ] ) ;
596+ expect ( store . lid_team . map ( ( r ) => r . name ) ) . toEqual ( [ 'Platform' ] ) ;
597+ expect ( store . lid_membership ) . toHaveLength ( 1 ) ;
598+ expect ( replay . results . find ( ( r ) => r . object === 'lid_membership' ) ! ) . toMatchObject ( { inserted : 0 , skipped : 2 , referencesResolved : 1 } ) ;
599+ } ) ;
600+
601+ it ( 'the deliberate re-seed inserts a removed row again and records it, so a second delete is remembered too' , async ( ) => {
602+ const { engine, store } = createFaithfulEngine ( ) ;
603+ const metadata = ledgerMetadata ( ) ;
604+ await loadInto ( engine , metadata , seeds ( ) ) ;
605+ removeRow ( store , 'lk_account' , ( r ) => r . name === 'Globex' ) ;
606+
607+ const reseed = await loadInto ( engine , metadata , seeds ( ) , { landedKeys : 'reseed' } ) ;
608+ expect ( reseed . results . find ( ( r ) => r . object === 'lk_account' ) ! . inserted ) . toBe ( 1 ) ;
609+ expect ( names ( store . lk_account ) ) . toEqual ( [ 'Acme' , 'Globex' ] ) ;
610+ expect ( store [ LEDGER ] ) . toHaveLength ( 4 ) ;
611+
612+ removeRow ( store , 'lk_account' , ( r ) => r . name === 'Globex' ) ;
613+ await loadInto ( engine , metadata , seeds ( ) ) ;
614+ expect ( names ( store . lk_account ) ) . toEqual ( [ 'Acme' ] ) ;
615+ } ) ;
616+
617+ it ( "the record is the organization's own: a removal in one organization does not reach another's replay" , async ( ) => {
618+ const { engine, store } = createFaithfulEngine ( ) ;
619+ const metadata = ledgerMetadata ( ) ;
620+ await loadInto ( engine , metadata , seeds ( ) , { organizationId : 'org_a' } ) ;
621+ await loadInto ( engine , metadata , seeds ( ) , { organizationId : 'org_b' } ) ;
622+ removeRow ( store , 'lk_account' , ( r ) => r . organization_id === 'org_a' && r . name === 'Globex' ) ;
623+ removeRow ( store , 'lk_account' , ( r ) => r . organization_id === 'org_b' && r . name === 'Acme' ) ;
624+
625+ await loadInto ( engine , metadata , seeds ( ) , { organizationId : 'org_a' } ) ;
626+ await loadInto ( engine , metadata , seeds ( ) , { organizationId : 'org_b' } ) ;
627+ expect ( names ( store . lk_account , 'org_a' ) ) . toEqual ( [ 'Acme' ] ) ;
628+ expect ( names ( store . lk_account , 'org_b' ) ) . toEqual ( [ 'Globex' ] ) ;
629+
630+ // A new organization has no record yet: it gets the whole seed.
631+ await loadInto ( engine , metadata , seeds ( ) , { organizationId : 'org_c' } ) ;
632+ expect ( names ( store . lk_account , 'org_c' ) ) . toEqual ( [ 'Acme' , 'Globex' ] ) ;
633+ } ) ;
634+
635+ it ( 'an `id`-keyed row is recorded under its authored id, so a replay that gives it a per-organization id still finds it removed' , async ( ) => {
636+ const { engine, store } = createFaithfulEngine ( ) ;
637+ const metadata = ledgerMetadata ( { lk_unit : { name : 'lk_unit' , fields : { name : { type : 'text' } } } } ) ;
638+ const units = ( ) => [ { object : 'lk_unit' , externalId : 'id' , mode : 'ignore' , records : [ { id : 'bu_hq' , name : 'HQ' } ] } ] as any [ ] ;
639+ await loadInto ( engine , metadata , units ( ) , { organizationId : 'org_a' } ) ;
640+ await loadInto ( engine , metadata , units ( ) , { organizationId : 'org_b' } ) ;
641+ // org_b holds the derived id (#21665); both record the authored one.
642+ expect ( store . lk_unit . map ( ( r ) => [ r . organization_id , r . id ] ) ) . toEqual ( [ [ 'org_a' , 'bu_hq' ] , [ 'org_b' , 'bu_hq__org_b' ] ] ) ;
643+ expect ( store [ LEDGER ] . map ( ( r ) => r . seed_key ) ) . toEqual ( [ '[["id","bu_hq"]]' , '[["id","bu_hq"]]' ] ) ;
644+
645+ // Removed in org_b, then in org_a: the authored id is free again, and
646+ // org_b's replay would take it — the record still names the row.
647+ removeRow ( store , 'lk_unit' , ( r ) => r . organization_id === 'org_b' ) ;
648+ removeRow ( store , 'lk_unit' , ( r ) => r . organization_id === 'org_a' ) ;
649+ await loadInto ( engine , metadata , units ( ) , { organizationId : 'org_b' } ) ;
650+ expect ( store . lk_unit ) . toEqual ( [ ] ) ;
651+ } ) ;
652+
653+ it ( 'a load that keeps no record says why once, and inserts as before: no organization, or no registered record object' , async ( ) => {
654+ const noOrg = createFaithfulEngine ( ) ;
655+ const noOrgLogger = createLogger ( ) ;
656+ await new SeedLoaderService ( noOrg . engine , ledgerMetadata ( ) , noOrgLogger , { landedKeys : 'honour' } )
657+ . load ( { seeds : seeds ( ) , config : { ...CONFIG , defaultMode : 'ignore' } } ) ;
658+ expect ( noOrg . store [ LEDGER ] ) . toBeUndefined ( ) ;
659+ expect ( noOrgLogger . info . mock . calls . map ( ( c : any [ ] ) => String ( c [ 0 ] ) ) . filter ( ( m : string ) => m . includes ( 'seeds no organization' ) ) ) . toHaveLength ( 1 ) ;
660+
661+ const unregistered = createFaithfulEngine ( ) ;
662+ const logger = createLogger ( ) ;
663+ const bare = ledgerMetadata ( { } , { registered : false } ) ;
664+ await loadInto ( unregistered . engine , bare , seeds ( ) , { logger } ) ;
665+ removeRow ( unregistered . store , 'lk_account' , ( r ) => r . name === 'Globex' ) ;
666+ await loadInto ( unregistered . engine , bare , seeds ( ) , { logger } ) ;
667+ expect ( unregistered . store [ LEDGER ] ) . toBeUndefined ( ) ;
668+ expect ( names ( unregistered . store . lk_account ) ) . toEqual ( [ 'Acme' , 'Globex' ] ) ;
669+ const warned = logger . warn . mock . calls . map ( ( c : any [ ] ) => String ( c [ 0 ] ) ) ;
670+ expect ( warned ) . toHaveLength ( 2 ) ;
671+ expect ( warned [ 0 ] ) . toContain ( `'${ LEDGER } ' is not registered` ) ;
672+ } ) ;
673+
674+ it ( 'a loader not asked for the record neither reads nor writes it' , async ( ) => {
675+ const { engine, store } = createFaithfulEngine ( ) ;
676+ const metadata = ledgerMetadata ( ) ;
677+ await new SeedLoaderService ( engine , metadata , createLogger ( ) ) . load ( { seeds : seeds ( ) , config : orgConfig ( ) } ) ;
678+ expect ( store [ LEDGER ] ) . toBeUndefined ( ) ;
679+ expect ( ( engine . find as any ) . mock . calls . some ( ( c : any [ ] ) => c [ 0 ] === LEDGER ) ) . toBe ( false ) ;
680+ } ) ;
681+
682+ it ( 'a failed record write is reported once at `error`; the rows it seeded still land and the load succeeds' , async ( ) => {
683+ const { engine, store } = createFaithfulEngine ( ) ;
684+ const insert = ( engine . insert as any ) . getMockImplementation ( ) ;
685+ ( engine . insert as any ) . mockImplementation ( async ( object : string , data : any , opts ?: any ) => {
686+ if ( object === LEDGER ) throw Object . assign ( new Error ( 'disk full' ) , { code : 'SQLITE_FULL' } ) ;
687+ return insert ( object , data , opts ) ;
688+ } ) ;
689+ const logger = createLogger ( ) ;
690+
691+ const result = await loadInto ( engine , ledgerMetadata ( ) , seeds ( ) , { logger } ) ;
692+
693+ expect ( result . success ) . toBe ( true ) ;
694+ expect ( names ( store . lk_account ) ) . toEqual ( [ 'Acme' , 'Globex' ] ) ;
695+ expect ( logger . error ) . toHaveBeenCalledTimes ( 1 ) ;
696+ expect ( String ( logger . error . mock . calls [ 0 ] [ 0 ] ) ) . toContain ( 'Recording which seed rows landed FAILED' ) ;
697+ } ) ;
698+
699+ it ( 'a record read that fails for any reason but a missing table stops the load rather than guess' , async ( ) => {
700+ const { engine } = createFaithfulEngine ( ) ;
701+ const find = ( engine . find as any ) . getMockImplementation ( ) ;
702+ ( engine . find as any ) . mockImplementation ( async ( object : string , query ?: any ) => {
703+ if ( object === LEDGER ) throw Object . assign ( new Error ( 'connection reset' ) , { code : 'ECONNRESET' } ) ;
704+ return find ( object , query ) ;
705+ } ) ;
706+
707+ await expect ( loadInto ( engine , ledgerMetadata ( ) , seeds ( ) ) ) . rejects . toThrow ( 'connection reset' ) ;
708+ } ) ;
709+ } ) ;
0 commit comments