diff --git a/packages/server/src/sdk/app/rows/search/sqs.ts b/packages/server/src/sdk/app/rows/search/sqs.ts index 2c9ee1356c..72a1557cc9 100644 --- a/packages/server/src/sdk/app/rows/search/sqs.ts +++ b/packages/server/src/sdk/app/rows/search/sqs.ts @@ -41,6 +41,7 @@ import { getTableIDList, } from "./filters" import { dataFilters } from "@budibase/shared-core" +import { DEFAULT_TABLE_IDS } from "../../../../constants" const builder = new sql.Sql(SqlClient.SQL_LITE) const MISSING_COLUMN_REGEX = new RegExp(`no such column: .+`) @@ -211,6 +212,18 @@ async function runSqlQuery( return response } +function resyncDefinitionsRequired(status: number, message: string) { + // pre data_ prefix on column names, need to resync + return ( + (status === 400 && message?.match(USER_COLUMN_PREFIX_REGEX)) || + // default tables aren't included in definition + (status === 400 && + DEFAULT_TABLE_IDS.find(tableId => message?.includes(tableId))) || + // no design document found, needs a full sync + (status === 404 && message?.includes(SQLITE_DESIGN_DOC_ID)) + ) +} + export async function search( options: RowSearchParams, table: Table @@ -338,10 +351,7 @@ export async function search( return response } catch (err: any) { const msg = typeof err === "string" ? err : err.message - const syncAndRepeat = - (err.status === 400 && msg?.match(USER_COLUMN_PREFIX_REGEX)) || - (err.status === 404 && msg?.includes(SQLITE_DESIGN_DOC_ID)) - if (syncAndRepeat) { + if (resyncDefinitionsRequired(err.status, msg)) { await sdk.tables.sqs.syncDefinition() return search(options, table) } diff --git a/packages/server/src/sdk/app/tables/internal/sqs.ts b/packages/server/src/sdk/app/tables/internal/sqs.ts index 9e831f4af7..2d49adf96e 100644 --- a/packages/server/src/sdk/app/tables/internal/sqs.ts +++ b/packages/server/src/sdk/app/tables/internal/sqs.ts @@ -15,6 +15,7 @@ import { generateJunctionTableID, } from "../../../../db/utils" import { isEqual } from "lodash" +import { DEFAULT_TABLES } from "../../../../db/defaultData/datasource_bb_default" const FieldTypeMap: Record = { [FieldType.BOOLEAN]: SQLiteType.NUMERIC, @@ -126,8 +127,9 @@ function mapTable(table: Table): SQLiteTables { // nothing exists, need to iterate though existing tables async function buildBaseDefinition(): Promise { const tables = await tablesSdk.getAllInternalTables() + const defaultTables = DEFAULT_TABLES const definition = sql.designDoc.base("tableId") - for (let table of tables) { + for (let table of tables.concat(defaultTables)) { definition.sql.tables = { ...definition.sql.tables, ...mapTable(table),