Skip to content

Commit

Permalink
fix: field dependence update crash (#1129)
Browse files Browse the repository at this point in the history
* fix: field dependence update crash

* fix: theme crash
  • Loading branch information
tea-artist authored Dec 2, 2024
1 parent 3e76a7f commit e6c7fd9
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,25 @@ export class FieldConvertingService {

private async generateReferenceFieldOps(fieldId: string) {
const topoOrdersContext = await this.fieldCalculationService.getTopoOrdersContext([fieldId]);
const { fieldMap, fieldId2TableId, directedGraph } = topoOrdersContext;

const { fieldMap, fieldId2TableId } = topoOrdersContext;
// Find affected fields using directedGraph
const affectedFields = new Set<string>();

// get all fields after current field
const topoOrders = topoOrdersContext.topoOrders.slice(
topoOrdersContext.topoOrders.findIndex((item) => item.id === fieldId) + 1
);
function findAffectedFields(currentId: string) {
for (const { fromFieldId, toFieldId } of directedGraph) {
if (fromFieldId === currentId && !affectedFields.has(toFieldId)) {
affectedFields.add(toFieldId);
findAffectedFields(toFieldId);
}
}
}

// Start from the initial field
findAffectedFields(fieldId);

// Filter topoOrders to only include affected fields
const topoOrders = topoOrdersContext.topoOrders.filter((item) => affectedFields.has(item.id));

if (!topoOrders.length) {
return {};
Expand All @@ -234,9 +246,9 @@ export class FieldConvertingService {

for (let i = 0; i < topoOrders.length; i++) {
const topoOrder = topoOrders[i];
// curField will be mutate in loop
const curField = fieldMap[topoOrder.id];
const tableId = fieldId2TableId[curField.id];

if (curField.isLookup) {
pushOpsMap(tableId, curField.id, this.updateLookupField(curField, fieldMap));
} else if (curField.type === FieldType.Formula) {
Expand Down
48 changes: 48 additions & 0 deletions apps/nestjs-backend/test/field-converting.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,54 @@ describe('OpenAPI Freely perform column transformations (e2e)', () => {
expect(newField.name).toEqual('my name');
expect(newField.description).toEqual('world');
});

// A -> B -> C
// D -> E -> C
// should not update E when A update
// all context: A, B, C, E
// update context: A, B, C
it('should not update E when A update', async () => {
const aField = await createField(table1.id, {
type: FieldType.Number,
});

const bField = await createField(table1.id, {
type: FieldType.Formula,
options: {
expression: `{${aField.id}}`,
},
});

const dField = await createField(table1.id, {
type: FieldType.Number,
});

const eField = await createField(table1.id, {
type: FieldType.Formula,
options: {
expression: `{${dField.id}}`,
},
});

const cField = await createField(table1.id, {
type: FieldType.Formula,
options: {
expression: `{${bField.id}} + {${eField.id}}`,
},
});

await updateRecordByApi(table1.id, table1.records[0].id, aField.id, 1);

await convertField(table1.id, bField.id, {
type: FieldType.Formula,
options: {
expression: `{${aField.id}} & ''`,
},
});

const record1 = await getRecord(table1.id, table1.records[0].id);
expect(record1.fields[cField.id]).toEqual('1null');
});
});

describe('convert text field', () => {
Expand Down
2 changes: 1 addition & 1 deletion apps/nextjs-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
"@teable/common-i18n": "workspace:^",
"@teable/core": "workspace:^",
"@teable/icons": "workspace:^",
"@teable/next-themes": "0.3.3",
"@teable/next-themes": "0.3.5",
"@teable/openapi": "workspace:^",
"@teable/sdk": "workspace:^",
"@teable/ui-lib": "workspace:^",
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"@teable/common-i18n": "workspace:*",
"@teable/core": "workspace:*",
"@teable/icons": "workspace:*",
"@teable/next-themes": "0.3.3",
"@teable/next-themes": "0.3.5",
"@teable/openapi": "workspace:*",
"@teable/ui-lib": "workspace:*",
"@udecode/cn": "37.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/ui-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
"@radix-ui/react-toggle-group": "1.1.0",
"@radix-ui/react-tooltip": "1.0.7",
"@teable/icons": "workspace:^",
"@teable/next-themes": "0.3.3",
"@teable/next-themes": "0.3.5",
"class-variance-authority": "0.7.0",
"clsx": "2.1.0",
"cmdk": "1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion plugins/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@teable/common-i18n": "workspace:^",
"@teable/core": "workspace:^",
"@teable/icons": "workspace:^",
"@teable/next-themes": "0.3.3",
"@teable/next-themes": "0.3.5",
"@teable/openapi": "workspace:^",
"@teable/sdk": "workspace:^",
"@teable/ui-lib": "workspace:^",
Expand Down
28 changes: 14 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e6c7fd9

Please sign in to comment.