Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions client/packages/platform/__tests__/src/migrations.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect, test as test } from 'vitest';
import { i } from '@instantdb/core';
import {
convertTxSteps,
diffSchemas,
Identifier,
MigrationTx,
Expand Down Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion client/packages/platform/src/migrationUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const linkDefToNewAttrTx = (link: AnyLink): MigrationTx => {
'checked-data-type': null,
'index?': false,
...uniqueAndCardinality,
'required?': false,
'required?': !!link.forward.required,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was actually done on purpose -- because add-attr on a large set of data would throw an error in the transaction. It would take too long. Maybe we need to seperate this into two steps? add attr, and make-required?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to do that later, since we have the same problem with adding indexed to a field.

But we might not actually validate required on this path. Can you double check that we actually validate that every entity has the field in this path?

identifier: {
attrName: link.forward.label,
namespace: link.forward.on,
Expand Down
Loading