From 79c292538c70b7d2d2697eb78d2d3207955647cc Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 12 Jul 2024 13:51:06 +0100 Subject: [PATCH] There is a risk with default tables that the schema may exist in the DB as well as existing in memory - in this case we should merge the schemas to make sure that all possible attributes from the in memory representation, and the on disk version (which may have been updated by the user) have been captured in the SQLite schema. --- packages/server/src/sdk/app/tables/internal/sqs.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/server/src/sdk/app/tables/internal/sqs.ts b/packages/server/src/sdk/app/tables/internal/sqs.ts index fc0ee8fc0b..aeaa33ed2d 100644 --- a/packages/server/src/sdk/app/tables/internal/sqs.ts +++ b/packages/server/src/sdk/app/tables/internal/sqs.ts @@ -14,7 +14,7 @@ import { CONSTANT_INTERNAL_ROW_COLS, generateJunctionTableID, } from "../../../../db/utils" -import { isEqual } from "lodash" +import { isEqual, merge } from "lodash" import { DEFAULT_TABLES } from "../../../../db/defaultData/datasource_bb_default" const FieldTypeMap: Record = { @@ -130,9 +130,18 @@ async function buildBaseDefinition(): Promise { const defaultTables = DEFAULT_TABLES const definition = sql.designDoc.base("tableId") for (let table of tables.concat(defaultTables)) { + const tableId = table._id! + let existing = definition.sql.tables[tableId] + let mapped = mapTable(table) + // there are multiple definitions for this table (default table overlap) + // when there is overlap - we have to make sure we have columns from all definitions + // this problem really only applies to sample data tables where they've been expanded + if (existing) { + mapped[tableId] = merge(mapped[tableId], existing) + } definition.sql.tables = { ...definition.sql.tables, - ...mapTable(table), + ...mapped, } } return definition