From a268e5560718e317180f8da7c71667b218c59cc1 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Tue, 30 Jan 2024 17:31:11 +0000 Subject: [PATCH 01/29] Plumbing for showing a maintenance page when SQS is required but missing. --- .../builder/src/pages/builder/_layout.svelte | 4 +++ .../pages/builder/maintenance/index.svelte | 27 +++++++++++++++++++ .../portal/_components/BudibaseLogo.svelte | 13 +++++++++ packages/builder/src/stores/portal/admin.js | 2 ++ packages/types/src/core/installation.ts | 4 +++ .../src/api/controllers/system/environment.ts | 19 ++++++++++--- .../routes/system/tests/environment.spec.ts | 4 +-- 7 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 packages/builder/src/pages/builder/maintenance/index.svelte create mode 100644 packages/builder/src/pages/builder/portal/_components/BudibaseLogo.svelte diff --git a/packages/builder/src/pages/builder/_layout.svelte b/packages/builder/src/pages/builder/_layout.svelte index 62d3951fb5..95ca05b87b 100644 --- a/packages/builder/src/pages/builder/_layout.svelte +++ b/packages/builder/src/pages/builder/_layout.svelte @@ -71,6 +71,10 @@ await auth.getSelf() await admin.init() + if ($admin.maintenance.length > 0) { + $redirect("./maintenance") + } + if ($auth.user) { await licensing.init() } diff --git a/packages/builder/src/pages/builder/maintenance/index.svelte b/packages/builder/src/pages/builder/maintenance/index.svelte new file mode 100644 index 0000000000..bdadd98c42 --- /dev/null +++ b/packages/builder/src/pages/builder/maintenance/index.svelte @@ -0,0 +1,27 @@ + + +{#each $admin.maintenance as maintenance} + {#if maintenance.type === MaintenanceType.SQS_MISSING} + + + + Please upgrade your Budibase installation + + We've detected that the version of Budibase you're using depends on a + more recent version of the CouchDB database than what you have + installed. + + + To resolve this, you can either rollback to a previous version of + Budibase, or follow the migration guide here + to update to a later version of CouchDB. + + + + {/if} +{/each} diff --git a/packages/builder/src/pages/builder/portal/_components/BudibaseLogo.svelte b/packages/builder/src/pages/builder/portal/_components/BudibaseLogo.svelte new file mode 100644 index 0000000000..8276510201 --- /dev/null +++ b/packages/builder/src/pages/builder/portal/_components/BudibaseLogo.svelte @@ -0,0 +1,13 @@ + + +Budibase Logo $goto("./apps")} /> + + diff --git a/packages/builder/src/stores/portal/admin.js b/packages/builder/src/stores/portal/admin.js index 2106acac27..29d4585c06 100644 --- a/packages/builder/src/stores/portal/admin.js +++ b/packages/builder/src/stores/portal/admin.js @@ -17,6 +17,7 @@ export const DEFAULT_CONFIG = { adminUser: { checked: false }, sso: { checked: false }, }, + maintenance: [], offlineMode: false, } @@ -48,6 +49,7 @@ export function createAdminStore() { store.isDev = environment.isDev store.baseUrl = environment.baseUrl store.offlineMode = environment.offlineMode + store.maintenance = environment.maintenance return store }) } diff --git a/packages/types/src/core/installation.ts b/packages/types/src/core/installation.ts index 7679290f36..ec89e439d9 100644 --- a/packages/types/src/core/installation.ts +++ b/packages/types/src/core/installation.ts @@ -2,3 +2,7 @@ export enum ServiceType { WORKER = "worker", APPS = "apps", } + +export enum MaintenanceType { + SQS_MISSING = "sqs_missing", +} diff --git a/packages/worker/src/api/controllers/system/environment.ts b/packages/worker/src/api/controllers/system/environment.ts index bf9270607f..6171ccf4db 100644 --- a/packages/worker/src/api/controllers/system/environment.ts +++ b/packages/worker/src/api/controllers/system/environment.ts @@ -1,10 +1,18 @@ -import { Ctx } from "@budibase/types" +import { Ctx, MaintenanceType } from "@budibase/types" import env from "../../../environment" import { env as coreEnv } from "@budibase/backend-core" import nodeFetch from "node-fetch" +// When we come to move to SQS fully and move away from Clouseau, we will need +// to flip this to true (or remove it entirely). This will then be used to +// determine if we should show the maintenance page that links to the SQS +// migration docs. +const sqsRequired = false + let sqsAvailable: boolean async function isSqsAvailable() { + // We cache this value for the duration of the Node process because we don't + // want every page load to be making this relatively expensive check. if (sqsAvailable !== undefined) { return sqsAvailable } @@ -21,6 +29,10 @@ async function isSqsAvailable() { } } +async function isSqsMissing() { + return sqsRequired && !(await isSqsAvailable()) +} + export const fetch = async (ctx: Ctx) => { ctx.body = { multiTenancy: !!env.MULTI_TENANCY, @@ -33,8 +45,9 @@ export const fetch = async (ctx: Ctx) => { } if (env.SELF_HOSTED) { - ctx.body.infrastructure = { - sqs: await isSqsAvailable(), + ctx.body.maintenance = [] + if (await isSqsMissing()) { + ctx.body.maintenance.push({ type: MaintenanceType.SQS_MISSING }) } } } diff --git a/packages/worker/src/api/routes/system/tests/environment.spec.ts b/packages/worker/src/api/routes/system/tests/environment.spec.ts index 2efbfa07c9..4dee77d3a9 100644 --- a/packages/worker/src/api/routes/system/tests/environment.spec.ts +++ b/packages/worker/src/api/routes/system/tests/environment.spec.ts @@ -40,9 +40,7 @@ describe("/api/system/environment", () => { multiTenancy: true, baseUrl: "http://localhost:10000", offlineMode: false, - infrastructure: { - sqs: false, - }, + maintenance: [], }) }) }) From 281b88a86adac77f06e6295cb584095aec87d046 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 31 Jan 2024 15:54:36 +0000 Subject: [PATCH 02/29] Respond to PR feedback. --- packages/worker/src/api/controllers/system/environment.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/worker/src/api/controllers/system/environment.ts b/packages/worker/src/api/controllers/system/environment.ts index 6171ccf4db..203d3d41ff 100644 --- a/packages/worker/src/api/controllers/system/environment.ts +++ b/packages/worker/src/api/controllers/system/environment.ts @@ -42,10 +42,10 @@ export const fetch = async (ctx: Ctx) => { disableAccountPortal: env.DISABLE_ACCOUNT_PORTAL, baseUrl: env.PLATFORM_URL, isDev: env.isDev() && !env.isTest(), + maintenance: [], } if (env.SELF_HOSTED) { - ctx.body.maintenance = [] if (await isSqsMissing()) { ctx.body.maintenance.push({ type: MaintenanceType.SQS_MISSING }) } From b52ba43979b2d7a397356300c5e291edf0d3b35a Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 31 Jan 2024 15:56:17 +0000 Subject: [PATCH 03/29] Fix tests. --- packages/worker/src/api/routes/system/tests/environment.spec.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/worker/src/api/routes/system/tests/environment.spec.ts b/packages/worker/src/api/routes/system/tests/environment.spec.ts index 4dee77d3a9..dbe9be7374 100644 --- a/packages/worker/src/api/routes/system/tests/environment.spec.ts +++ b/packages/worker/src/api/routes/system/tests/environment.spec.ts @@ -27,6 +27,7 @@ describe("/api/system/environment", () => { multiTenancy: true, baseUrl: "http://localhost:10000", offlineMode: false, + maintenance: [], }) }) From 5dbe68a9b1d9342afce35a9b6142c40cd7661861 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 21 Feb 2024 11:03:58 +0000 Subject: [PATCH 04/29] Restyle maintenance page. --- .../pages/builder/maintenance/index.svelte | 123 +++++++++++++++--- 1 file changed, 102 insertions(+), 21 deletions(-) diff --git a/packages/builder/src/pages/builder/maintenance/index.svelte b/packages/builder/src/pages/builder/maintenance/index.svelte index bdadd98c42..0658ac3e3a 100644 --- a/packages/builder/src/pages/builder/maintenance/index.svelte +++ b/packages/builder/src/pages/builder/maintenance/index.svelte @@ -1,27 +1,108 @@ -{#each $admin.maintenance as maintenance} - {#if maintenance.type === MaintenanceType.SQS_MISSING} - - - - Please upgrade your Budibase installation - - We've detected that the version of Budibase you're using depends on a - more recent version of the CouchDB database than what you have - installed. - - - To resolve this, you can either rollback to a previous version of - Budibase, or follow the migration guide here - to update to a later version of CouchDB. - - - - {/if} -{/each} +
+
+
+ +
+
+ {#each $admin.maintenance as maintenance} + {#if maintenance.type === MaintenanceType.SQS_MISSING} +

Please upgrade your Budibase installation

+

+ We've detected that the version of Budibase you're using depends on + a more recent version of the CouchDB database than what you have + installed. +

+

+ To resolve this, you can either rollback to a previous version of + Budibase, or follow the migration guide to update to a later version + of CouchDB. +

+ + {/if} + {/each} +
+
+
+ + From 19803f91826feb1163ac3dbd416e38e9efb9c8ee Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Mon, 11 Mar 2024 15:19:23 +0000 Subject: [PATCH 05/29] Use BBUI components instead of raw HTML. --- .../pages/builder/maintenance/index.svelte | 86 ++++++------------- .../src/api/controllers/system/environment.ts | 3 + 2 files changed, 30 insertions(+), 59 deletions(-) diff --git a/packages/builder/src/pages/builder/maintenance/index.svelte b/packages/builder/src/pages/builder/maintenance/index.svelte index 0658ac3e3a..95080d0563 100644 --- a/packages/builder/src/pages/builder/maintenance/index.svelte +++ b/packages/builder/src/pages/builder/maintenance/index.svelte @@ -1,5 +1,6 @@ @@ -9,25 +10,26 @@
-
+
{#each $admin.maintenance as maintenance} {#if maintenance.type === MaintenanceType.SQS_MISSING} -

Please upgrade your Budibase installation

-

- We've detected that the version of Budibase you're using depends on - a more recent version of the CouchDB database than what you have - installed. -

-

- To resolve this, you can either rollback to a previous version of - Budibase, or follow the migration guide to update to a later version - of CouchDB. -

- + + Please upgrade your Budibase installation + + We've detected that the version of Budibase you're using depends + on a more recent version of the CouchDB database than what you + have installed. + + + To resolve this, you can either rollback to a previous version of + Budibase, or follow the migration guide to update to a later + version of CouchDB. + + + {/if} {/each}
@@ -35,27 +37,7 @@
diff --git a/packages/client/src/index.js b/packages/client/src/index.js index 2c8d310619..9a2f0addbc 100644 --- a/packages/client/src/index.js +++ b/packages/client/src/index.js @@ -1,5 +1,6 @@ import ClientApp from "./components/ClientApp.svelte" import UpdatingApp from "./components/UpdatingApp.svelte" +import MaintenanceScreen from "./components/MaintenanceScreen.svelte" import { builderStore, appStore, @@ -65,6 +66,15 @@ const loadBudibase = async () => { await environmentStore.actions.fetchEnvironment() } + const maintenanceList = get(environmentStore)?.maintenance + if (maintenanceList?.length > 0) { + new MaintenanceScreen({ + target: window.document.body, + props: { maintenanceList }, + }) + return + } + // Register handler for runtime events from the builder window.handleBuilderRuntimeEvent = (type, data) => { if (!window["##BUDIBASE_IN_BUILDER##"]) { From 186f916b4084a0d99811d141b9533bf203421c72 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Tue, 12 Mar 2024 09:57:59 +0000 Subject: [PATCH 07/29] Get tests passing against a real MySQL. --- packages/server/src/api/routes/tests/row.spec.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index a032f4324c..5051fb1a5b 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -38,11 +38,16 @@ import * as uuid from "uuid" const timestamp = new Date("2023-01-26T11:48:57.597Z").toISOString() tk.freeze(timestamp) +jest.setTimeout(99999999) +jest.unmock("mysql2") +jest.unmock("mysql2/promise") + const { basicRow } = setup.structures describe.each([ - ["internal", undefined], - ["postgres", databaseTestProviders.postgres], + // ["internal", undefined], + // ["postgres", databaseTestProviders.postgres], + ["mysql", databaseTestProviders.mysql], ])("/rows (%s)", (__, dsProvider) => { const isInternal = !dsProvider @@ -70,7 +75,7 @@ describe.each([ const generateTableConfig: () => SaveTableRequest = () => { return { - name: uuid.v4(), + name: uuid.v4().substring(0, 16), type: "table", primary: ["id"], primaryDisplay: "name", @@ -641,7 +646,7 @@ describe.each([ const createdRow = await config.createRow() const res = await config.api.row.bulkDelete(table._id!, { - rows: [createdRow, { _id: "2" }], + rows: [createdRow, { _id: "9999999" }], }) expect(res[0]._id).toEqual(createdRow._id) From ce209a16b3f85fbd6151b2a38cfa9e7976378be0 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Tue, 12 Mar 2024 10:42:42 +0000 Subject: [PATCH 08/29] Get tests running with SQL Server. Need to make them pass next. --- .../server/src/api/routes/tests/row.spec.ts | 4 +- .../src/integrations/tests/utils/index.ts | 3 +- .../src/integrations/tests/utils/mssql.ts | 53 +++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 packages/server/src/integrations/tests/utils/mssql.ts diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index 5051fb1a5b..bf96dd62b5 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -41,13 +41,15 @@ tk.freeze(timestamp) jest.setTimeout(99999999) jest.unmock("mysql2") jest.unmock("mysql2/promise") +jest.unmock("mssql") const { basicRow } = setup.structures describe.each([ // ["internal", undefined], // ["postgres", databaseTestProviders.postgres], - ["mysql", databaseTestProviders.mysql], + // ["mysql", databaseTestProviders.mysql], + ["mssql", databaseTestProviders.mssql], ])("/rows (%s)", (__, dsProvider) => { const isInternal = !dsProvider diff --git a/packages/server/src/integrations/tests/utils/index.ts b/packages/server/src/integrations/tests/utils/index.ts index b6e4e43e7a..a5282bff6f 100644 --- a/packages/server/src/integrations/tests/utils/index.ts +++ b/packages/server/src/integrations/tests/utils/index.ts @@ -4,6 +4,7 @@ import { Datasource } from "@budibase/types" import * as postgres from "./postgres" import * as mongodb from "./mongodb" import * as mysql from "./mysql" +import * as mssql from "./mssql" import { StartedTestContainer } from "testcontainers" jest.setTimeout(30000) @@ -14,4 +15,4 @@ export interface DatabaseProvider { datasource(): Promise } -export const databaseTestProviders = { postgres, mongodb, mysql } +export const databaseTestProviders = { postgres, mongodb, mysql, mssql } diff --git a/packages/server/src/integrations/tests/utils/mssql.ts b/packages/server/src/integrations/tests/utils/mssql.ts new file mode 100644 index 0000000000..f548f0c42c --- /dev/null +++ b/packages/server/src/integrations/tests/utils/mssql.ts @@ -0,0 +1,53 @@ +import { Datasource, SourceName } from "@budibase/types" +import { GenericContainer, Wait, StartedTestContainer } from "testcontainers" + +let container: StartedTestContainer | undefined + +export async function start(): Promise { + return await new GenericContainer( + "mcr.microsoft.com/mssql/server:2022-latest" + ) + .withExposedPorts(1433) + .withEnvironment({ + ACCEPT_EULA: "Y", + MSSQL_SA_PASSWORD: "Password_123", + // This is important, as Microsoft allow us to use the "Developer" edition + // of SQL Server for development and testing purposes. We can't use other + // versions without a valid license, and we cannot use the Developer + // version in production. + MSSQL_PID: "Developer", + }) + .withWaitStrategy( + Wait.forSuccessfulCommand( + "/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P Password_123 -q 'SELECT 1'" + ) + ) + .start() +} + +export async function datasource(): Promise { + if (!container) { + container = await start() + } + const host = container.getHost() + const port = container.getMappedPort(1433) + + return { + type: "datasource_plus", + source: SourceName.SQL_SERVER, + plus: true, + config: { + server: host, + port, + user: "sa", + password: "Password_123", + }, + } +} + +export async function stop() { + if (container) { + await container.stop() + container = undefined + } +} From d1f876d67f35541bc88d4fd034cdaf89b94893d9 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Tue, 12 Mar 2024 10:55:58 +0000 Subject: [PATCH 09/29] Fix test that was failing because SQL Server doesn't allow you to insert values into primary key columns unless you set a setting. --- packages/server/src/api/routes/tests/row.spec.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index bf96dd62b5..cd8580c64b 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -438,7 +438,7 @@ describe.each([ }) describe("view save", () => { - it("views have extra data trimmed", async () => { + it.only("views have extra data trimmed", async () => { const table = await createTable({ type: "table", name: "orders", @@ -474,7 +474,6 @@ describe.each([ const createRowResponse = await config.api.row.save( createViewResponse.id, { - OrderID: "1111", Country: "Aussy", Story: "aaaaa", } @@ -484,7 +483,7 @@ describe.each([ expect(row.Story).toBeUndefined() expect(row).toEqual({ ...defaultRowFields, - OrderID: 1111, + OrderID: createRowResponse.OrderID, Country: "Aussy", _id: createRowResponse._id, _rev: createRowResponse._rev, From 477d17b53ecd2f904b470fb93b09631734a71610 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Tue, 12 Mar 2024 12:25:30 +0000 Subject: [PATCH 10/29] Making progress on getting SQL Server working. --- .../server/src/api/routes/tests/row.spec.ts | 64 +++++++++---------- .../src/integrations/microsoftSqlServer.ts | 30 ++++++++- packages/server/src/sdk/app/rows/search.ts | 8 +-- 3 files changed, 61 insertions(+), 41 deletions(-) diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index cd8580c64b..e95d441981 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -438,7 +438,7 @@ describe.each([ }) describe("view save", () => { - it.only("views have extra data trimmed", async () => { + it("views have extra data trimmed", async () => { const table = await createTable({ type: "table", name: "orders", @@ -1604,35 +1604,35 @@ describe.each([ }) describe.each([ - [ - "relationship fields", - (): Record => ({ - user: { - name: "user", - relationshipType: RelationshipType.ONE_TO_MANY, - type: FieldType.LINK, - tableId: o2mTable._id!, - fieldName: "fk_o2m", - }, - users: { - name: "users", - relationshipType: RelationshipType.MANY_TO_MANY, - type: FieldType.LINK, - tableId: m2mTable._id!, - fieldName: "fk_m2m", - }, - }), - (tableId: string) => - config.api.row.save(tableId, { - name: uuid.v4(), - description: generator.paragraph(), - tableId, - }), - (row: Row) => ({ - _id: row._id, - primaryDisplay: row.name, - }), - ], + // [ + // "relationship fields", + // (): Record => ({ + // user: { + // name: "user", + // relationshipType: RelationshipType.ONE_TO_MANY, + // type: FieldType.LINK, + // tableId: o2mTable._id!, + // fieldName: "fk_o2m", + // }, + // users: { + // name: "users", + // relationshipType: RelationshipType.MANY_TO_MANY, + // type: FieldType.LINK, + // tableId: m2mTable._id!, + // fieldName: "fk_m2m", + // }, + // }), + // (tableId: string) => + // config.api.row.save(tableId, { + // name: uuid.v4(), + // description: generator.paragraph(), + // tableId, + // }), + // (row: Row) => ({ + // _id: row._id, + // primaryDisplay: row.name, + // }), + // ], [ "bb reference fields", (): Record => ({ @@ -1960,8 +1960,8 @@ describe.each([ ] await config.api.row.save(tableId, rows[0]) - await config.api.row.save(tableId, rows[1]) - await config.api.row.save(tableId, rows[2]) + // await config.api.row.save(tableId, rows[1]) + // await config.api.row.save(tableId, rows[2]) const res = await config.api.row.search(tableId) diff --git a/packages/server/src/integrations/microsoftSqlServer.ts b/packages/server/src/integrations/microsoftSqlServer.ts index f87e248ac0..e53d2ddc44 100644 --- a/packages/server/src/integrations/microsoftSqlServer.ts +++ b/packages/server/src/integrations/microsoftSqlServer.ts @@ -14,6 +14,8 @@ import { Schema, TableSourceType, DatasourcePlusQueryResponse, + FieldType, + FieldSubtype, } from "@budibase/types" import { getSqlQuery, @@ -26,7 +28,7 @@ import { import Sql from "./base/sql" import { MSSQLTablesResponse, MSSQLColumn } from "./base/types" import { getReadableErrorMessage } from "./base/errorMapping" -import sqlServer from "mssql" +import sqlServer, { IRecordSet, IResult } from "mssql" const DEFAULT_SCHEMA = "dbo" @@ -503,10 +505,34 @@ class SqlServerIntegration extends Sql implements DatasourcePlus { const operation = this._operation(json) const queryFn = (query: any, op: string) => this.internalQuery(query, op) const processFn = (result: any) => - result.recordset ? result.recordset : [{ [operation]: true }] + result.recordset + ? this._postProcessJson(json, result.recordset) + : [{ [operation]: true }] return this.queryWithReturning(json, queryFn, processFn) } + _postProcessJson(json: QueryJson, results: IRecordSet) { + const table = json.meta?.table + if (!table) { + return results + } + for (const [name, field] of Object.entries(table.schema)) { + if ( + field.type === FieldType.JSON || + (field.type === FieldType.BB_REFERENCE && + field.subtype === FieldSubtype.USERS) + ) { + const fullName = `${table.name}.${name}` + for (let row of results) { + if (typeof row[fullName] === "string") { + row[fullName] = JSON.parse(row[fullName]) + } + } + } + } + return results + } + async getExternalSchema() { // Query to retrieve table schema const query = ` diff --git a/packages/server/src/sdk/app/rows/search.ts b/packages/server/src/sdk/app/rows/search.ts index 8b24f9bc5f..63bbd699fa 100644 --- a/packages/server/src/sdk/app/rows/search.ts +++ b/packages/server/src/sdk/app/rows/search.ts @@ -1,10 +1,4 @@ -import { - Row, - SearchFilters, - SearchParams, - SortOrder, - SortType, -} from "@budibase/types" +import { Row, SearchFilters, SearchParams, SortOrder } from "@budibase/types" import { isExternalTableID } from "../../../integrations/utils" import * as internal from "./search/internal" import * as external from "./search/external" From 1334f5dcc58066f3714cc675f11f69e1fd4650b9 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Tue, 12 Mar 2024 14:46:52 +0000 Subject: [PATCH 11/29] SQL Server fully passing. --- .../server/src/api/routes/tests/row.spec.ts | 68 +++++++++---------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index e95d441981..f902084a48 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -46,9 +46,9 @@ jest.unmock("mssql") const { basicRow } = setup.structures describe.each([ - // ["internal", undefined], - // ["postgres", databaseTestProviders.postgres], - // ["mysql", databaseTestProviders.mysql], + ["internal", undefined], + ["postgres", databaseTestProviders.postgres], + ["mysql", databaseTestProviders.mysql], ["mssql", databaseTestProviders.mssql], ])("/rows (%s)", (__, dsProvider) => { const isInternal = !dsProvider @@ -1604,35 +1604,35 @@ describe.each([ }) describe.each([ - // [ - // "relationship fields", - // (): Record => ({ - // user: { - // name: "user", - // relationshipType: RelationshipType.ONE_TO_MANY, - // type: FieldType.LINK, - // tableId: o2mTable._id!, - // fieldName: "fk_o2m", - // }, - // users: { - // name: "users", - // relationshipType: RelationshipType.MANY_TO_MANY, - // type: FieldType.LINK, - // tableId: m2mTable._id!, - // fieldName: "fk_m2m", - // }, - // }), - // (tableId: string) => - // config.api.row.save(tableId, { - // name: uuid.v4(), - // description: generator.paragraph(), - // tableId, - // }), - // (row: Row) => ({ - // _id: row._id, - // primaryDisplay: row.name, - // }), - // ], + [ + "relationship fields", + (): Record => ({ + user: { + name: "user", + relationshipType: RelationshipType.ONE_TO_MANY, + type: FieldType.LINK, + tableId: o2mTable._id!, + fieldName: "fk_o2m", + }, + users: { + name: "users", + relationshipType: RelationshipType.MANY_TO_MANY, + type: FieldType.LINK, + tableId: m2mTable._id!, + fieldName: "fk_m2m", + }, + }), + (tableId: string) => + config.api.row.save(tableId, { + name: uuid.v4(), + description: generator.paragraph(), + tableId, + }), + (row: Row) => ({ + _id: row._id, + primaryDisplay: row.name, + }), + ], [ "bb reference fields", (): Record => ({ @@ -1960,8 +1960,8 @@ describe.each([ ] await config.api.row.save(tableId, rows[0]) - // await config.api.row.save(tableId, rows[1]) - // await config.api.row.save(tableId, rows[2]) + await config.api.row.save(tableId, rows[1]) + await config.api.row.save(tableId, rows[2]) const res = await config.api.row.search(tableId) From aff0209176e00fb9557a699687a8aa07636beec7 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Tue, 12 Mar 2024 15:27:34 +0000 Subject: [PATCH 12/29] MariaDB tests passing. --- .../server/src/api/routes/tests/row.spec.ts | 1 + packages/server/src/integrations/base/sql.ts | 33 +++++++++++ .../src/integrations/microsoftSqlServer.ts | 32 +++------- packages/server/src/integrations/mysql.ts | 32 +++++++++- .../src/integrations/tests/utils/index.ts | 9 ++- .../src/integrations/tests/utils/mariadb.ts | 58 +++++++++++++++++++ 6 files changed, 138 insertions(+), 27 deletions(-) create mode 100644 packages/server/src/integrations/tests/utils/mariadb.ts diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index f902084a48..89da4175d9 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -50,6 +50,7 @@ describe.each([ ["postgres", databaseTestProviders.postgres], ["mysql", databaseTestProviders.mysql], ["mssql", databaseTestProviders.mssql], + ["mariadb", databaseTestProviders.mariadb], ])("/rows (%s)", (__, dsProvider) => { const isInternal = !dsProvider diff --git a/packages/server/src/integrations/base/sql.ts b/packages/server/src/integrations/base/sql.ts index be1883c8ec..e6b9f047b9 100644 --- a/packages/server/src/integrations/base/sql.ts +++ b/packages/server/src/integrations/base/sql.ts @@ -4,11 +4,15 @@ import { QueryOptions } from "../../definitions/datasource" import { isIsoDateString, SqlClient, isValidFilter } from "../utils" import SqlTableQueryBuilder from "./sqlTable" import { + FieldSchema, + FieldSubtype, + FieldType, Operation, QueryJson, RelationshipsJson, SearchFilters, SortDirection, + Table, } from "@budibase/types" import environment from "../../environment" @@ -691,6 +695,35 @@ class SqlQueryBuilder extends SqlTableQueryBuilder { return results.length ? results : [{ [operation.toLowerCase()]: true }] } + convertJsonStringColumns( + table: Table, + results: Record[] + ): Record[] { + for (const [name, field] of Object.entries(table.schema)) { + if (!this._isJsonColumn(field)) { + continue + } + const fullName = `${table.name}.${name}` + for (let row of results) { + if (typeof row[fullName] === "string") { + row[fullName] = JSON.parse(row[fullName]) + } + if (typeof row[name] === "string") { + row[name] = JSON.parse(row[name]) + } + } + } + return results + } + + _isJsonColumn(field: FieldSchema) { + return ( + field.type === FieldType.JSON || + (field.type === FieldType.BB_REFERENCE && + field.subtype === FieldSubtype.USERS) + ) + } + log(query: string, values?: any[]) { if (!environment.SQL_LOGGING_ENABLE) { return diff --git a/packages/server/src/integrations/microsoftSqlServer.ts b/packages/server/src/integrations/microsoftSqlServer.ts index e53d2ddc44..0c9b8f4547 100644 --- a/packages/server/src/integrations/microsoftSqlServer.ts +++ b/packages/server/src/integrations/microsoftSqlServer.ts @@ -504,33 +504,15 @@ class SqlServerIntegration extends Sql implements DatasourcePlus { } const operation = this._operation(json) const queryFn = (query: any, op: string) => this.internalQuery(query, op) - const processFn = (result: any) => - result.recordset - ? this._postProcessJson(json, result.recordset) - : [{ [operation]: true }] - return this.queryWithReturning(json, queryFn, processFn) - } - - _postProcessJson(json: QueryJson, results: IRecordSet) { - const table = json.meta?.table - if (!table) { - return results - } - for (const [name, field] of Object.entries(table.schema)) { - if ( - field.type === FieldType.JSON || - (field.type === FieldType.BB_REFERENCE && - field.subtype === FieldSubtype.USERS) - ) { - const fullName = `${table.name}.${name}` - for (let row of results) { - if (typeof row[fullName] === "string") { - row[fullName] = JSON.parse(row[fullName]) - } - } + const processFn = (result: any) => { + if (json?.meta?.table && result.recordset) { + return this.convertJsonStringColumns(json.meta.table, result.recordset) + } else if (result.recordset) { + return result.recordset } + return [{ [operation]: true }] } - return results + return this.queryWithReturning(json, queryFn, processFn) } async getExternalSchema() { diff --git a/packages/server/src/integrations/mysql.ts b/packages/server/src/integrations/mysql.ts index f629381807..1f50525675 100644 --- a/packages/server/src/integrations/mysql.ts +++ b/packages/server/src/integrations/mysql.ts @@ -13,6 +13,8 @@ import { Schema, TableSourceType, DatasourcePlusQueryResponse, + FieldType, + FieldSubtype, } from "@budibase/types" import { getSqlQuery, @@ -386,12 +388,40 @@ class MySQLIntegration extends Sql implements DatasourcePlus { try { const queryFn = (query: any) => this.internalQuery(query, { connect: false, disableCoercion: true }) - return await this.queryWithReturning(json, queryFn) + const processFn = (result: any) => { + if (json?.meta?.table && Array.isArray(result)) { + return this.convertJsonStringColumns(json.meta.table, result) + } + return result + } + return await this.queryWithReturning(json, queryFn, processFn) } finally { await this.disconnect() } } + _postProcessJson(json: QueryJson, results: any) { + const table = json.meta?.table + if (!table) { + return results + } + for (const [name, field] of Object.entries(table.schema)) { + if ( + field.type === FieldType.JSON || + (field.type === FieldType.BB_REFERENCE && + field.subtype === FieldSubtype.USERS) + ) { + const fullName = `${table.name}.${name}` + for (let row of results) { + if (typeof row[fullName] === "string") { + row[fullName] = JSON.parse(row[fullName]) + } + } + } + } + return results + } + async getExternalSchema() { try { const [databaseResult] = await this.internalQuery({ diff --git a/packages/server/src/integrations/tests/utils/index.ts b/packages/server/src/integrations/tests/utils/index.ts index a5282bff6f..b2be3df4e0 100644 --- a/packages/server/src/integrations/tests/utils/index.ts +++ b/packages/server/src/integrations/tests/utils/index.ts @@ -5,6 +5,7 @@ import * as postgres from "./postgres" import * as mongodb from "./mongodb" import * as mysql from "./mysql" import * as mssql from "./mssql" +import * as mariadb from "./mariadb" import { StartedTestContainer } from "testcontainers" jest.setTimeout(30000) @@ -15,4 +16,10 @@ export interface DatabaseProvider { datasource(): Promise } -export const databaseTestProviders = { postgres, mongodb, mysql, mssql } +export const databaseTestProviders = { + postgres, + mongodb, + mysql, + mssql, + mariadb, +} diff --git a/packages/server/src/integrations/tests/utils/mariadb.ts b/packages/server/src/integrations/tests/utils/mariadb.ts new file mode 100644 index 0000000000..a097e0aaa1 --- /dev/null +++ b/packages/server/src/integrations/tests/utils/mariadb.ts @@ -0,0 +1,58 @@ +import { Datasource, SourceName } from "@budibase/types" +import { GenericContainer, Wait, StartedTestContainer } from "testcontainers" +import { AbstractWaitStrategy } from "testcontainers/build/wait-strategies/wait-strategy" + +let container: StartedTestContainer | undefined + +class MariaDBWaitStrategy extends AbstractWaitStrategy { + async waitUntilReady(container: any, boundPorts: any, startTime?: Date) { + // Because MariaDB first starts itself up, runs an init script, then restarts, + // it's possible for the mysqladmin ping to succeed early and then tests to + // run against a MariaDB that's mid-restart and fail. To get around this, we + // wait for logs and then do a ping check. + + const logs = Wait.forLogMessage("mariadbd: ready for connections", 2) + await logs.waitUntilReady(container, boundPorts, startTime) + + const command = Wait.forSuccessfulCommand( + `mysqladmin ping -h localhost -P 3306 -u root -ppassword` + ) + await command.waitUntilReady(container) + } +} + +export async function start(): Promise { + return await new GenericContainer("mariadb:lts") + .withExposedPorts(3306) + .withEnvironment({ MARIADB_ROOT_PASSWORD: "password" }) + .withWaitStrategy(new MariaDBWaitStrategy()) + .start() +} + +export async function datasource(): Promise { + if (!container) { + container = await start() + } + const host = container.getHost() + const port = container.getMappedPort(3306) + + return { + type: "datasource_plus", + source: SourceName.MYSQL, + plus: true, + config: { + host, + port, + user: "root", + password: "password", + database: "mysql", + }, + } +} + +export async function stop() { + if (container) { + await container.stop() + container = undefined + } +} From a70cb903f6aa0cb61656e7345b08ee5e4b811fea Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Tue, 12 Mar 2024 17:17:01 +0000 Subject: [PATCH 13/29] Remove jest timeout. --- packages/server/src/api/routes/tests/row.spec.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index 89da4175d9..d50dd8a3d9 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -38,7 +38,6 @@ import * as uuid from "uuid" const timestamp = new Date("2023-01-26T11:48:57.597Z").toISOString() tk.freeze(timestamp) -jest.setTimeout(99999999) jest.unmock("mysql2") jest.unmock("mysql2/promise") jest.unmock("mssql") From d061c19c80233f10f18514ac41f4e3fb8f4bfd09 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Tue, 12 Mar 2024 17:21:33 +0000 Subject: [PATCH 14/29] Remove some extraneous, unused code. --- .../src/integrations/microsoftSqlServer.ts | 2 +- packages/server/src/integrations/mysql.ts | 22 ------------------- 2 files changed, 1 insertion(+), 23 deletions(-) diff --git a/packages/server/src/integrations/microsoftSqlServer.ts b/packages/server/src/integrations/microsoftSqlServer.ts index 0c9b8f4547..c79eb136ed 100644 --- a/packages/server/src/integrations/microsoftSqlServer.ts +++ b/packages/server/src/integrations/microsoftSqlServer.ts @@ -28,7 +28,7 @@ import { import Sql from "./base/sql" import { MSSQLTablesResponse, MSSQLColumn } from "./base/types" import { getReadableErrorMessage } from "./base/errorMapping" -import sqlServer, { IRecordSet, IResult } from "mssql" +import sqlServer from "mssql" const DEFAULT_SCHEMA = "dbo" diff --git a/packages/server/src/integrations/mysql.ts b/packages/server/src/integrations/mysql.ts index 1f50525675..9638afa8ea 100644 --- a/packages/server/src/integrations/mysql.ts +++ b/packages/server/src/integrations/mysql.ts @@ -400,28 +400,6 @@ class MySQLIntegration extends Sql implements DatasourcePlus { } } - _postProcessJson(json: QueryJson, results: any) { - const table = json.meta?.table - if (!table) { - return results - } - for (const [name, field] of Object.entries(table.schema)) { - if ( - field.type === FieldType.JSON || - (field.type === FieldType.BB_REFERENCE && - field.subtype === FieldSubtype.USERS) - ) { - const fullName = `${table.name}.${name}` - for (let row of results) { - if (typeof row[fullName] === "string") { - row[fullName] = JSON.parse(row[fullName]) - } - } - } - } - return results - } - async getExternalSchema() { try { const [databaseResult] = await this.internalQuery({ From a5c8e8845f92a02e24c77f2793c2fde31e56b010 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 13 Mar 2024 13:38:08 +0000 Subject: [PATCH 15/29] Implement Adri's type guard suggestion. --- packages/server/src/integrations/base/sql.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/server/src/integrations/base/sql.ts b/packages/server/src/integrations/base/sql.ts index e6b9f047b9..8342c45fd7 100644 --- a/packages/server/src/integrations/base/sql.ts +++ b/packages/server/src/integrations/base/sql.ts @@ -4,9 +4,11 @@ import { QueryOptions } from "../../definitions/datasource" import { isIsoDateString, SqlClient, isValidFilter } from "../utils" import SqlTableQueryBuilder from "./sqlTable" import { + BBReferenceFieldMetadata, FieldSchema, FieldSubtype, FieldType, + JsonFieldMetadata, Operation, QueryJson, RelationshipsJson, @@ -716,7 +718,9 @@ class SqlQueryBuilder extends SqlTableQueryBuilder { return results } - _isJsonColumn(field: FieldSchema) { + _isJsonColumn( + field: FieldSchema + ): field is JsonFieldMetadata | BBReferenceFieldMetadata { return ( field.type === FieldType.JSON || (field.type === FieldType.BB_REFERENCE && From 0420734d9793403a6a119e9d48bdd32b0e9270ed Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 13 Mar 2024 16:48:32 +0100 Subject: [PATCH 16/29] Add failing test --- .../rowProcessor/tests/attachments.spec.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/server/src/utilities/rowProcessor/tests/attachments.spec.ts b/packages/server/src/utilities/rowProcessor/tests/attachments.spec.ts index 43af79d82c..beea04829b 100644 --- a/packages/server/src/utilities/rowProcessor/tests/attachments.spec.ts +++ b/packages/server/src/utilities/rowProcessor/tests/attachments.spec.ts @@ -115,4 +115,17 @@ describe("attachment cleanup", () => { await AttachmentCleanup.rowUpdate(table(), { row: row(), oldRow: row() }) expect(mockedDeleteFiles).not.toBeCalled() }) + + it("should be able to cleanup a column and not throw when attachments are undefined", async () => { + const originalTable = table() + delete originalTable.schema["attach"] + await AttachmentCleanup.tableUpdate( + originalTable, + [{ attach: undefined }], + { + oldTable: table(), + } + ) + expect(mockedDeleteFiles).not.toBeCalled() + }) }) From 940ff5acd24b00ff489cc7a3e41c334cde13becb Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 13 Mar 2024 16:48:58 +0100 Subject: [PATCH 17/29] Fix table changes with empty attachments --- packages/server/src/utilities/rowProcessor/attachments.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/server/src/utilities/rowProcessor/attachments.ts b/packages/server/src/utilities/rowProcessor/attachments.ts index c289680eb3..e1c83352d4 100644 --- a/packages/server/src/utilities/rowProcessor/attachments.ts +++ b/packages/server/src/utilities/rowProcessor/attachments.ts @@ -43,7 +43,7 @@ export class AttachmentCleanup { if ((columnRemoved && !renaming) || opts.deleting) { rows.forEach(row => { files = files.concat( - row[key].map((attachment: any) => attachment.key) + (row[key] || []).map((attachment: any) => attachment.key) ) }) } From 625c1dda9fd49444e20bdc044c7da865495e8b05 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 13 Mar 2024 16:54:39 +0100 Subject: [PATCH 18/29] Improve tests --- .../rowProcessor/tests/attachments.spec.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/server/src/utilities/rowProcessor/tests/attachments.spec.ts b/packages/server/src/utilities/rowProcessor/tests/attachments.spec.ts index beea04829b..3c58d2c056 100644 --- a/packages/server/src/utilities/rowProcessor/tests/attachments.spec.ts +++ b/packages/server/src/utilities/rowProcessor/tests/attachments.spec.ts @@ -121,7 +121,21 @@ describe("attachment cleanup", () => { delete originalTable.schema["attach"] await AttachmentCleanup.tableUpdate( originalTable, - [{ attach: undefined }], + [row("file 1"), { attach: undefined }, row("file 2")], + { + oldTable: table(), + } + ) + expect(mockedDeleteFiles).toBeCalledTimes(1) + expect(mockedDeleteFiles).toBeCalledWith(BUCKET, ["file 1", "file 2"]) + }) + + it("should be able to cleanup a column and not throw when ALL attachments are undefined", async () => { + const originalTable = table() + delete originalTable.schema["attach"] + await AttachmentCleanup.tableUpdate( + originalTable, + [{}, { attach: undefined }], { oldTable: table(), } From ed2926d924d7eceb8a8ada8d91672b8c4dbdccc0 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 13 Mar 2024 16:08:21 +0000 Subject: [PATCH 19/29] Significantly reduce reliance on the config.* methods on row.spec.ts, making them more independent. --- .../server/src/api/routes/tests/row.spec.ts | 606 ++++++++---------- 1 file changed, 275 insertions(+), 331 deletions(-) diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index d50dd8a3d9..cf4e7c26d0 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -7,6 +7,7 @@ import { context, InternalTable, roles, tenancy } from "@budibase/backend-core" import { quotas } from "@budibase/pro" import { AutoFieldSubType, + Datasource, DeleteRow, FieldSchema, FieldType, @@ -24,6 +25,7 @@ import { StaticQuotaName, Table, TableSourceType, + ViewV2, } from "@budibase/types" import { expectAnyExternalColsAttributes, @@ -32,7 +34,7 @@ import { mocks, structures, } from "@budibase/backend-core/tests" -import _ from "lodash" +import _, { merge } from "lodash" import * as uuid from "uuid" const timestamp = new Date("2023-01-26T11:48:57.597Z").toISOString() @@ -51,12 +53,21 @@ describe.each([ ["mssql", databaseTestProviders.mssql], ["mariadb", databaseTestProviders.mariadb], ])("/rows (%s)", (__, dsProvider) => { - const isInternal = !dsProvider - + const isInternal = dsProvider === undefined const request = setup.getRequest() const config = setup.getConfig() + let table: Table - let tableId: string + let datasource: Datasource | undefined + + beforeAll(async () => { + await config.init() + if (dsProvider) { + datasource = await config.createDatasource({ + datasource: await dsProvider.datasource(), + }) + } + }) afterAll(async () => { if (dsProvider) { @@ -65,24 +76,17 @@ describe.each([ setup.afterAll() }) - beforeAll(async () => { - await config.init() - - if (dsProvider) { - await config.createDatasource({ - datasource: await dsProvider.datasource(), - }) - } - }) - - const generateTableConfig: () => SaveTableRequest = () => { - return { + function saveTableRequest( + ...overrides: Partial[] + ): SaveTableRequest { + const req: SaveTableRequest = { name: uuid.v4().substring(0, 16), type: "table", + sourceType: datasource + ? TableSourceType.EXTERNAL + : TableSourceType.INTERNAL, + sourceId: datasource ? datasource._id! : INTERNAL_TABLE_SOURCE_ID, primary: ["id"], - primaryDisplay: "name", - sourceType: TableSourceType.INTERNAL, - sourceId: INTERNAL_TABLE_SOURCE_ID, schema: { id: { type: FieldType.AUTO, @@ -92,22 +96,36 @@ describe.each([ presence: true, }, }, - name: { - type: FieldType.STRING, - name: "name", - constraints: { - type: "string", + }, + } + return merge(req, ...overrides) + } + + function defaultTable( + ...overrides: Partial[] + ): SaveTableRequest { + return saveTableRequest( + { + primaryDisplay: "name", + schema: { + name: { + type: FieldType.STRING, + name: "name", + constraints: { + type: "string", + }, }, - }, - description: { - type: FieldType.STRING, - name: "description", - constraints: { - type: "string", + description: { + type: FieldType.STRING, + name: "description", + constraints: { + type: "string", + }, }, }, }, - } + ...overrides + ) } beforeEach(async () => { @@ -148,9 +166,7 @@ describe.each([ } beforeAll(async () => { - const tableConfig = generateTableConfig() - let table = await createTable(tableConfig) - tableId = table._id! + table = await config.api.table.save(defaultTable()) }) describe("save, load, update", () => { @@ -158,13 +174,13 @@ describe.each([ const rowUsage = await getRowUsage() const res = await request - .post(`/api/${tableId}/rows`) - .send(basicRow(tableId)) + .post(`/api/${table._id!}/rows`) + .send(basicRow(table._id!)) .set(config.defaultHeaders()) .expect("Content-Type", /json/) .expect(200) expect((res as any).res.statusMessage).toEqual( - `${config.table!.name} saved successfully` + `${table.name} saved successfully` ) expect(res.body.name).toEqual("Test Contact") expect(res.body._rev).toBeDefined() @@ -174,13 +190,10 @@ describe.each([ it("Increment row autoId per create row request", async () => { const rowUsage = await getRowUsage() - const tableConfig = generateTableConfig() - const newTable = await createTable( - { - ...tableConfig, + const newTable = await config.api.table.save( + saveTableRequest({ name: "TestTableAuto", schema: { - ...tableConfig.schema, "Row ID": { name: "Row ID", type: FieldType.NUMBER, @@ -197,37 +210,25 @@ describe.each([ }, }, }, - }, - { skipReassigning: true } + }) ) - const ids = [1, 2, 3] - - // Performing several create row requests should increment the autoID fields accordingly - const createRow = async (id: number) => { - const res = await config.api.row.save(newTable._id!, { - name: "row_" + id, - }) - expect(res.name).toEqual("row_" + id) - expect(res._rev).toBeDefined() - expect(res["Row ID"]).toEqual(id) + let previousId = 0 + for (let i = 0; i < 10; i++) { + const row = await config.api.row.save(newTable._id!, {}) + expect(row["Row ID"]).toBeGreaterThan(previousId) + previousId = row["Row ID"] } - - for (let i = 0; i < ids.length; i++) { - await createRow(ids[i]) - } - - await assertRowUsage(rowUsage + ids.length) + await assertRowUsage(rowUsage + 10) }) it("updates a row successfully", async () => { - const existing = await config.createRow() + const existing = await config.api.row.save(table._id!, {}) const rowUsage = await getRowUsage() - const res = await config.api.row.save(tableId, { + const res = await config.api.row.save(table._id!, { _id: existing._id, _rev: existing._rev, - tableId, name: "Updated Name", }) @@ -236,9 +237,9 @@ describe.each([ }) it("should load a row", async () => { - const existing = await config.createRow() + const existing = await config.api.row.save(table._id!, {}) - const res = await config.api.row.get(tableId, existing._id!) + const res = await config.api.row.get(table._id!, existing._id!) expect(res).toEqual({ ...existing, @@ -247,29 +248,22 @@ describe.each([ }) it("should list all rows for given tableId", async () => { - const table = await createTable(generateTableConfig(), { - skipReassigning: true, - }) - const tableId = table._id! - const newRow = { - tableId, - name: "Second Contact", - description: "new", - } - const firstRow = await config.createRow({ tableId }) - await config.createRow(newRow) + const table = await config.api.table.save(defaultTable()) + const rows = await Promise.all([ + config.api.row.save(table._id!, {}), + config.api.row.save(table._id!, {}), + ]) - const res = await config.api.row.fetch(tableId) - - expect(res.length).toBe(2) - expect(res.find((r: Row) => r.name === newRow.name)).toBeDefined() - expect(res.find((r: Row) => r.name === firstRow.name)).toBeDefined() + const res = await config.api.row.fetch(table._id!) + expect(res.map(r => r._id)).toEqual( + expect.arrayContaining(rows.map(r => r._id)) + ) }) it("load should return 404 when row does not exist", async () => { - await config.createRow() - - await config.api.row.get(tableId, "1234567", { + const table = await config.api.table.save(defaultTable()) + await config.api.row.save(table._id!, {}) + await config.api.row.get(table._id!, "1234567", { status: 404, }) }) @@ -459,7 +453,8 @@ describe.each([ }, }) - const createViewResponse = await config.createView({ + const createViewResponse = await config.api.viewV2.create({ + tableId: table._id!, name: uuid.v4(), schema: { Country: { @@ -496,25 +491,25 @@ describe.each([ let otherTable: Table beforeAll(async () => { - const tableConfig = generateTableConfig() - table = await createTable(tableConfig) - const otherTableConfig = generateTableConfig() - // need a short name of table here - for relationship tests - otherTableConfig.name = "a" - otherTableConfig.schema.relationship = { - name: "relationship", - relationshipType: RelationshipType.ONE_TO_MANY, - type: FieldType.LINK, - tableId: table._id!, - fieldName: "relationship", - } - otherTable = await createTable(otherTableConfig) - // need to set the config back to the original table - config.table = table + table = await config.api.table.save(defaultTable()) + otherTable = await config.api.table.save( + defaultTable({ + name: "a", + schema: { + relationship: { + name: "relationship", + relationshipType: RelationshipType.ONE_TO_MANY, + type: FieldType.LINK, + tableId: table._id!, + fieldName: "relationship", + }, + }, + }) + ) }) it("should update only the fields that are supplied", async () => { - const existing = await config.createRow() + const existing = await config.api.row.save(table._id!, {}) const rowUsage = await getRowUsage() @@ -536,7 +531,7 @@ describe.each([ }) it("should throw an error when given improper types", async () => { - const existing = await config.createRow() + const existing = await config.api.row.save(table._id!, {}) const rowUsage = await getRowUsage() await config.api.row.patch( @@ -628,12 +623,11 @@ describe.each([ describe("destroy", () => { beforeAll(async () => { - const tableConfig = generateTableConfig() - table = await createTable(tableConfig) + table = await config.api.table.save(defaultTable()) }) it("should be able to delete a row", async () => { - const createdRow = await config.createRow() + const createdRow = await config.api.row.save(table._id!, {}) const rowUsage = await getRowUsage() const res = await config.api.row.bulkDelete(table._id!, { @@ -644,7 +638,7 @@ describe.each([ }) it("should be able to bulk delete rows, including a row that doesn't exist", async () => { - const createdRow = await config.createRow() + const createdRow = await config.api.row.save(table._id!, {}) const res = await config.api.row.bulkDelete(table._id!, { rows: [createdRow, { _id: "9999999" }], @@ -657,8 +651,7 @@ describe.each([ describe("validate", () => { beforeAll(async () => { - const tableConfig = generateTableConfig() - table = await createTable(tableConfig) + table = await config.api.table.save(defaultTable()) }) it("should return no errors on valid row", async () => { @@ -690,13 +683,12 @@ describe.each([ describe("bulkDelete", () => { beforeAll(async () => { - const tableConfig = generateTableConfig() - table = await createTable(tableConfig) + table = await config.api.table.save(defaultTable()) }) it("should be able to delete a bulk set of rows", async () => { - const row1 = await config.createRow() - const row2 = await config.createRow() + const row1 = await config.api.row.save(table._id!, {}) + const row2 = await config.api.row.save(table._id!, {}) const rowUsage = await getRowUsage() const res = await config.api.row.bulkDelete(table._id!, { @@ -710,9 +702,9 @@ describe.each([ it("should be able to delete a variety of row set types", async () => { const [row1, row2, row3] = await Promise.all([ - config.createRow(), - config.createRow(), - config.createRow(), + config.api.row.save(table._id!, {}), + config.api.row.save(table._id!, {}), + config.api.row.save(table._id!, {}), ]) const rowUsage = await getRowUsage() @@ -726,7 +718,7 @@ describe.each([ }) it("should accept a valid row object and delete the row", async () => { - const row1 = await config.createRow() + const row1 = await config.api.row.save(table._id!, {}) const rowUsage = await getRowUsage() const res = await config.api.row.delete(table._id!, row1 as DeleteRow) @@ -768,12 +760,11 @@ describe.each([ isInternal && describe("fetchView", () => { beforeEach(async () => { - const tableConfig = generateTableConfig() - table = await createTable(tableConfig) + table = await config.api.table.save(defaultTable()) }) it("should be able to fetch tables contents via 'view'", async () => { - const row = await config.createRow() + const row = await config.api.row.save(table._id!, {}) const rowUsage = await getRowUsage() const rows = await config.api.legacyView.get(table._id!) @@ -797,7 +788,7 @@ describe.each([ filters: [], schema: {}, }) - const row = await config.createRow() + const row = await config.api.row.save(table._id!, {}) const rowUsage = await getRowUsage() const rows = await config.api.legacyView.get(view.name) @@ -810,45 +801,34 @@ describe.each([ describe("fetchEnrichedRows", () => { beforeAll(async () => { - const tableConfig = generateTableConfig() - table = await createTable(tableConfig) + table = await config.api.table.save(defaultTable()) }) it("should allow enriching some linked rows", async () => { const { linkedTable, firstRow, secondRow } = await tenancy.doInTenant( config.getTenantId(), async () => { - const linkedTable = await config.createLinkedTable( - RelationshipType.ONE_TO_MANY, - ["link"], - { - // Making sure that the combined table name + column name is within postgres limits - name: uuid.v4().replace(/-/g, "").substring(0, 16), - type: "table", - primary: ["id"], - primaryDisplay: "id", + const linkedTable = await config.api.table.save( + defaultTable({ schema: { - id: { - type: FieldType.AUTO, - name: "id", - autocolumn: true, - constraints: { - presence: true, - }, + link: { + name: "link", + fieldName: "link", + type: FieldType.LINK, + relationshipType: RelationshipType.ONE_TO_MANY, + tableId: table._id!, }, }, - } + }) ) - const firstRow = await config.createRow({ + const firstRow = await config.api.row.save(table._id!, { name: "Test Contact", description: "original description", - tableId: table._id, }) - const secondRow = await config.createRow({ + const secondRow = await config.api.row.save(linkedTable._id!, { name: "Test 2", description: "og desc", link: [{ _id: firstRow._id }], - tableId: linkedTable._id, }) return { linkedTable, firstRow, secondRow } } @@ -882,8 +862,7 @@ describe.each([ isInternal && describe("attachments", () => { beforeAll(async () => { - const tableConfig = generateTableConfig() - table = await createTable(tableConfig) + table = await config.api.table.save(defaultTable()) }) it("should allow enriching attachment rows", async () => { @@ -912,12 +891,11 @@ describe.each([ describe("exportData", () => { beforeAll(async () => { - const tableConfig = generateTableConfig() - table = await createTable(tableConfig) + table = await config.api.table.save(defaultTable()) }) it("should allow exporting all columns", async () => { - const existing = await config.createRow() + const existing = await config.api.row.save(table._id!, {}) const res = await config.api.row.exportRows(table._id!, { rows: [existing._id!], }) @@ -935,7 +913,7 @@ describe.each([ }) it("should allow exporting only certain columns", async () => { - const existing = await config.createRow() + const existing = await config.api.row.save(table._id!, {}) const res = await config.api.row.exportRows(table._id!, { rows: [existing._id!], columns: ["_id"], @@ -952,21 +930,10 @@ describe.each([ describe("view 2.0", () => { async function userTable(): Promise { - return { + return saveTableRequest({ name: `users_${uuid.v4()}`, - sourceId: INTERNAL_TABLE_SOURCE_ID, - sourceType: TableSourceType.INTERNAL, type: "table", - primary: ["id"], schema: { - id: { - type: FieldType.AUTO, - name: "id", - autocolumn: true, - constraints: { - presence: true, - }, - }, name: { type: FieldType.STRING, name: "name", @@ -988,7 +955,7 @@ describe.each([ name: "jobTitle", }, }, - } + }) } const randomRowData = () => ({ @@ -1001,8 +968,9 @@ describe.each([ describe("create", () => { it("should persist a new row with only the provided view fields", async () => { - const table = await createTable(await userTable()) - const view = await config.createView({ + const table = await config.api.table.save(await userTable()) + const view = await config.api.viewV2.create({ + tableId: table._id!, schema: { name: { visible: true }, surname: { visible: true }, @@ -1036,9 +1004,10 @@ describe.each([ describe("patch", () => { it("should update only the view fields for a row", async () => { - const table = await createTable(await userTable()) + const table = await config.api.table.save(await userTable()) const tableId = table._id! - const view = await config.createView({ + const view = await config.api.viewV2.create({ + tableId: tableId, schema: { name: { visible: true }, address: { visible: true }, @@ -1077,16 +1046,17 @@ describe.each([ describe("destroy", () => { it("should be able to delete a row", async () => { - const table = await createTable(await userTable()) + const table = await config.api.table.save(await userTable()) const tableId = table._id! - const view = await config.createView({ + const view = await config.api.viewV2.create({ + tableId: tableId, schema: { name: { visible: true }, address: { visible: true }, }, }) - const createdRow = await config.createRow() + const createdRow = await config.api.row.save(table._id!, {}) const rowUsage = await getRowUsage() await config.api.row.bulkDelete(view.id, { rows: [createdRow] }) @@ -1099,9 +1069,10 @@ describe.each([ }) it("should be able to delete multiple rows", async () => { - const table = await createTable(await userTable()) + const table = await config.api.table.save(await userTable()) const tableId = table._id! - const view = await config.createView({ + const view = await config.api.viewV2.create({ + tableId: tableId, schema: { name: { visible: true }, address: { visible: true }, @@ -1109,9 +1080,9 @@ describe.each([ }) const rows = await Promise.all([ - config.createRow(), - config.createRow(), - config.createRow(), + config.api.row.save(table._id!, {}), + config.api.row.save(table._id!, {}), + config.api.row.save(table._id!, {}), ]) const rowUsage = await getRowUsage() @@ -1130,46 +1101,39 @@ describe.each([ }) describe("view search", () => { + let table: Table const viewSchema = { age: { visible: true }, name: { visible: true } } - async function userTable(): Promise
{ - return { - name: `users_${uuid.v4()}`, - sourceId: INTERNAL_TABLE_SOURCE_ID, - sourceType: TableSourceType.INTERNAL, - type: "table", - primary: ["id"], - schema: { - id: { - type: FieldType.AUTO, - name: "id", - autocolumn: true, - constraints: { - presence: true, + + beforeAll(async () => { + table = await config.api.table.save( + saveTableRequest({ + name: `users_${uuid.v4()}`, + schema: { + name: { + type: FieldType.STRING, + name: "name", + constraints: { type: "string" }, + }, + age: { + type: FieldType.NUMBER, + name: "age", + constraints: {}, }, }, - name: { - type: FieldType.STRING, - name: "name", - constraints: { type: "string" }, - }, - age: { - type: FieldType.NUMBER, - name: "age", - constraints: {}, - }, - }, - } - } + }) + ) + }) it("returns empty rows from view when no schema is passed", async () => { - const table = await createTable(await userTable()) const rows = await Promise.all( Array.from({ length: 10 }, () => config.api.row.save(table._id!, { tableId: table._id }) ) ) - const createViewResponse = await config.createView() + const createViewResponse = await config.api.viewV2.create({ + tableId: table._id!, + }) const response = await config.api.viewV2.search(createViewResponse.id) expect(response.rows).toHaveLength(10) @@ -1193,8 +1157,6 @@ describe.each([ }) it("searching respects the view filters", async () => { - const table = await createTable(await userTable()) - await Promise.all( Array.from({ length: 10 }, () => config.api.row.save(table._id!, { @@ -1215,7 +1177,8 @@ describe.each([ ) ) - const createViewResponse = await config.createView({ + const createViewResponse = await config.api.viewV2.create({ + tableId: table._id!, query: [ { operator: SearchQueryOperators.EQUAL, field: "age", value: 40 }, ], @@ -1316,8 +1279,9 @@ describe.each([ ] describe("sorting", () => { + let table: Table beforeAll(async () => { - const table = await createTable(await userTable()) + table = await config.api.table.save(await userTable()) const users = [ { name: "Alice", age: 25 }, { name: "Bob", age: 30 }, @@ -1337,7 +1301,8 @@ describe.each([ it.each(sortTestOptions)( "allow sorting (%s)", async (sortParams, expected) => { - const createViewResponse = await config.createView({ + const createViewResponse = await config.api.viewV2.create({ + tableId: table._id!, sort: sortParams, schema: viewSchema, }) @@ -1356,7 +1321,8 @@ describe.each([ it.each(sortTestOptions)( "allow override the default view sorting (%s)", async (sortParams, expected) => { - const createViewResponse = await config.createView({ + const createViewResponse = await config.api.viewV2.create({ + tableId: table._id!, sort: { field: "name", order: SortOrder.ASCENDING, @@ -1384,7 +1350,7 @@ describe.each([ }) it("when schema is defined, defined columns and row attributes are returned", async () => { - const table = await createTable(await userTable()) + const table = await config.api.table.save(await userTable()) const rows = await Promise.all( Array.from({ length: 10 }, () => config.api.row.save(table._id!, { @@ -1395,7 +1361,8 @@ describe.each([ ) ) - const view = await config.createView({ + const view = await config.api.viewV2.create({ + tableId: table._id!, schema: { name: { visible: true } }, }) const response = await config.api.viewV2.search(view.id) @@ -1415,21 +1382,25 @@ describe.each([ }) it("views without data can be returned", async () => { - const table = await createTable(await userTable()) - - const createViewResponse = await config.createView() + const table = await config.api.table.save(await userTable()) + const createViewResponse = await config.api.viewV2.create({ + tableId: table._id!, + }) const response = await config.api.viewV2.search(createViewResponse.id) - expect(response.rows).toHaveLength(0) }) it("respects the limit parameter", async () => { - await createTable(await userTable()) - await Promise.all(Array.from({ length: 10 }, () => config.createRow())) + const table = await config.api.table.save(await userTable()) + await Promise.all( + Array.from({ length: 10 }, () => config.api.row.save(table._id!, {})) + ) const limit = generator.integer({ min: 1, max: 8 }) - const createViewResponse = await config.createView() + const createViewResponse = await config.api.viewV2.create({ + tableId: table._id!, + }) const response = await config.api.viewV2.search(createViewResponse.id, { limit, query: {}, @@ -1439,56 +1410,49 @@ describe.each([ }) it("can handle pagination", async () => { - await createTable(await userTable()) - await Promise.all(Array.from({ length: 10 }, () => config.createRow())) - - const createViewResponse = await config.createView() - const allRows = (await config.api.viewV2.search(createViewResponse.id)) - .rows - - const firstPageResponse = await config.api.viewV2.search( - createViewResponse.id, - { - paginate: true, - limit: 4, - query: {}, - } + const table = await config.api.table.save(await userTable()) + await Promise.all( + Array.from({ length: 10 }, () => config.api.row.save(table._id!, {})) ) - expect(firstPageResponse).toEqual({ - rows: expect.arrayContaining(allRows.slice(0, 4)), + const view = await config.api.viewV2.create({ + tableId: table._id!, + }) + const rows = (await config.api.viewV2.search(view.id)).rows + + const page1 = await config.api.viewV2.search(view.id, { + paginate: true, + limit: 4, + query: {}, + }) + expect(page1).toEqual({ + rows: expect.arrayContaining(rows.slice(0, 4)), totalRows: isInternal ? 10 : undefined, hasNextPage: true, bookmark: expect.anything(), }) - const secondPageResponse = await config.api.viewV2.search( - createViewResponse.id, - { - paginate: true, - limit: 4, - bookmark: firstPageResponse.bookmark, + const page2 = await config.api.viewV2.search(view.id, { + paginate: true, + limit: 4, + bookmark: page1.bookmark, - query: {}, - } - ) - expect(secondPageResponse).toEqual({ - rows: expect.arrayContaining(allRows.slice(4, 8)), + query: {}, + }) + expect(page2).toEqual({ + rows: expect.arrayContaining(rows.slice(4, 8)), totalRows: isInternal ? 10 : undefined, hasNextPage: true, bookmark: expect.anything(), }) - const lastPageResponse = await config.api.viewV2.search( - createViewResponse.id, - { - paginate: true, - limit: 4, - bookmark: secondPageResponse.bookmark, - query: {}, - } - ) - expect(lastPageResponse).toEqual({ - rows: expect.arrayContaining(allRows.slice(8)), + const page3 = await config.api.viewV2.search(view.id, { + paginate: true, + limit: 4, + bookmark: page2.bookmark, + query: {}, + }) + expect(page3).toEqual({ + rows: expect.arrayContaining(rows.slice(8)), totalRows: isInternal ? 10 : undefined, hasNextPage: false, bookmark: expect.anything(), @@ -1513,19 +1477,20 @@ describe.each([ }) describe("permissions", () => { - let viewId: string - let tableId: string + let table: Table + let view: ViewV2 beforeAll(async () => { - await createTable(await userTable()) + table = await config.api.table.save(await userTable()) await Promise.all( - Array.from({ length: 10 }, () => config.createRow()) + Array.from({ length: 10 }, () => + config.api.row.save(table._id!, {}) + ) ) - const createViewResponse = await config.createView() - - tableId = table._id! - viewId = createViewResponse.id + view = await config.api.viewV2.create({ + tableId: table._id!, + }) }) beforeEach(() => { @@ -1534,7 +1499,7 @@ describe.each([ it("does not allow public users to fetch by default", async () => { await config.publish() - await config.api.viewV2.publicSearch(viewId, undefined, { + await config.api.viewV2.publicSearch(view.id, undefined, { status: 403, }) }) @@ -1543,11 +1508,11 @@ describe.each([ await config.api.permission.add({ roleId: roles.BUILTIN_ROLE_IDS.PUBLIC, level: PermissionLevel.READ, - resourceId: viewId, + resourceId: view.id, }) await config.publish() - const response = await config.api.viewV2.publicSearch(viewId) + const response = await config.api.viewV2.publicSearch(view.id) expect(response.rows).toHaveLength(10) }) @@ -1556,11 +1521,11 @@ describe.each([ await config.api.permission.add({ roleId: roles.BUILTIN_ROLE_IDS.PUBLIC, level: PermissionLevel.READ, - resourceId: tableId, + resourceId: table._id!, }) await config.publish() - const response = await config.api.viewV2.publicSearch(viewId) + const response = await config.api.viewV2.publicSearch(view.id) expect(response.rows).toHaveLength(10) }) @@ -1569,16 +1534,16 @@ describe.each([ await config.api.permission.add({ roleId: roles.BUILTIN_ROLE_IDS.PUBLIC, level: PermissionLevel.READ, - resourceId: tableId, + resourceId: table._id!, }) await config.api.permission.add({ roleId: roles.BUILTIN_ROLE_IDS.POWER, level: PermissionLevel.READ, - resourceId: viewId, + resourceId: view.id, }) await config.publish() - await config.api.viewV2.publicSearch(viewId, undefined, { + await config.api.viewV2.publicSearch(view.id, undefined, { status: 403, }) }) @@ -1589,18 +1554,8 @@ describe.each([ let o2mTable: Table let m2mTable: Table beforeAll(async () => { - o2mTable = await createTable( - { ...generateTableConfig(), name: "o2m" }, - { - skipReassigning: true, - } - ) - m2mTable = await createTable( - { ...generateTableConfig(), name: "m2m" }, - { - skipReassigning: true, - } - ) + o2mTable = await config.api.table.save(defaultTable({ name: "o2m" })) + m2mTable = await config.api.table.save(defaultTable({ name: "m2m" })) }) describe.each([ @@ -1662,21 +1617,9 @@ describe.each([ let m2mData: Row[] beforeAll(async () => { - const tableConfig = generateTableConfig() - - if (config.datasource) { - tableConfig.sourceId = config.datasource._id! - if (config.datasource.plus) { - tableConfig.sourceType = TableSourceType.EXTERNAL - } - } - const table = await config.api.table.save({ - ...tableConfig, - schema: { - ...tableConfig.schema, - ...relSchema(), - }, - }) + const table = await config.api.table.save( + defaultTable({ schema: relSchema() }) + ) tableId = table._id! o2mData = [ @@ -1994,20 +1937,23 @@ describe.each([ }) describe("Formula fields", () => { - let relationshipTable: Table, tableId: string, relatedRow: Row + let table: Table + let otherTable: Table + let relatedRow: Row beforeAll(async () => { - const otherTableId = config.table!._id! - const cfg = generateTableConfig() - relationshipTable = await config.createLinkedTable( - RelationshipType.ONE_TO_MANY, - ["links"], - { - ...cfg, - // needs to be a short name + otherTable = await config.api.table.save(defaultTable()) + table = await config.api.table.save( + saveTableRequest({ name: "b", schema: { - ...cfg.schema, + links: { + name: "links", + fieldName: "links", + type: FieldType.LINK, + tableId: otherTable._id!, + relationshipType: RelationshipType.ONE_TO_MANY, + }, formula: { name: "formula", type: FieldType.FORMULA, @@ -2015,25 +1961,23 @@ describe.each([ formulaType: FormulaType.DYNAMIC, }, }, - } + }) ) - tableId = relationshipTable._id! - - relatedRow = await config.api.row.save(otherTableId, { + relatedRow = await config.api.row.save(otherTable._id!, { name: generator.word(), description: generator.paragraph(), }) - await config.api.row.save(tableId, { + await config.api.row.save(table._id!, { name: generator.word(), description: generator.paragraph(), - tableId, + tableId: table._id!, links: [relatedRow._id], }) }) it("should be able to search for rows containing formulas", async () => { - const { rows } = await config.api.row.search(tableId) + const { rows } = await config.api.row.search(table._id!) expect(rows.length).toBe(1) expect(rows[0].links.length).toBe(1) const row = rows[0] @@ -2054,22 +1998,22 @@ describe.each([ ` ).toString("base64") - const table = await config.createTable({ - name: "table", - type: "table", - schema: { - text: { - name: "text", - type: FieldType.STRING, + const table = await config.api.table.save( + saveTableRequest({ + schema: { + text: { + name: "text", + type: FieldType.STRING, + }, + formula: { + name: "formula", + type: FieldType.FORMULA, + formula: `{{ js "${js}"}}`, + formulaType: FormulaType.DYNAMIC, + }, }, - formula: { - name: "formula", - type: FieldType.FORMULA, - formula: `{{ js "${js}"}}`, - formulaType: FormulaType.DYNAMIC, - }, - }, - }) + }) + ) await config.api.row.save(table._id!, { text: "foo" }) const { rows } = await config.api.row.search(table._id!) From d3c8a28ab6da370d2f1c6b790b2e9d29c8043056 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 13 Mar 2024 16:20:45 +0000 Subject: [PATCH 20/29] Remove more old testing practices. --- .../server/src/api/routes/tests/row.spec.ts | 143 ++++++------------ 1 file changed, 50 insertions(+), 93 deletions(-) diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index cf4e7c26d0..09866a7917 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -44,8 +44,6 @@ jest.unmock("mysql2") jest.unmock("mysql2/promise") jest.unmock("mssql") -const { basicRow } = setup.structures - describe.each([ ["internal", undefined], ["postgres", databaseTestProviders.postgres], @@ -54,7 +52,6 @@ describe.each([ ["mariadb", databaseTestProviders.mariadb], ])("/rows (%s)", (__, dsProvider) => { const isInternal = dsProvider === undefined - const request = setup.getRequest() const config = setup.getConfig() let table: Table @@ -172,18 +169,11 @@ describe.each([ describe("save, load, update", () => { it("returns a success message when the row is created", async () => { const rowUsage = await getRowUsage() - - const res = await request - .post(`/api/${table._id!}/rows`) - .send(basicRow(table._id!)) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) - expect((res as any).res.statusMessage).toEqual( - `${table.name} saved successfully` - ) - expect(res.body.name).toEqual("Test Contact") - expect(res.body._rev).toBeDefined() + const row = await config.api.row.save(table._id!, { + name: "Test Contact", + }) + expect(row.name).toEqual("Test Contact") + expect(row._rev).toBeDefined() await assertRowUsage(rowUsage + 1) }) @@ -1638,37 +1628,33 @@ describe.each([ }) it("can save a row when relationship fields are empty", async () => { - const rowData = { - ...basicRow(tableId), - name: generator.name(), - description: generator.name(), - } - const row = await config.api.row.save(tableId, rowData) + const row = await config.api.row.save(tableId, { + name: "foo", + description: "bar", + }) expect(row).toEqual({ - name: rowData.name, - description: rowData.description, - tableId, _id: expect.any(String), _rev: expect.any(String), id: isInternal ? undefined : expect.any(Number), type: isInternal ? "row" : undefined, + name: "foo", + description: "bar", + tableId, }) }) it("can save a row with a single relationship field", async () => { const user = _.sample(o2mData)! - const rowData = { - ...basicRow(tableId), - name: generator.name(), - description: generator.name(), + const row = await config.api.row.save(tableId, { + name: "foo", + description: "bar", user: [user], - } - const row = await config.api.row.save(tableId, rowData) + }) expect(row).toEqual({ - name: rowData.name, - description: rowData.description, + name: "foo", + description: "bar", tableId, user: [user].map(u => resultMapper(u)), _id: expect.any(String), @@ -1681,17 +1667,15 @@ describe.each([ it("can save a row with a multiple relationship field", async () => { const selectedUsers = _.sampleSize(m2mData, 2) - const rowData = { - ...basicRow(tableId), - name: generator.name(), - description: generator.name(), + const row = await config.api.row.save(tableId, { + name: "foo", + description: "bar", users: selectedUsers, - } - const row = await config.api.row.save(tableId, rowData) + }) expect(row).toEqual({ - name: rowData.name, - description: rowData.description, + name: "foo", + description: "bar", tableId, users: expect.arrayContaining(selectedUsers.map(u => resultMapper(u))), _id: expect.any(String), @@ -1702,17 +1686,15 @@ describe.each([ }) it("can retrieve rows with no populated relationships", async () => { - const rowData = { - ...basicRow(tableId), - name: generator.name(), - description: generator.name(), - } - const row = await config.api.row.save(tableId, rowData) + const row = await config.api.row.save(tableId, { + name: "foo", + description: "bar", + }) const retrieved = await config.api.row.get(tableId, row._id!) expect(retrieved).toEqual({ - name: rowData.name, - description: rowData.description, + name: "foo", + description: "bar", tableId, user: undefined, users: undefined, @@ -1727,19 +1709,17 @@ describe.each([ const user1 = _.sample(o2mData)! const [user2, user3] = _.sampleSize(m2mData, 2) - const rowData = { - ...basicRow(tableId), - name: generator.name(), - description: generator.name(), + const row = await config.api.row.save(tableId, { + name: "foo", + description: "bar", users: [user2, user3], user: [user1], - } - const row = await config.api.row.save(tableId, rowData) + }) const retrieved = await config.api.row.get(tableId, row._id!) expect(retrieved).toEqual({ - name: rowData.name, - description: rowData.description, + name: "foo", + description: "bar", tableId, user: expect.arrayContaining([user1].map(u => resultMapper(u))), users: expect.arrayContaining([user2, user3].map(u => resultMapper(u))), @@ -1755,13 +1735,11 @@ describe.each([ const user = _.sample(o2mData)! const [users1, users2, users3] = _.sampleSize(m2mData, 3) - const rowData = { - ...basicRow(tableId), - name: generator.name(), - description: generator.name(), + const row = await config.api.row.save(tableId, { + name: "foo", + description: "bar", users: [users1, users2], - } - const row = await config.api.row.save(tableId, rowData) + }) const updatedRow = await config.api.row.save(tableId, { ...row, @@ -1769,8 +1747,8 @@ describe.each([ users: [users3, users1], }) expect(updatedRow).toEqual({ - name: rowData.name, - description: rowData.description, + name: "foo", + description: "bar", tableId, user: expect.arrayContaining([user].map(u => resultMapper(u))), users: expect.arrayContaining( @@ -1786,14 +1764,11 @@ describe.each([ it("can wipe an existing populated relationships in row", async () => { const [user1, user2] = _.sampleSize(m2mData, 2) - - const rowData = { - ...basicRow(tableId), - name: generator.name(), - description: generator.name(), + const row = await config.api.row.save(tableId, { + name: "foo", + description: "bar", users: [user1, user2], - } - const row = await config.api.row.save(tableId, rowData) + }) const updatedRow = await config.api.row.save(tableId, { ...row, @@ -1801,8 +1776,8 @@ describe.each([ users: null, }) expect(updatedRow).toEqual({ - name: rowData.name, - description: rowData.description, + name: "foo", + description: "bar", tableId, _id: row._id, _rev: expect.any(String), @@ -1815,28 +1790,19 @@ describe.each([ const [user1] = _.sampleSize(o2mData, 1) const [users1, users2, users3] = _.sampleSize(m2mData, 3) - const rows: { - name: string - description: string - user?: Row[] - users?: Row[] - tableId: string - }[] = [ + const rows = [ { - ...basicRow(tableId), name: generator.name(), description: generator.name(), users: [users1, users2], }, { - ...basicRow(tableId), name: generator.name(), description: generator.name(), user: [user1], users: [users1, users3], }, { - ...basicRow(tableId), name: generator.name(), description: generator.name(), users: [users3], @@ -1874,28 +1840,19 @@ describe.each([ const [user1] = _.sampleSize(o2mData, 1) const [users1, users2, users3] = _.sampleSize(m2mData, 3) - const rows: { - name: string - description: string - user?: Row[] - users?: Row[] - tableId: string - }[] = [ + const rows = [ { - ...basicRow(tableId), name: generator.name(), description: generator.name(), users: [users1, users2], }, { - ...basicRow(tableId), name: generator.name(), description: generator.name(), user: [user1], users: [users1, users3], }, { - ...basicRow(tableId), name: generator.name(), description: generator.name(), users: [users3], From 68777b08439223ac43f7d1671043f153f3846589 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 13 Mar 2024 16:32:33 +0000 Subject: [PATCH 21/29] Remove all uses of config.createTable --- .../server/src/api/routes/tests/row.spec.ts | 267 ++++++++---------- 1 file changed, 122 insertions(+), 145 deletions(-) diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index 09866a7917..613928712c 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -32,7 +32,6 @@ import { expectAnyInternalColsAttributes, generator, mocks, - structures, } from "@budibase/backend-core/tests" import _, { merge } from "lodash" import * as uuid from "uuid" @@ -149,19 +148,6 @@ describe.each([ } : undefined - async function createTable( - cfg: Omit, - opts?: { skipReassigning: boolean } - ) { - let table - if (dsProvider) { - table = await config.createExternalTable(cfg, opts) - } else { - table = await config.createTable(cfg, opts) - } - return table - } - beforeAll(async () => { table = await config.api.table.save(defaultTable()) }) @@ -308,45 +294,49 @@ describe.each([ inclusion: ["Alpha", "Beta", "Gamma"], }, } - const table = await createTable({ - name: "TestTable2", - type: "table", - schema: { - name: str, - stringUndefined: str, - stringNull: str, - stringString: str, - numberEmptyString: number, - numberNull: number, - numberUndefined: number, - numberString: number, - numberNumber: number, - datetimeEmptyString: datetime, - datetimeNull: datetime, - datetimeUndefined: datetime, - datetimeString: datetime, - datetimeDate: datetime, - boolNull: bool, - boolEmpty: bool, - boolUndefined: bool, - boolString: bool, - boolBool: bool, - attachmentNull: attachment, - attachmentUndefined: attachment, - attachmentEmpty: attachment, - attachmentEmptyArrayStr: attachment, - arrayFieldEmptyArrayStr: arrayField, - arrayFieldArrayStrKnown: arrayField, - arrayFieldNull: arrayField, - arrayFieldUndefined: arrayField, - optsFieldEmptyStr: optsField, - optsFieldUndefined: optsField, - optsFieldNull: optsField, - optsFieldStrKnown: optsField, - }, - }) + const table = await config.api.table.save( + saveTableRequest({ + name: "TestTable2", + type: "table", + schema: { + name: str, + stringUndefined: str, + stringNull: str, + stringString: str, + numberEmptyString: number, + numberNull: number, + numberUndefined: number, + numberString: number, + numberNumber: number, + datetimeEmptyString: datetime, + datetimeNull: datetime, + datetimeUndefined: datetime, + datetimeString: datetime, + datetimeDate: datetime, + boolNull: bool, + boolEmpty: bool, + boolUndefined: bool, + boolString: bool, + boolBool: bool, + attachmentNull: attachment, + attachmentUndefined: attachment, + attachmentEmpty: attachment, + attachmentEmptyArrayStr: attachment, + arrayFieldEmptyArrayStr: arrayField, + arrayFieldArrayStrKnown: arrayField, + arrayFieldNull: arrayField, + arrayFieldUndefined: arrayField, + optsFieldEmptyStr: optsField, + optsFieldUndefined: optsField, + optsFieldNull: optsField, + optsFieldStrKnown: optsField, + }, + }) + ) - const row = { + const datetimeStr = "1984-04-20T00:00:00.000Z" + + const row = await config.api.row.save(table._id!, { name: "Test Row", stringUndefined: undefined, stringNull: null, @@ -359,8 +349,8 @@ describe.each([ datetimeEmptyString: "", datetimeNull: null, datetimeUndefined: undefined, - datetimeString: "1984-04-20T00:00:00.000Z", - datetimeDate: new Date("1984-04-20"), + datetimeString: datetimeStr, + datetimeDate: new Date(datetimeStr), boolNull: null, boolEmpty: "", boolUndefined: undefined, @@ -379,69 +369,58 @@ describe.each([ optsFieldUndefined: undefined, optsFieldNull: null, optsFieldStrKnown: "Alpha", - } + }) - const createdRow = await config.createRow(row) - const id = createdRow._id! - - const saved = await config.api.row.get(table._id!, id) - - expect(saved.stringUndefined).toBe(undefined) - expect(saved.stringNull).toBe(null) - expect(saved.stringString).toBe("i am a string") - expect(saved.numberEmptyString).toBe(null) - expect(saved.numberNull).toBe(null) - expect(saved.numberUndefined).toBe(undefined) - expect(saved.numberString).toBe(123) - expect(saved.numberNumber).toBe(123) - expect(saved.datetimeEmptyString).toBe(null) - expect(saved.datetimeNull).toBe(null) - expect(saved.datetimeUndefined).toBe(undefined) - expect(saved.datetimeString).toBe( - new Date(row.datetimeString).toISOString() - ) - expect(saved.datetimeDate).toBe(row.datetimeDate.toISOString()) - expect(saved.boolNull).toBe(null) - expect(saved.boolEmpty).toBe(null) - expect(saved.boolUndefined).toBe(undefined) - expect(saved.boolString).toBe(true) - expect(saved.boolBool).toBe(true) - expect(saved.attachmentNull).toEqual([]) - expect(saved.attachmentUndefined).toBe(undefined) - expect(saved.attachmentEmpty).toEqual([]) - expect(saved.attachmentEmptyArrayStr).toEqual([]) - expect(saved.arrayFieldEmptyArrayStr).toEqual([]) - expect(saved.arrayFieldNull).toEqual([]) - expect(saved.arrayFieldUndefined).toEqual(undefined) - expect(saved.optsFieldEmptyStr).toEqual(null) - expect(saved.optsFieldUndefined).toEqual(undefined) - expect(saved.optsFieldNull).toEqual(null) - expect(saved.arrayFieldArrayStrKnown).toEqual(["One"]) - expect(saved.optsFieldStrKnown).toEqual("Alpha") + expect(row.stringUndefined).toBe(undefined) + expect(row.stringNull).toBe(null) + expect(row.stringString).toBe("i am a string") + expect(row.numberEmptyString).toBe(null) + expect(row.numberNull).toBe(null) + expect(row.numberUndefined).toBe(undefined) + expect(row.numberString).toBe(123) + expect(row.numberNumber).toBe(123) + expect(row.datetimeEmptyString).toBe(null) + expect(row.datetimeNull).toBe(null) + expect(row.datetimeUndefined).toBe(undefined) + expect(row.datetimeString).toBe(new Date(datetimeStr).toISOString()) + expect(row.datetimeDate).toBe(new Date(datetimeStr).toISOString()) + expect(row.boolNull).toBe(null) + expect(row.boolEmpty).toBe(null) + expect(row.boolUndefined).toBe(undefined) + expect(row.boolString).toBe(true) + expect(row.boolBool).toBe(true) + expect(row.attachmentNull).toEqual([]) + expect(row.attachmentUndefined).toBe(undefined) + expect(row.attachmentEmpty).toEqual([]) + expect(row.attachmentEmptyArrayStr).toEqual([]) + expect(row.arrayFieldEmptyArrayStr).toEqual([]) + expect(row.arrayFieldNull).toEqual([]) + expect(row.arrayFieldUndefined).toEqual(undefined) + expect(row.optsFieldEmptyStr).toEqual(null) + expect(row.optsFieldUndefined).toEqual(undefined) + expect(row.optsFieldNull).toEqual(null) + expect(row.arrayFieldArrayStrKnown).toEqual(["One"]) + expect(row.optsFieldStrKnown).toEqual("Alpha") }) }) describe("view save", () => { it("views have extra data trimmed", async () => { - const table = await createTable({ - type: "table", - name: "orders", - primary: ["OrderID"], - schema: { - Country: { - type: FieldType.STRING, - name: "Country", + const table = await config.api.table.save( + saveTableRequest({ + name: "orders", + schema: { + Country: { + type: FieldType.STRING, + name: "Country", + }, + Story: { + type: FieldType.STRING, + name: "Story", + }, }, - OrderID: { - type: FieldType.NUMBER, - name: "OrderID", - }, - Story: { - type: FieldType.STRING, - name: "Story", - }, - }, - }) + }) + ) const createViewResponse = await config.api.viewV2.create({ tableId: table._id!, @@ -450,9 +429,6 @@ describe.each([ Country: { visible: true, }, - OrderID: { - visible: true, - }, }, }) @@ -468,8 +444,8 @@ describe.each([ expect(row.Story).toBeUndefined() expect(row).toEqual({ ...defaultRowFields, - OrderID: createRowResponse.OrderID, Country: "Aussy", + id: createRowResponse.id, _id: createRowResponse._id, _rev: createRowResponse._rev, tableId: table._id, @@ -857,7 +833,7 @@ describe.each([ it("should allow enriching attachment rows", async () => { const table = await config.createAttachmentTable() - const attachmentId = `${structures.uuid()}.csv` + const attachmentId = `${uuid.v4()}.csv` const row = await config.createRow({ name: "test", description: "test", @@ -1998,22 +1974,23 @@ describe.each([ ` ).toString("base64") - const table = await config.createTable({ - name: "table", - type: "table", - schema: { - text: { - name: "text", - type: FieldType.STRING, + const table = await config.api.table.save( + saveTableRequest({ + name: "table", + schema: { + text: { + name: "text", + type: FieldType.STRING, + }, + formula: { + name: "formula", + type: FieldType.FORMULA, + formula: `{{ js "${js}"}}`, + formulaType: FormulaType.DYNAMIC, + }, }, - formula: { - name: "formula", - type: FieldType.FORMULA, - formula: `{{ js "${js}"}}`, - formulaType: FormulaType.DYNAMIC, - }, - }, - }) + }) + ) for (let i = 0; i < 10; i++) { await config.api.row.save(table._id!, { text: "foo" }) @@ -2051,22 +2028,22 @@ describe.each([ it("should not carry over context between formulas", async () => { const js = Buffer.from(`return $("[text]");`).toString("base64") - const table = await config.createTable({ - name: "table", - type: "table", - schema: { - text: { - name: "text", - type: FieldType.STRING, + const table = await config.api.table.save( + saveTableRequest({ + schema: { + text: { + name: "text", + type: FieldType.STRING, + }, + formula: { + name: "formula", + type: FieldType.FORMULA, + formula: `{{ js "${js}"}}`, + formulaType: FormulaType.DYNAMIC, + }, }, - formula: { - name: "formula", - type: FieldType.FORMULA, - formula: `{{ js "${js}"}}`, - formulaType: FormulaType.DYNAMIC, - }, - }, - }) + }) + ) for (let i = 0; i < 10; i++) { await config.api.row.save(table._id!, { text: `foo${i}` }) From 76ecfedaedd43f29b9a4ece1afaa8862b1177688 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 13 Mar 2024 16:44:05 +0000 Subject: [PATCH 22/29] Reduce reliance on config a bit more. --- packages/server/src/api/routes/tests/row.spec.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index 613928712c..22dca18023 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -832,9 +832,19 @@ describe.each([ }) it("should allow enriching attachment rows", async () => { - const table = await config.createAttachmentTable() + const table = await config.api.table.save( + defaultTable({ + schema: { + attachment: { + type: FieldType.ATTACHMENT, + name: "attachment", + constraints: { type: "array", presence: false }, + }, + }, + }) + ) const attachmentId = `${uuid.v4()}.csv` - const row = await config.createRow({ + const row = await config.api.row.save(table._id!, { name: "test", description: "test", attachment: [ From 3da22617350d1a23c5dca0cfe7739dab4f84fb1a Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 13 Mar 2024 17:42:24 +0000 Subject: [PATCH 23/29] Convert view.spec.js to TypeScript, remove reliace on TestConfiguration. --- .../tests/__snapshots__/view.spec.js.snap | 38 -- .../tests/{view.spec.js => view.spec.ts} | 326 +++++++++--------- .../src/tests/utilities/api/legacyView.ts | 34 +- packages/types/src/documents/app/view.ts | 1 + 4 files changed, 201 insertions(+), 198 deletions(-) delete mode 100644 packages/server/src/api/routes/tests/__snapshots__/view.spec.js.snap rename packages/server/src/api/routes/tests/{view.spec.js => view.spec.ts} (59%) diff --git a/packages/server/src/api/routes/tests/__snapshots__/view.spec.js.snap b/packages/server/src/api/routes/tests/__snapshots__/view.spec.js.snap deleted file mode 100644 index 357285a69a..0000000000 --- a/packages/server/src/api/routes/tests/__snapshots__/view.spec.js.snap +++ /dev/null @@ -1,38 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`/views query returns data for the created view 1`] = ` -[ - { - "avg": 2333.3333333333335, - "count": 3, - "group": null, - "max": 4000, - "min": 1000, - "sum": 7000, - "sumsqr": 21000000, - }, -] -`; - -exports[`/views query returns data for the created view using a group by 1`] = ` -[ - { - "avg": 1500, - "count": 2, - "group": "One", - "max": 2000, - "min": 1000, - "sum": 3000, - "sumsqr": 5000000, - }, - { - "avg": 4000, - "count": 1, - "group": "Two", - "max": 4000, - "min": 4000, - "sum": 4000, - "sumsqr": 16000000, - }, -] -`; diff --git a/packages/server/src/api/routes/tests/view.spec.js b/packages/server/src/api/routes/tests/view.spec.ts similarity index 59% rename from packages/server/src/api/routes/tests/view.spec.js rename to packages/server/src/api/routes/tests/view.spec.ts index 92ff097899..2e8c71b812 100644 --- a/packages/server/src/api/routes/tests/view.spec.js +++ b/packages/server/src/api/routes/tests/view.spec.ts @@ -1,30 +1,38 @@ -const setup = require("./utilities") -const { events } = require("@budibase/backend-core") +import { events } from "@budibase/backend-core" +import * as setup from "./utilities" +import { + FieldType, + INTERNAL_TABLE_SOURCE_ID, + SaveTableRequest, + Table, + TableSourceType, + View, + ViewCalculation, +} from "@budibase/types" -function priceTable() { - return { - name: "table", - type: "table", - key: "name", - schema: { - Price: { - type: "number", - constraints: {}, - }, - Category: { +const priceTable: SaveTableRequest = { + name: "table", + type: "table", + sourceId: INTERNAL_TABLE_SOURCE_ID, + sourceType: TableSourceType.INTERNAL, + schema: { + Price: { + name: "Price", + type: FieldType.NUMBER, + }, + Category: { + name: "Category", + type: FieldType.STRING, + constraints: { type: "string", - constraints: { - type: "string", - }, }, }, - } + }, } describe("/views", () => { - let request = setup.getRequest() let config = setup.getConfig() - let table + let table: Table afterAll(setup.afterAll) @@ -33,38 +41,34 @@ describe("/views", () => { }) beforeEach(async () => { - table = await config.createTable(priceTable()) + table = await config.api.table.save(priceTable) }) - const saveView = async view => { - const viewToSave = { + const saveView = async (view?: Partial) => { + const viewToSave: View = { name: "TestView", field: "Price", - calculation: "stats", - tableId: table._id, + calculation: ViewCalculation.STATISTICS, + tableId: table._id!, + filters: [], + schema: {}, ...view, } - return request - .post(`/api/views`) - .send(viewToSave) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) + return config.api.legacyView.save(viewToSave) } describe("create", () => { it("returns a success message when the view is successfully created", async () => { const res = await saveView() - expect(res.body.tableId).toBe(table._id) expect(events.view.created).toBeCalledTimes(1) }) it("creates a view with a calculation", async () => { jest.clearAllMocks() - const res = await saveView({ calculation: "count" }) + const view = await saveView({ calculation: ViewCalculation.COUNT }) - expect(res.body.tableId).toBe(table._id) + expect(view.tableId).toBe(table._id) expect(events.view.created).toBeCalledTimes(1) expect(events.view.updated).not.toBeCalled() expect(events.view.calculationCreated).toBeCalledTimes(1) @@ -78,8 +82,8 @@ describe("/views", () => { it("creates a view with a filter", async () => { jest.clearAllMocks() - const res = await saveView({ - calculation: null, + const view = await saveView({ + calculation: undefined, filters: [ { value: "1", @@ -89,7 +93,7 @@ describe("/views", () => { ], }) - expect(res.body.tableId).toBe(table._id) + expect(view.tableId).toBe(table._id) expect(events.view.created).toBeCalledTimes(1) expect(events.view.updated).not.toBeCalled() expect(events.view.calculationCreated).not.toBeCalled() @@ -101,52 +105,41 @@ describe("/views", () => { }) it("updates the table row with the new view metadata", async () => { - const res = await request - .post(`/api/views`) - .send({ - name: "TestView", - field: "Price", - calculation: "stats", - tableId: table._id, + await saveView() + const updatedTable = await config.api.table.get(table._id!) + expect(updatedTable.views).toEqual( + expect.objectContaining({ + TestView: expect.objectContaining({ + field: "Price", + calculation: "stats", + tableId: table._id, + filters: [], + schema: { + sum: { + type: "number", + }, + min: { + type: "number", + }, + max: { + type: "number", + }, + count: { + type: "number", + }, + sumsqr: { + type: "number", + }, + avg: { + type: "number", + }, + field: { + type: "string", + }, + }, + }), }) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) - expect(res.body.tableId).toBe(table._id) - - const updatedTable = await config.getTable(table._id) - const expectedObj = expect.objectContaining({ - TestView: expect.objectContaining({ - field: "Price", - calculation: "stats", - tableId: table._id, - filters: [], - schema: { - sum: { - type: "number", - }, - min: { - type: "number", - }, - max: { - type: "number", - }, - count: { - type: "number", - }, - sumsqr: { - type: "number", - }, - avg: { - type: "number", - }, - field: { - type: "string", - }, - }, - }), - }) - expect(updatedTable.views).toEqual(expectedObj) + ) }) }) @@ -168,10 +161,10 @@ describe("/views", () => { }) it("updates a view calculation", async () => { - await saveView({ calculation: "sum" }) + await saveView({ calculation: ViewCalculation.SUM }) jest.clearAllMocks() - await saveView({ calculation: "count" }) + await saveView({ calculation: ViewCalculation.COUNT }) expect(events.view.created).not.toBeCalled() expect(events.view.updated).toBeCalledTimes(1) @@ -184,10 +177,10 @@ describe("/views", () => { }) it("deletes a view calculation", async () => { - await saveView({ calculation: "sum" }) + await saveView({ calculation: ViewCalculation.SUM }) jest.clearAllMocks() - await saveView({ calculation: null }) + await saveView({ calculation: undefined }) expect(events.view.created).not.toBeCalled() expect(events.view.updated).toBeCalledTimes(1) @@ -258,100 +251,98 @@ describe("/views", () => { describe("fetch", () => { beforeEach(async () => { - table = await config.createTable(priceTable()) + table = await config.api.table.save(priceTable) }) it("returns only custom views", async () => { - await config.createLegacyView({ + await saveView({ name: "TestView", field: "Price", - calculation: "stats", + calculation: ViewCalculation.STATISTICS, tableId: table._id, }) - const res = await request - .get(`/api/views`) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) - expect(res.body.length).toBe(1) - expect(res.body.find(({ name }) => name === "TestView")).toBeDefined() + const views = await config.api.legacyView.fetch() + expect(views.length).toBe(1) + expect(views.find(({ name }) => name === "TestView")).toBeDefined() }) }) describe("query", () => { it("returns data for the created view", async () => { - await config.createLegacyView({ + await saveView({ name: "TestView", field: "Price", - calculation: "stats", - tableId: table._id, + calculation: ViewCalculation.STATISTICS, + tableId: table._id!, }) - await config.createRow({ - tableId: table._id, + await config.api.row.save(table._id!, { Price: 1000, }) - await config.createRow({ - tableId: table._id, + await config.api.row.save(table._id!, { Price: 2000, }) - await config.createRow({ - tableId: table._id, + await config.api.row.save(table._id!, { Price: 4000, }) - const res = await request - .get(`/api/views/TestView?calculation=stats`) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) - expect(res.body.length).toBe(1) - expect(res.body).toMatchSnapshot() + const rows = await config.api.legacyView.get("TestView", { + calculation: ViewCalculation.STATISTICS, + }) + expect(rows.length).toBe(1) + expect(rows[0]).toEqual({ + avg: 2333.3333333333335, + count: 3, + group: null, + max: 4000, + min: 1000, + sum: 7000, + sumsqr: 21000000, + }) }) it("returns data for the created view using a group by", async () => { - await config.createLegacyView({ - calculation: "stats", + await saveView({ + calculation: ViewCalculation.STATISTICS, name: "TestView", field: "Price", groupBy: "Category", tableId: table._id, }) - await config.createRow({ - tableId: table._id, + await config.api.row.save(table._id!, { Price: 1000, Category: "One", }) - await config.createRow({ - tableId: table._id, + await config.api.row.save(table._id!, { Price: 2000, Category: "One", }) - await config.createRow({ - tableId: table._id, + await config.api.row.save(table._id!, { Price: 4000, Category: "Two", }) - const res = await request - .get(`/api/views/TestView?calculation=stats&group=Category`) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) - - expect(res.body.length).toBe(2) - expect(res.body).toMatchSnapshot() + const rows = await config.api.legacyView.get("TestView", { + calculation: ViewCalculation.STATISTICS, + group: "Category", + }) + expect(rows.length).toBe(2) + expect(rows[0]).toEqual({ + avg: 1500, + count: 2, + group: "One", + max: 2000, + min: 1000, + sum: 3000, + sumsqr: 5000000, + }) }) }) describe("destroy", () => { it("should be able to delete a view", async () => { - const table = await config.createTable(priceTable()) - const view = await config.createLegacyView() - const res = await request - .delete(`/api/views/${view.name}`) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) - expect(res.body.map).toBeDefined() - expect(res.body.meta.tableId).toEqual(table._id) + const table = await config.api.table.save(priceTable) + const view = await saveView({ tableId: table._id }) + const deletedView = await config.api.legacyView.destroy(view.name!) + expect(deletedView.map).toBeDefined() + expect(deletedView.meta?.tableId).toEqual(table._id) expect(events.view.deleted).toBeCalledTimes(1) }) }) @@ -362,33 +353,44 @@ describe("/views", () => { }) const setupExport = async () => { - const table = await config.createTable() - await config.createRow({ name: "test-name", description: "ùúûü" }) + const table = await config.api.table.save({ + name: "test-table", + type: "table", + sourceId: INTERNAL_TABLE_SOURCE_ID, + sourceType: TableSourceType.INTERNAL, + schema: { + name: { + name: "name", + type: FieldType.STRING, + }, + description: { + name: "description", + type: FieldType.STRING, + }, + }, + }) + await config.api.row.save(table._id!, { + name: "test-name", + description: "ùúûü", + }) return table } - const exportView = async (viewName, format) => { - return request - .get(`/api/views/export?view=${viewName}&format=${format}`) - .set(config.defaultHeaders()) - .expect(200) - } - - const assertJsonExport = res => { - const rows = JSON.parse(res.text) + const assertJsonExport = (res: string) => { + const rows = JSON.parse(res) expect(rows.length).toBe(1) expect(rows[0].name).toBe("test-name") expect(rows[0].description).toBe("ùúûü") } - const assertCSVExport = res => { - expect(res.text).toBe(`"name","description"\n"test-name","ùúûü"`) + const assertCSVExport = (res: string) => { + expect(res).toBe(`"name","description"\n"test-name","ùúûü"`) } it("should be able to export a table as JSON", async () => { const table = await setupExport() - const res = await exportView(table._id, "json") + const res = await config.api.legacyView.export(table._id!, "json") assertJsonExport(res) expect(events.table.exported).toBeCalledTimes(1) @@ -398,7 +400,7 @@ describe("/views", () => { it("should be able to export a table as CSV", async () => { const table = await setupExport() - const res = await exportView(table._id, "csv") + const res = await config.api.legacyView.export(table._id!, "csv") assertCSVExport(res) expect(events.table.exported).toBeCalledTimes(1) @@ -407,10 +409,15 @@ describe("/views", () => { it("should be able to export a view as JSON", async () => { let table = await setupExport() - const view = await config.createLegacyView() - table = await config.getTable(table._id) + const view = await config.api.legacyView.save({ + name: "test-view", + tableId: table._id!, + filters: [], + schema: {}, + }) + table = await config.api.table.get(table._id!) - let res = await exportView(view.name, "json") + let res = await config.api.legacyView.export(view.name!, "json") assertJsonExport(res) expect(events.view.exported).toBeCalledTimes(1) @@ -419,10 +426,15 @@ describe("/views", () => { it("should be able to export a view as CSV", async () => { let table = await setupExport() - const view = await config.createLegacyView() - table = await config.getTable(table._id) + const view = await config.api.legacyView.save({ + name: "test-view", + tableId: table._id!, + filters: [], + schema: {}, + }) + table = await config.api.table.get(table._id!) - let res = await exportView(view.name, "csv") + let res = await config.api.legacyView.export(view.name!, "csv") assertCSVExport(res) expect(events.view.exported).toBeCalledTimes(1) diff --git a/packages/server/src/tests/utilities/api/legacyView.ts b/packages/server/src/tests/utilities/api/legacyView.ts index 38ef70d62a..19a6eceb3b 100644 --- a/packages/server/src/tests/utilities/api/legacyView.ts +++ b/packages/server/src/tests/utilities/api/legacyView.ts @@ -1,8 +1,36 @@ import { Expectations, TestAPI } from "./base" -import { Row } from "@budibase/types" +import { Row, View, ViewCalculation } from "@budibase/types" export class LegacyViewAPI extends TestAPI { - get = async (id: string, expectations?: Expectations) => { - return await this._get(`/api/views/${id}`, { expectations }) + get = async ( + id: string, + query: { calculation: ViewCalculation; group?: string }, + expectations?: Expectations + ) => { + return await this._get(`/api/views/${id}`, { query, expectations }) + } + + save = async (body: View, expectations?: Expectations) => { + return await this._post(`/api/views/`, { body, expectations }) + } + + fetch = async (expectations?: Expectations) => { + return await this._get(`/api/views`, { expectations }) + } + + destroy = async (id: string, expectations?: Expectations) => { + return await this._delete(`/api/views/${id}`, { expectations }) + } + + export = async ( + viewName: string, + format: "json" | "csv" | "jsonWithSchema", + expectations?: Expectations + ) => { + const response = await this._requestRaw("get", `/api/views/export`, { + query: { view: viewName, format }, + expectations, + }) + return response.text } } diff --git a/packages/types/src/documents/app/view.ts b/packages/types/src/documents/app/view.ts index b5a22ec592..7b93d24f3d 100644 --- a/packages/types/src/documents/app/view.ts +++ b/packages/types/src/documents/app/view.ts @@ -30,6 +30,7 @@ export interface View { map?: string reduce?: any meta?: ViewTemplateOpts + groupBy?: string } export interface ViewV2 { From fcd7ee10f1bcc7ba76fb0f78e8f23362abd73323 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Thu, 14 Mar 2024 09:57:00 +0000 Subject: [PATCH 24/29] Create app user facing maintenance page. --- .../pages/builder/maintenance/index.svelte | 6 + .../client/src/components/ClientApp.svelte | 210 +++++++++--------- .../src/components/MaintenanceScreen.svelte | 31 +-- packages/client/src/index.js | 10 - 4 files changed, 132 insertions(+), 125 deletions(-) diff --git a/packages/builder/src/pages/builder/maintenance/index.svelte b/packages/builder/src/pages/builder/maintenance/index.svelte index 95080d0563..1735a93e84 100644 --- a/packages/builder/src/pages/builder/maintenance/index.svelte +++ b/packages/builder/src/pages/builder/maintenance/index.svelte @@ -3,6 +3,12 @@ import { Heading, Body, Button, Layout } from "@budibase/bbui" import { admin } from "stores/portal" import BudibaseLogo from "../portal/_components/BudibaseLogo.svelte" + + $: { + if ($admin.maintenance.length === 0) { + window.location = "/builder" + } + }
diff --git a/packages/client/src/components/ClientApp.svelte b/packages/client/src/components/ClientApp.svelte index 4efa8af4e6..7e30e21ae8 100644 --- a/packages/client/src/components/ClientApp.svelte +++ b/packages/client/src/components/ClientApp.svelte @@ -17,6 +17,7 @@ appStore, devToolsStore, devToolsEnabled, + environmentStore, } from "stores" import NotificationDisplay from "components/overlay/NotificationDisplay.svelte" import ConfirmationDisplay from "components/overlay/ConfirmationDisplay.svelte" @@ -36,6 +37,7 @@ import DevToolsHeader from "components/devtools/DevToolsHeader.svelte" import DevTools from "components/devtools/DevTools.svelte" import FreeFooter from "components/FreeFooter.svelte" + import MaintenanceScreen from "components/MaintenanceScreen.svelte" import licensing from "../licensing" // Provide contexts @@ -111,122 +113,128 @@ class="spectrum spectrum--medium {$themeStore.baseTheme} {$themeStore.theme}" class:builder={$builderStore.inBuilder} > - - - - - - - - {#key $builderStore.selectedComponentId} - {#if $builderStore.inBuilder} - - {/if} - {/key} - - -
- -
- {#if showDevTools} - + {#if $environmentStore.maintenance.length > 0} + + {:else} + + + + + + + + {#key $builderStore.selectedComponentId} + {#if $builderStore.inBuilder} + {/if} + {/key} -
- {#if permissionError} -
- - - {@html ErrorSVG} - - You don't have permission to use this app - - - Ask your administrator to grant you access - - -
- {:else if !$screenStore.activeLayout} -
- - - {@html ErrorSVG} - - Something went wrong rendering your app - - - Get in touch with support if this issue persists - - -
- {:else if embedNoScreens} -
- - - {@html ErrorSVG} - - This Budibase app is not publicly accessible - - -
- {:else} - - {#key $screenStore.activeLayout._id} - - {/key} + +
+ +
+ {#if showDevTools} + + {/if} - + {@html ErrorSVG} + + You don't have permission to use this app + + + Ask your administrator to grant you access + + +
+ {:else if !$screenStore.activeLayout} +
+ + + {@html ErrorSVG} + + Something went wrong rendering your app + + + Get in touch with support if this issue persists + + +
+ {:else if embedNoScreens} +
+ + + {@html ErrorSVG} + + This Budibase app is not publicly accessible + + +
+ {:else} + + {#key $screenStore.activeLayout._id} + + {/key} + + -
+
- - - {#if !$builderStore.inBuilder && licensing.logoEnabled()} - + + {#if $appStore.isDevApp} + + {/if} + {#if $builderStore.inBuilder || $devToolsStore.allowSelection} + + {/if} + {#if $builderStore.inBuilder} + + {/if}
- - - {#if $appStore.isDevApp} - - {/if} - {#if $builderStore.inBuilder || $devToolsStore.allowSelection} - - {/if} - {#if $builderStore.inBuilder} - - - {/if} -
- - - - - + + + + + + {/if}
{/if} diff --git a/packages/client/src/components/MaintenanceScreen.svelte b/packages/client/src/components/MaintenanceScreen.svelte index b43525fdb6..529f20781a 100644 --- a/packages/client/src/components/MaintenanceScreen.svelte +++ b/packages/client/src/components/MaintenanceScreen.svelte @@ -1,6 +1,17 @@ + @@ -11,22 +22,14 @@ {#each maintenanceList as maintenance} {#if maintenance.type === MaintenanceType.SQS_MISSING} - Please upgrade your Budibase installation + Budibase installation requires maintenance - We've detected that the version of Budibase you're using depends - on a more recent version of the CouchDB database than what you - have installed. - - - To resolve this, you can either rollback to a previous version of - Budibase, or follow the migration guide to update to a later - version of CouchDB. + The administrator of this Budibase installation needs to take + actions to update components that are out of date. Please contact + them and show them this warning. More information will be + available when they log into their account. - {/if} {/each}
diff --git a/packages/client/src/index.js b/packages/client/src/index.js index 9a2f0addbc..2c8d310619 100644 --- a/packages/client/src/index.js +++ b/packages/client/src/index.js @@ -1,6 +1,5 @@ import ClientApp from "./components/ClientApp.svelte" import UpdatingApp from "./components/UpdatingApp.svelte" -import MaintenanceScreen from "./components/MaintenanceScreen.svelte" import { builderStore, appStore, @@ -66,15 +65,6 @@ const loadBudibase = async () => { await environmentStore.actions.fetchEnvironment() } - const maintenanceList = get(environmentStore)?.maintenance - if (maintenanceList?.length > 0) { - new MaintenanceScreen({ - target: window.document.body, - props: { maintenanceList }, - }) - return - } - // Register handler for runtime events from the builder window.handleBuilderRuntimeEvent = (type, data) => { if (!window["##BUDIBASE_IN_BUILDER##"]) { From 62cec7289dcb3758c38e70d539c1e9531c4dbc80 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 14 Mar 2024 11:03:43 +0100 Subject: [PATCH 25/29] Use guid instead of words --- packages/backend-core/src/cache/tests/docWritethrough.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/backend-core/src/cache/tests/docWritethrough.spec.ts b/packages/backend-core/src/cache/tests/docWritethrough.spec.ts index 5fe09b95ff..1ae85cfd0b 100644 --- a/packages/backend-core/src/cache/tests/docWritethrough.spec.ts +++ b/packages/backend-core/src/cache/tests/docWritethrough.spec.ts @@ -32,7 +32,7 @@ describe("docWritethrough", () => { describe("patch", () => { function generatePatchObject(fieldCount: number) { - const keys = generator.unique(() => generator.word(), fieldCount) + const keys = generator.unique(() => generator.guid(), fieldCount) return keys.reduce((acc, c) => { acc[c] = generator.word() return acc From c8f37e16e567af9c8f9376f17eff99da44814018 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Thu, 14 Mar 2024 10:23:42 +0000 Subject: [PATCH 26/29] CSS tweaks. --- .../pages/builder/maintenance/index.svelte | 1 + .../src/components/MaintenanceScreen.svelte | 56 ++++++------------- 2 files changed, 19 insertions(+), 38 deletions(-) diff --git a/packages/builder/src/pages/builder/maintenance/index.svelte b/packages/builder/src/pages/builder/maintenance/index.svelte index 1735a93e84..e4c379885a 100644 --- a/packages/builder/src/pages/builder/maintenance/index.svelte +++ b/packages/builder/src/pages/builder/maintenance/index.svelte @@ -51,6 +51,7 @@ flex-direction: row; align-items: center; justify-content: center; + padding: var(--spacing-l); } .hero { margin: var(--spacing-l); diff --git a/packages/client/src/components/MaintenanceScreen.svelte b/packages/client/src/components/MaintenanceScreen.svelte index 529f20781a..a44c1292dd 100644 --- a/packages/client/src/components/MaintenanceScreen.svelte +++ b/packages/client/src/components/MaintenanceScreen.svelte @@ -16,28 +16,24 @@ export let maintenanceList -
-
-
- {#each maintenanceList as maintenance} - {#if maintenance.type === MaintenanceType.SQS_MISSING} - - Budibase installation requires maintenance - - The administrator of this Budibase installation needs to take - actions to update components that are out of date. Please contact - them and show them this warning. More information will be - available when they log into their account. - - - {/if} - {/each} -
-
+
+ {#each maintenanceList as maintenance} + {#if maintenance.type === MaintenanceType.SQS_MISSING} + + Budibase installation requires maintenance + + The administrator of this Budibase installation needs to take actions + to update components that are out of date. Please contact them and + show them this warning. More information will be available when they + log into their account. + + + {/if} + {/each}
From 5918dfebbaecd6a7d79f82bd93d7dbb5f4a1c2bd Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Thu, 14 Mar 2024 10:25:41 +0000 Subject: [PATCH 27/29] Silence lint warnings. --- .../src/pages/builder/portal/_components/BudibaseLogo.svelte | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/builder/src/pages/builder/portal/_components/BudibaseLogo.svelte b/packages/builder/src/pages/builder/portal/_components/BudibaseLogo.svelte index 8276510201..b0dc4cda50 100644 --- a/packages/builder/src/pages/builder/portal/_components/BudibaseLogo.svelte +++ b/packages/builder/src/pages/builder/portal/_components/BudibaseLogo.svelte @@ -3,6 +3,8 @@ import { goto } from "@roxi/routify" + + Budibase Logo $goto("./apps")} />