From 79c292538c70b7d2d2697eb78d2d3207955647cc Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 12 Jul 2024 13:51:06 +0100 Subject: [PATCH 1/2] 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 From 745a05fe8d34517715a968cd9d562cf77b2140f8 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 12 Jul 2024 13:54:55 +0100 Subject: [PATCH 2/2] Updating how the 'merging' is handled, don't include the in-memory representation if it exists on disk in Couch, prefer that. --- .../server/src/sdk/app/tables/internal/sqs.ts | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/packages/server/src/sdk/app/tables/internal/sqs.ts b/packages/server/src/sdk/app/tables/internal/sqs.ts index aeaa33ed2d..9db10f2b41 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, merge } from "lodash" +import { isEqual } from "lodash" import { DEFAULT_TABLES } from "../../../../db/defaultData/datasource_bb_default" const FieldTypeMap: Record = { @@ -127,21 +127,17 @@ 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.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) + for (const defaultTable of DEFAULT_TABLES) { + // the default table doesn't exist in Couch, use the in-memory representation + if (!tables.find(table => table._id === defaultTable._id)) { + tables.push(defaultTable) } + } + const definition = sql.designDoc.base("tableId") + for (let table of tables) { definition.sql.tables = { ...definition.sql.tables, - ...mapped, + ...mapTable(table), } } return definition