From bbcb77c738b4c7c82e94c8f7d4c52bc8d2c5af1d Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 20 Aug 2024 07:38:23 +0100 Subject: [PATCH] Fixing an issue where searching on a relationship column which is in a broken state would break in SQS (previously this was allowed and would return empty relationships). --- .../src/api/controllers/row/utils/sqlUtils.ts | 9 ++++- .../server/src/sdk/app/rows/queryUtils.ts | 33 +++++++++++-------- .../src/sdk/app/rows/search/internal/sqs.ts | 2 +- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/packages/server/src/api/controllers/row/utils/sqlUtils.ts b/packages/server/src/api/controllers/row/utils/sqlUtils.ts index 7beadabd89..a24ec17c26 100644 --- a/packages/server/src/api/controllers/row/utils/sqlUtils.ts +++ b/packages/server/src/api/controllers/row/utils/sqlUtils.ts @@ -151,7 +151,10 @@ export function buildExternalRelationships( return relationships } -export function buildInternalRelationships(table: Table): RelationshipsJson[] { +export function buildInternalRelationships( + table: Table, + allTables: Table[] +): RelationshipsJson[] { const relationships: RelationshipsJson[] = [] const links = Object.values(table.schema).filter( column => column.type === FieldType.LINK @@ -164,6 +167,10 @@ export function buildInternalRelationships(table: Table): RelationshipsJson[] { const linkTableId = link.tableId! const junctionTableId = generateJunctionTableID(tableId, linkTableId) const isFirstTable = tableId > linkTableId + // skip relationships with missing table definitions + if (!allTables.find(table => table._id === linkTableId)) { + continue + } relationships.push({ through: junctionTableId, column: link.name, diff --git a/packages/server/src/sdk/app/rows/queryUtils.ts b/packages/server/src/sdk/app/rows/queryUtils.ts index b033c6dd17..a5e78b2fb9 100644 --- a/packages/server/src/sdk/app/rows/queryUtils.ts +++ b/packages/server/src/sdk/app/rows/queryUtils.ts @@ -71,20 +71,27 @@ export const getQueryableFields = async ( // avoid circular loops continue } - const relatedTable = await sdk.tables.getTable(subSchema.tableId) - const relatedFields = await extractTableFields( - relatedTable, - Object.keys(relatedTable.schema), - [...fromTables, subSchema.tableId] - ) + try { + const relatedTable = await sdk.tables.getTable(subSchema.tableId) + const relatedFields = await extractTableFields( + relatedTable, + Object.keys(relatedTable.schema), + [...fromTables, subSchema.tableId] + ) - result.push( - ...relatedFields.flatMap(f => [ - `${subSchema.name}.${f}`, - // should be able to filter by relationship using table name - `${relatedTable.name}.${f}`, - ]) - ) + result.push( + ...relatedFields.flatMap(f => [ + `${subSchema.name}.${f}`, + // should be able to filter by relationship using table name + `${relatedTable.name}.${f}`, + ]) + ) + } catch (err: any) { + // if related table is removed, ignore + if (err.status !== 404) { + throw err + } + } } else { result.push(field) } diff --git a/packages/server/src/sdk/app/rows/search/internal/sqs.ts b/packages/server/src/sdk/app/rows/search/internal/sqs.ts index 4bfa8f8fa5..6736ff6abf 100644 --- a/packages/server/src/sdk/app/rows/search/internal/sqs.ts +++ b/packages/server/src/sdk/app/rows/search/internal/sqs.ts @@ -297,7 +297,7 @@ export async function search( throw new Error("Unable to find table") } - const relationships = buildInternalRelationships(table) + const relationships = buildInternalRelationships(table, allTables) const searchFilters: SearchFilters = { ...cleanupFilters(query, table, allTables),