From 011a258ba82b46f8a939f2ff64fee9f26f82dcfb Mon Sep 17 00:00:00 2001 From: stopachka Date: Sun, 15 Mar 2026 12:30:15 -0700 Subject: [PATCH] [codex] Preserve required constraints when creating links --- .../platform/__tests__/src/migrations.test.ts | 61 +++++++++++++++++++ .../packages/platform/src/migrationUtils.ts | 2 +- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/client/packages/platform/__tests__/src/migrations.test.ts b/client/packages/platform/__tests__/src/migrations.test.ts index 3abd1ea72f..82fa379d67 100644 --- a/client/packages/platform/__tests__/src/migrations.test.ts +++ b/client/packages/platform/__tests__/src/migrations.test.ts @@ -1,6 +1,7 @@ import { expect, test as test } from 'vitest'; import { i } from '@instantdb/core'; import { + convertTxSteps, diffSchemas, Identifier, MigrationTx, @@ -595,6 +596,66 @@ test('make link optional', async () => { expect(found).toBeDefined(); }); +test('create required link', async () => { + const result = await diffSchemas( + i.schema({ + entities: { + albums: i.entity({ + name: i.string(), + }), + songs: i.entity({ + name: i.string(), + }), + }, + links: {}, + }), + i.schema({ + entities: { + albums: i.entity({ + name: i.string(), + }), + songs: i.entity({ + name: i.string(), + }), + }, + links: { + songAlbum: { + forward: { + on: 'albums', + has: 'many', + label: 'songs', + required: true, + }, + reverse: { on: 'songs', has: 'one', label: 'albums' }, + }, + }, + }), + createChooser([]), + systemCatalogIdentNames, + ); + + expectTxType(result, 'add-attr', 1); + + const addAttr = result.find( + (tx) => + tx.type === 'add-attr' && + tx.identifier.namespace === 'albums' && + tx.identifier.attrName === 'songs', + ); + expect(addAttr).toMatchObject({ + type: 'add-attr', + 'required?': true, + }); + + const requiredPlanStep = convertTxSteps(result, []).find( + (step) => + step[0] === 'required' && + step[1]['forward-identity'][1] === 'albums' && + step[1]['forward-identity'][2] === 'songs', + ); + expect(requiredPlanStep).toBeDefined(); +}); + test('system catalog attrs are ignored when adding entities', async () => { const result = await diffSchemas( i.schema({ diff --git a/client/packages/platform/src/migrationUtils.ts b/client/packages/platform/src/migrationUtils.ts index 1980ea4db0..7d5f8f1c1c 100644 --- a/client/packages/platform/src/migrationUtils.ts +++ b/client/packages/platform/src/migrationUtils.ts @@ -35,7 +35,7 @@ export const linkDefToNewAttrTx = (link: AnyLink): MigrationTx => { 'checked-data-type': null, 'index?': false, ...uniqueAndCardinality, - 'required?': false, + 'required?': !!link.forward.required, identifier: { attrName: link.forward.label, namespace: link.forward.on,