From f80a207d289220be4c48550331741201a32d6235 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Thu, 4 Apr 2024 17:39:35 +0100 Subject: [PATCH 01/13] Working towards running table.spec.ts against external datasources. --- .../server/src/api/routes/tests/table.spec.ts | 374 ++++++++---------- .../server/src/tests/utilities/api/table.ts | 26 ++ .../server/src/tests/utilities/structures.ts | 29 +- 3 files changed, 206 insertions(+), 223 deletions(-) diff --git a/packages/server/src/api/routes/tests/table.spec.ts b/packages/server/src/api/routes/tests/table.spec.ts index 1038808fe1..6e72236cbb 100644 --- a/packages/server/src/api/routes/tests/table.spec.ts +++ b/packages/server/src/api/routes/tests/table.spec.ts @@ -1,6 +1,7 @@ import { context, events } from "@budibase/backend-core" import { AutoFieldSubType, + Datasource, FieldSubtype, FieldType, INTERNAL_TABLE_SOURCE_ID, @@ -21,23 +22,35 @@ import * as uuid from "uuid" import tk from "timekeeper" import { generator, mocks } from "@budibase/backend-core/tests" -import { TableToBuild } from "../../../tests/utilities/TestConfiguration" +import { DatabaseName, getDatasource } from "../../../integrations/tests/utils" +import { tableForDatasource } from "../../../tests/utilities/structures" tk.freeze(mocks.date.MOCK_DATE) const { basicTable } = setup.structures const ISO_REGEX_PATTERN = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/ -describe("/tables", () => { - let request = setup.getRequest() +describe.each([ + ["internal", undefined], + [DatabaseName.POSTGRES, getDatasource(DatabaseName.POSTGRES)], + // [DatabaseName.MYSQL, getDatasource(DatabaseName.MYSQL)], + // [DatabaseName.SQL_SERVER, getDatasource(DatabaseName.SQL_SERVER)], + // [DatabaseName.MARIADB, getDatasource(DatabaseName.MARIADB)], +])("/tables (%s)", (_, dsProvider) => { + let isInternal: boolean + let datasource: Datasource | undefined let config = setup.getConfig() - let appId: string afterAll(setup.afterAll) beforeAll(async () => { - const app = await config.init() - appId = app.appId + await config.init() + if (dsProvider) { + datasource = await config.api.datasource.create(await dsProvider) + isInternal = false + } else { + isInternal = true + } }) describe("create", () => { @@ -45,33 +58,15 @@ describe("/tables", () => { jest.clearAllMocks() }) - const createTable = (table?: Table) => { - if (!table) { - table = basicTable() - } - return request - .post(`/api/tables`) - .send(table) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) - } - it("returns a success message when the table is successfully created", async () => { - const res = await createTable() - - expect((res as any).res.statusMessage).toEqual( - "Table TestTable saved successfully." - ) - expect(res.body.name).toEqual("TestTable") + const table = await config.api.table.save(basicTable(datasource)) + expect(table.name).toEqual("TestTable") expect(events.table.created).toHaveBeenCalledTimes(1) - expect(events.table.created).toHaveBeenCalledWith(res.body) + expect(events.table.created).toHaveBeenCalledWith(table) }) it("creates all the passed fields", async () => { - const tableData: TableToBuild = { - name: "TestTable", - type: "table", + const tableData = tableForDatasource(datasource, { schema: { autoId: { name: "id", @@ -92,8 +87,9 @@ describe("/tables", () => { tableId: "tableId", }, }, - } - const testTable = await config.createTable(tableData) + }) + + const testTable = await config.api.table.save(tableData) const expected: Table = { ...tableData, @@ -116,8 +112,6 @@ describe("/tables", () => { }, }, }, - sourceType: TableSourceType.INTERNAL, - sourceId: expect.any(String), _rev: expect.stringMatching(/^1-.+/), _id: expect.any(String), createdAt: mocks.date.MOCK_DATE.toISOString(), @@ -133,14 +127,14 @@ describe("/tables", () => { const table: SaveTableRequest = basicTable() table.rows = [{ name: "test-name", description: "test-desc" }] - const res = await createTable(table) + const res = await config.api.table.save(table) expect(events.table.created).toHaveBeenCalledTimes(1) - expect(events.table.created).toHaveBeenCalledWith(res.body) + expect(events.table.created).toHaveBeenCalledWith(res) expect(events.table.imported).toHaveBeenCalledTimes(1) - expect(events.table.imported).toHaveBeenCalledWith(res.body) + expect(events.table.imported).toHaveBeenCalledWith(res) expect(events.rows.imported).toHaveBeenCalledTimes(1) - expect(events.rows.imported).toHaveBeenCalledWith(res.body, 1) + expect(events.rows.imported).toHaveBeenCalledWith(res, 1) }) it("should apply authorization to endpoint", async () => { @@ -155,17 +149,24 @@ describe("/tables", () => { describe("update", () => { it("updates a table", async () => { - const testTable = await config.createTable() - - const res = await request - .post(`/api/tables`) - .send(testTable) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) + const table = await config.api.table.save({ + name: "TestTable", + type: "table", + sourceId: INTERNAL_TABLE_SOURCE_ID, + sourceType: TableSourceType.INTERNAL, + schema: { + name: { + type: FieldType.STRING, + name: "name", + constraints: { + type: "string", + }, + }, + }, + }) expect(events.table.updated).toHaveBeenCalledTimes(1) - expect(events.table.updated).toHaveBeenCalledWith(res.body) + expect(events.table.updated).toHaveBeenCalledWith(table) }) it("updates all the row fields for a table when a schema key is renamed", async () => { @@ -179,111 +180,93 @@ describe("/tables", () => { filters: [], }) - const testRow = await request - .post(`/api/${testTable._id}/rows`) - .send({ - name: "test", - }) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) + const testRow = await config.api.row.save(testTable._id!, { + name: "test", + }) - const updatedTable = await request - .post(`/api/tables`) - .send({ - _id: testTable._id, - _rev: testTable._rev, - name: "TestTable", - key: "name", - _rename: { - old: "name", - updated: "updatedName", - }, - schema: { - updatedName: { type: "string" }, - }, - }) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) - expect((updatedTable as any).res.statusMessage).toEqual( - "Table TestTable saved successfully." - ) - expect(updatedTable.body.name).toEqual("TestTable") + const updatedTable = await config.api.table.save({ + _id: testTable._id, + _rev: testTable._rev, + type: "table", + sourceId: datasource ? datasource._id! : INTERNAL_TABLE_SOURCE_ID, + sourceType: isInternal + ? TableSourceType.INTERNAL + : TableSourceType.EXTERNAL, + name: "TestTable", + _rename: { + old: "name", + updated: "updatedName", + }, + schema: { + updatedName: { type: FieldType.STRING, name: "updatedName" }, + }, + }) - const res = await request - .get(`/api/${testTable._id}/rows/${testRow.body._id}`) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) + expect(updatedTable.name).toEqual("TestTable") - expect(res.body.updatedName).toEqual("test") - expect(res.body.name).toBeUndefined() + const res = await config.api.row.get(testTable._id!, testRow._id!) + expect(res.updatedName).toEqual("test") + expect(res.name).toBeUndefined() }) - it("updates only the passed fields", async () => { - const testTable = await config.createTable({ - name: "TestTable", - type: "table", - schema: { - autoId: { - name: "id", - type: FieldType.NUMBER, - subtype: AutoFieldSubType.AUTO_ID, - autocolumn: true, - constraints: { - type: "number", - presence: false, + it.only("updates only the passed fields", async () => { + const table = await config.api.table.save( + tableForDatasource(datasource, { + name: "TestTable", + schema: { + autoId: { + name: "id", + type: FieldType.NUMBER, + subtype: AutoFieldSubType.AUTO_ID, + autocolumn: true, + constraints: { + type: "number", + presence: false, + }, }, }, - }, - views: { - view1: { - id: "viewId", - version: 2, - name: "table view", - tableId: "tableId", - }, - }, - }) - - const response = await request - .post(`/api/tables`) - .send({ - ...testTable, - name: "UpdatedName", }) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) + ) - expect(response.body).toEqual({ - ...testTable, + const updatedTable = await config.api.table.save({ + ...table, name: "UpdatedName", - _rev: expect.stringMatching(/^2-.+/), }) - const persistedTable = await config.api.table.get(testTable._id!) - expect(persistedTable).toEqual({ - ...testTable, + let expected: Table = { + ...table, name: "UpdatedName", - _rev: expect.stringMatching(/^2-.+/), - }) + _id: expect.any(String), + } + if (isInternal) { + expected._rev = expect.stringMatching(/^2-.+/) + } + + expect(updatedTable).toEqual(expected) + + const persistedTable = await config.api.table.get(updatedTable._id!) + expected = { + ...table, + name: "UpdatedName", + _id: updatedTable._id, + } + if (datasource?.isSQL) { + expected.sql = true + } + if (isInternal) { + expected._rev = expect.stringMatching(/^2-.+/) + } + expect(persistedTable).toEqual(expected) }) describe("user table", () => { it("should add roleId and email field when adjusting user table schema", async () => { - const res = await request - .post(`/api/tables`) - .send({ - ...basicTable(), - _id: "ta_users", - }) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) - expect(res.body.schema.email).toBeDefined() - expect(res.body.schema.roleId).toBeDefined() + const table = await config.api.table.save({ + ...basicTable(datasource), + _id: "ta_users", + }) + expect(table.schema.email).toBeDefined() + expect(table.schema.roleId).toBeDefined() }) }) @@ -295,12 +278,7 @@ describe("/tables", () => { ...basicTable(), } - const response = await request - .post(`/api/tables`) - .send(saveTableRequest) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) + const response = await config.api.table.save(saveTableRequest) const expectedResponse = { ...saveTableRequest, @@ -311,15 +289,13 @@ describe("/tables", () => { views: {}, } delete expectedResponse._add - - expect(response.status).toBe(200) - expect(response.body).toEqual(expectedResponse) + expect(response).toEqual(expectedResponse) }) }) describe("import", () => { it("imports rows successfully", async () => { - const table = await config.createTable() + const table = await config.api.table.save(basicTable(datasource)) const importRequest = { schema: table.schema, rows: [{ name: "test-name", description: "test-desc" }], @@ -327,12 +303,7 @@ describe("/tables", () => { jest.clearAllMocks() - await request - .post(`/api/tables/${table._id}/import`) - .send(importRequest) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) + await config.api.table.import(table._id!, importRequest) expect(events.table.created).not.toHaveBeenCalled() expect(events.rows.imported).toHaveBeenCalledTimes(1) @@ -346,22 +317,23 @@ describe("/tables", () => { }) it("should update Auto ID field after bulk import", async () => { - const table = await config.createTable({ - name: "TestTable", - type: "table", - schema: { - autoId: { - name: "id", - type: FieldType.NUMBER, - subtype: AutoFieldSubType.AUTO_ID, - autocolumn: true, - constraints: { - type: "number", - presence: false, + const table = await config.api.table.save( + tableForDatasource(datasource, { + name: "TestTable", + schema: { + autoId: { + name: "id", + type: FieldType.NUMBER, + subtype: AutoFieldSubType.AUTO_ID, + autocolumn: true, + constraints: { + type: "number", + presence: false, + }, }, }, - }, - }) + }) + ) let row = await config.api.row.save(table._id!, {}) expect(row.autoId).toEqual(1) @@ -381,11 +353,7 @@ describe("/tables", () => { const enrichViewSchemasMock = jest.spyOn(sdk.tables, "enrichViewSchemas") beforeEach(async () => { - testTable = await config.createTable(testTable) - }) - - afterEach(() => { - delete testTable._rev + testTable = await config.api.table.save(basicTable(datasource)) }) afterAll(() => { @@ -393,17 +361,12 @@ describe("/tables", () => { }) it("returns all the tables for that instance in the response body", async () => { - const res = await request - .get(`/api/tables`) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) - - const table = res.body.find((t: Table) => t._id === testTable._id) + const res = await config.api.table.fetch() + const table = res.find(t => t._id === testTable._id) expect(table).toBeDefined() - expect(table.name).toEqual(testTable.name) - expect(table.type).toEqual("table") - expect(table.sourceType).toEqual("internal") + expect(table!.name).toEqual(testTable.name) + expect(table!.type).toEqual("table") + expect(table!.sourceType).toEqual("internal") }) it("should apply authorization to endpoint", async () => { @@ -421,13 +384,8 @@ describe("/tables", () => { await config.api.viewV2.create({ tableId, name: generator.guid() }), ] - const res = await request - .get(`/api/tables`) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) - - expect(res.body).toEqual( + const res = await config.api.table.fetch() + expect(res).toEqual( expect.arrayContaining([ expect.objectContaining({ _id: tableId, @@ -481,32 +439,22 @@ describe("/tables", () => { describe("indexing", () => { it("should be able to create a table with indexes", async () => { - await context.doInAppContext(appId, async () => { + await context.doInAppContext(config.getAppId(), async () => { const db = context.getAppDB() const indexCount = (await db.getIndexes()).total_rows const table = basicTable() table.indexes = ["name"] - const res = await request - .post(`/api/tables`) - .send(table) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) - expect(res.body._id).toBeDefined() - expect(res.body._rev).toBeDefined() + const res = await config.api.table.save(table) + expect(res._id).toBeDefined() + expect(res._rev).toBeDefined() expect((await db.getIndexes()).total_rows).toEqual(indexCount + 1) // update index to see what happens table.indexes = ["name", "description"] - await request - .post(`/api/tables`) - .send({ - ...table, - _id: res.body._id, - _rev: res.body._rev, - }) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) + await config.api.table.save({ + ...table, + _id: res._id, + _rev: res._rev, + }) // shouldn't have created a new index expect((await db.getIndexes()).total_rows).toEqual(indexCount + 1) }) @@ -521,12 +469,9 @@ describe("/tables", () => { }) it("returns a success response when a table is deleted.", async () => { - const res = await request - .delete(`/api/tables/${testTable._id}/${testTable._rev}`) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) - expect(res.body.message).toEqual(`Table ${testTable._id} deleted.`) + await config.api.table.destroy(testTable._id!, testTable._rev!, { + body: { message: `Table ${testTable._id} deleted.` }, + }) expect(events.table.deleted).toHaveBeenCalledTimes(1) expect(events.table.deleted).toHaveBeenCalledWith({ ...testTable, @@ -559,12 +504,9 @@ describe("/tables", () => { }, }) - const res = await request - .delete(`/api/tables/${testTable._id}/${testTable._rev}`) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) - expect(res.body.message).toEqual(`Table ${testTable._id} deleted.`) + await config.api.table.destroy(testTable._id!, testTable._rev!, { + body: { message: `Table ${testTable._id} deleted.` }, + }) const dependentTable = await config.api.table.get(linkedTable._id!) expect(dependentTable.schema.TestTable).not.toBeDefined() }) diff --git a/packages/server/src/tests/utilities/api/table.ts b/packages/server/src/tests/utilities/api/table.ts index 49105a3883..d918ba8b9a 100644 --- a/packages/server/src/tests/utilities/api/table.ts +++ b/packages/server/src/tests/utilities/api/table.ts @@ -1,4 +1,6 @@ import { + BulkImportRequest, + BulkImportResponse, MigrateRequest, MigrateResponse, SaveTableRequest, @@ -39,4 +41,28 @@ export class TableAPI extends TestAPI { expectations, }) } + + import = async ( + tableId: string, + data: BulkImportRequest, + expectations?: Expectations + ): Promise => { + return await this._post( + `/api/tables/${tableId}/import`, + { + body: data, + expectations, + } + ) + } + + destroy = async ( + tableId: string, + revId: string, + expectations?: Expectations + ): Promise => { + return await this._delete(`/api/tables/${tableId}/${revId}`, { + expectations, + }) + } } diff --git a/packages/server/src/tests/utilities/structures.ts b/packages/server/src/tests/utilities/structures.ts index 5b50bd1175..f4e59a3c78 100644 --- a/packages/server/src/tests/utilities/structures.ts +++ b/packages/server/src/tests/utilities/structures.ts @@ -26,15 +26,30 @@ import { WebhookActionType, } from "@budibase/types" import { LoopInput, LoopStepType } from "../../definitions/automations" +import { merge } from "lodash" const { BUILTIN_ROLE_IDS } = roles -export function basicTable(): Table { - return { - name: "TestTable", - type: "table", - sourceId: INTERNAL_TABLE_SOURCE_ID, - sourceType: TableSourceType.INTERNAL, +export function tableForDatasource( + datasource?: Datasource, + ...extra: Partial[] +): Table { + return merge( + { + name: "TestTable", + type: "table", + sourceType: datasource + ? TableSourceType.EXTERNAL + : TableSourceType.INTERNAL, + sourceId: datasource ? datasource._id! : INTERNAL_TABLE_SOURCE_ID, + schema: {}, + }, + ...extra + ) +} + +export function basicTable(datasource?: Datasource): Table { + return tableForDatasource(datasource, { schema: { name: { type: FieldType.STRING, @@ -51,7 +66,7 @@ export function basicTable(): Table { }, }, }, - } + }) } export function basicView(tableId: string) { From bc072e1424c2ab7f5d6ec5892daef6fa58ace928 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Fri, 5 Apr 2024 11:50:27 +0100 Subject: [PATCH 02/13] More progress on fixing up table.spec.ts. --- .../server/src/api/routes/tests/table.spec.ts | 169 ++++++++++-------- .../server/src/tests/utilities/structures.ts | 41 +++-- 2 files changed, 121 insertions(+), 89 deletions(-) diff --git a/packages/server/src/api/routes/tests/table.spec.ts b/packages/server/src/api/routes/tests/table.spec.ts index 6e72236cbb..400ff858a7 100644 --- a/packages/server/src/api/routes/tests/table.spec.ts +++ b/packages/server/src/api/routes/tests/table.spec.ts @@ -17,7 +17,6 @@ import { } from "@budibase/types" import { checkBuilderEndpoint } from "./utilities/TestFunctions" import * as setup from "./utilities" -import sdk from "../../../sdk" import * as uuid from "uuid" import tk from "timekeeper" @@ -59,8 +58,11 @@ describe.each([ }) it("returns a success message when the table is successfully created", async () => { - const table = await config.api.table.save(basicTable(datasource)) - expect(table.name).toEqual("TestTable") + const name = generator.guid() + const table = await config.api.table.save( + tableForDatasource(datasource, { name }) + ) + expect(table.name).toEqual(name) expect(events.table.created).toHaveBeenCalledTimes(1) expect(events.table.created).toHaveBeenCalledWith(table) }) @@ -149,20 +151,23 @@ describe.each([ describe("update", () => { it("updates a table", async () => { - const table = await config.api.table.save({ - name: "TestTable", - type: "table", - sourceId: INTERNAL_TABLE_SOURCE_ID, - sourceType: TableSourceType.INTERNAL, - schema: { - name: { - type: FieldType.STRING, - name: "name", - constraints: { - type: "string", + const table = await config.api.table.save( + tableForDatasource(datasource, { + schema: { + name: { + type: FieldType.STRING, + name: "name", + constraints: { + type: "string", + }, }, }, - }, + }) + ) + + await config.api.table.save({ + ...table, + name: generator.guid(), }) expect(events.table.updated).toHaveBeenCalledTimes(1) @@ -209,7 +214,7 @@ describe.each([ expect(res.name).toBeUndefined() }) - it.only("updates only the passed fields", async () => { + it("updates only the passed fields", async () => { const table = await config.api.table.save( tableForDatasource(datasource, { name: "TestTable", @@ -350,23 +355,20 @@ describe.each([ describe("fetch", () => { let testTable: Table - const enrichViewSchemasMock = jest.spyOn(sdk.tables, "enrichViewSchemas") beforeEach(async () => { - testTable = await config.api.table.save(basicTable(datasource)) + testTable = await config.api.table.save( + basicTable(datasource, { name: generator.guid() }) + ) }) - afterAll(() => { - enrichViewSchemasMock.mockRestore() - }) - - it("returns all the tables for that instance in the response body", async () => { + it("returns all tables", async () => { const res = await config.api.table.fetch() const table = res.find(t => t._id === testTable._id) expect(table).toBeDefined() expect(table!.name).toEqual(testTable.name) expect(table!.type).toEqual("table") - expect(table!.sourceType).toEqual("internal") + expect(table!.sourceType).toEqual(testTable.sourceType) }) it("should apply authorization to endpoint", async () => { @@ -377,63 +379,84 @@ describe.each([ }) }) - it("should fetch views", async () => { - const tableId = config.table!._id! - const views = [ - await config.api.viewV2.create({ tableId, name: generator.guid() }), - await config.api.viewV2.create({ tableId, name: generator.guid() }), - ] + it("should enrich the view schemas", async () => { + const viewV2 = await config.api.viewV2.create({ + tableId: testTable._id!, + name: generator.guid(), + }) + const legacyView = await config.api.legacyView.save({ + tableId: testTable._id!, + name: generator.guid(), + filters: [], + schema: {}, + }) const res = await config.api.table.fetch() - expect(res).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - _id: tableId, - views: views.reduce((p, c) => { - p[c.name] = { ...c, schema: expect.anything() } - return p - }, {} as any), - }), - ]) - ) - }) - it("should enrich the view schemas for viewsV2", async () => { - const tableId = config.table!._id! - enrichViewSchemasMock.mockImplementation(t => ({ - ...t, - views: { - view1: { - version: 2, - name: "view1", - schema: {}, - id: "new_view_id", - tableId: t._id!, + const table = res.find(t => t._id === testTable._id) + expect(table).toBeDefined() + expect(table!.views![viewV2.name]).toBeDefined() + expect(table!.views![viewV2.name!]).toEqual({ + ...viewV2, + schema: { + description: { + constraints: { + type: "string", + }, + name: "description", + type: "string", + visible: false, + }, + name: { + constraints: { + type: "string", + }, + name: "name", + type: "string", + visible: false, }, }, - })) + }) - await config.api.viewV2.create({ tableId, name: generator.guid() }) - await config.createLegacyView() - - const res = await config.api.table.fetch() - - expect(res).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - _id: tableId, - views: { - view1: { - version: 2, - name: "view1", - schema: {}, - id: "new_view_id", - tableId, + if (isInternal) { + expect(table!.views![legacyView.name!]).toBeDefined() + expect(table!.views![legacyView.name!]).toEqual({ + ...legacyView, + schema: { + description: { + constraints: { + type: "string", }, + name: "description", + type: "string", }, - }), - ]) - ) + name: { + constraints: { + type: "string", + }, + name: "name", + type: "string", + }, + }, + }) + } + + // expect(res).toEqual( + // expect.arrayContaining([ + // expect.objectContaining({ + // _id: tableId, + // views: { + // view1: { + // version: 2, + // name: "view1", + // schema: {}, + // id: "new_view_id", + // tableId, + // }, + // }, + // }), + // ]) + // ) }) }) diff --git a/packages/server/src/tests/utilities/structures.ts b/packages/server/src/tests/utilities/structures.ts index f4e59a3c78..2a32489c30 100644 --- a/packages/server/src/tests/utilities/structures.ts +++ b/packages/server/src/tests/utilities/structures.ts @@ -27,6 +27,7 @@ import { } from "@budibase/types" import { LoopInput, LoopStepType } from "../../definitions/automations" import { merge } from "lodash" +import { generator } from "@budibase/backend-core/tests" const { BUILTIN_ROLE_IDS } = roles @@ -36,7 +37,7 @@ export function tableForDatasource( ): Table { return merge( { - name: "TestTable", + name: generator.guid(), type: "table", sourceType: datasource ? TableSourceType.EXTERNAL @@ -48,25 +49,33 @@ export function tableForDatasource( ) } -export function basicTable(datasource?: Datasource): Table { - return tableForDatasource(datasource, { - schema: { - name: { - type: FieldType.STRING, - name: "name", - constraints: { - type: "string", +export function basicTable( + datasource?: Datasource, + ...extra: Partial
[] +): Table { + return tableForDatasource( + datasource, + { + name: "TestTable", + 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", + }, }, }, }, - }) + ...extra + ) } export function basicView(tableId: string) { From bcc09bd86e6494b12eb79bef193a1022fb529e38 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Fri, 5 Apr 2024 16:38:57 +0100 Subject: [PATCH 03/13] table.spec.ts passing for internal and postgres --- .../server/src/api/controllers/table/index.ts | 2 +- .../server/src/api/routes/tests/row.spec.ts | 33 ++ .../server/src/api/routes/tests/table.spec.ts | 305 +++++++----------- .../src/sdk/app/tables/external/index.ts | 16 + .../types/src/documents/app/table/schema.ts | 4 + 5 files changed, 166 insertions(+), 194 deletions(-) diff --git a/packages/server/src/api/controllers/table/index.ts b/packages/server/src/api/controllers/table/index.ts index 69305c461e..f799113333 100644 --- a/packages/server/src/api/controllers/table/index.ts +++ b/packages/server/src/api/controllers/table/index.ts @@ -84,8 +84,8 @@ export async function save(ctx: UserCtx) { } let savedTable = await api.save(ctx, renaming) if (!table._id) { - await events.table.created(savedTable) savedTable = sdk.tables.enrichViewSchemas(savedTable) + await events.table.created(savedTable) } else { await events.table.updated(savedTable) } diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index f9e05c5bd8..d9895466a5 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -722,6 +722,39 @@ describe.each([ }) }) + describe("bulkImport", () => { + isInternal && + it("should update Auto ID field after bulk import", async () => { + const table = await config.api.table.save( + saveTableRequest({ + primary: ["autoId"], + schema: { + autoId: { + name: "autoId", + type: FieldType.NUMBER, + subtype: AutoFieldSubType.AUTO_ID, + autocolumn: true, + constraints: { + type: "number", + presence: false, + }, + }, + }, + }) + ) + + let row = await config.api.row.save(table._id!, {}) + expect(row.autoId).toEqual(1) + + await config.api.row.bulkImport(table._id!, { + rows: [{ autoId: 2 }], + }) + + row = await config.api.row.save(table._id!, {}) + expect(row.autoId).toEqual(3) + }) + }) + describe("enrich", () => { beforeAll(async () => { table = await config.api.table.save(defaultTable()) diff --git a/packages/server/src/api/routes/tests/table.spec.ts b/packages/server/src/api/routes/tests/table.spec.ts index 400ff858a7..6d2dd791b1 100644 --- a/packages/server/src/api/routes/tests/table.spec.ts +++ b/packages/server/src/api/routes/tests/table.spec.ts @@ -6,7 +6,6 @@ import { FieldType, INTERNAL_TABLE_SOURCE_ID, InternalTable, - NumberFieldMetadata, RelationshipType, Row, SaveTableRequest, @@ -14,17 +13,16 @@ import { TableSourceType, User, ViewCalculation, + ViewV2, } from "@budibase/types" import { checkBuilderEndpoint } from "./utilities/TestFunctions" import * as setup from "./utilities" import * as uuid from "uuid" -import tk from "timekeeper" -import { generator, mocks } from "@budibase/backend-core/tests" +import { generator } from "@budibase/backend-core/tests" import { DatabaseName, getDatasource } from "../../../integrations/tests/utils" import { tableForDatasource } from "../../../tests/utilities/structures" - -tk.freeze(mocks.date.MOCK_DATE) +import timekeeper from "timekeeper" const { basicTable } = setup.structures const ISO_REGEX_PATTERN = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/ @@ -57,7 +55,7 @@ describe.each([ jest.clearAllMocks() }) - it("returns a success message when the table is successfully created", async () => { + it("creates a table successfully", async () => { const name = generator.guid() const table = await config.api.table.save( tableForDatasource(datasource, { name }) @@ -67,64 +65,6 @@ describe.each([ expect(events.table.created).toHaveBeenCalledWith(table) }) - it("creates all the passed fields", async () => { - const tableData = tableForDatasource(datasource, { - schema: { - autoId: { - name: "id", - type: FieldType.NUMBER, - subtype: AutoFieldSubType.AUTO_ID, - autocolumn: true, - constraints: { - type: "number", - presence: false, - }, - }, - }, - views: { - "table view": { - id: "viewId", - version: 2, - name: "table view", - tableId: "tableId", - }, - }, - }) - - const testTable = await config.api.table.save(tableData) - - const expected: Table = { - ...tableData, - type: "table", - views: { - "table view": { - ...tableData.views!["table view"], - schema: { - autoId: { - autocolumn: true, - constraints: { - presence: false, - type: "number", - }, - name: "id", - type: FieldType.NUMBER, - subtype: AutoFieldSubType.AUTO_ID, - visible: false, - } as NumberFieldMetadata, - }, - }, - }, - _rev: expect.stringMatching(/^1-.+/), - _id: expect.any(String), - createdAt: mocks.date.MOCK_DATE.toISOString(), - updatedAt: mocks.date.MOCK_DATE.toISOString(), - } - expect(testTable).toEqual(expected) - - const persistedTable = await config.api.table.get(testTable._id!) - expect(persistedTable).toEqual(expected) - }) - it("creates a table via data import", async () => { const table: SaveTableRequest = basicTable() table.rows = [{ name: "test-name", description: "test-desc" }] @@ -165,17 +105,17 @@ describe.each([ }) ) - await config.api.table.save({ + const updatedTable = await config.api.table.save({ ...table, name: generator.guid(), }) expect(events.table.updated).toHaveBeenCalledTimes(1) - expect(events.table.updated).toHaveBeenCalledWith(table) + expect(events.table.updated).toHaveBeenCalledWith(updatedTable) }) it("updates all the row fields for a table when a schema key is renamed", async () => { - const testTable = await config.createTable() + const testTable = await config.api.table.save(basicTable(datasource)) await config.createLegacyView({ name: "TestView", field: "Price", @@ -215,64 +155,68 @@ describe.each([ }) it("updates only the passed fields", async () => { - const table = await config.api.table.save( - tableForDatasource(datasource, { - name: "TestTable", - schema: { - autoId: { - name: "id", - type: FieldType.NUMBER, - subtype: AutoFieldSubType.AUTO_ID, - autocolumn: true, - constraints: { - type: "number", - presence: false, + await timekeeper.withFreeze(new Date(2021, 1, 1), async () => { + const table = await config.api.table.save( + tableForDatasource(datasource, { + schema: { + autoId: { + name: "id", + type: FieldType.NUMBER, + subtype: AutoFieldSubType.AUTO_ID, + autocolumn: true, + constraints: { + type: "number", + presence: false, + }, }, }, - }, + }) + ) + + const newName = generator.guid() + + const updatedTable = await config.api.table.save({ + ...table, + name: newName, }) - ) - const updatedTable = await config.api.table.save({ - ...table, - name: "UpdatedName", + let expected: Table = { + ...table, + name: newName, + _id: expect.any(String), + } + if (isInternal) { + expected._rev = expect.stringMatching(/^2-.+/) + } + + expect(updatedTable).toEqual(expected) + + const persistedTable = await config.api.table.get(updatedTable._id!) + expected = { + ...table, + name: newName, + _id: updatedTable._id, + } + if (datasource?.isSQL) { + expected.sql = true + } + if (isInternal) { + expected._rev = expect.stringMatching(/^2-.+/) + } + expect(persistedTable).toEqual(expected) }) - - let expected: Table = { - ...table, - name: "UpdatedName", - _id: expect.any(String), - } - if (isInternal) { - expected._rev = expect.stringMatching(/^2-.+/) - } - - expect(updatedTable).toEqual(expected) - - const persistedTable = await config.api.table.get(updatedTable._id!) - expected = { - ...table, - name: "UpdatedName", - _id: updatedTable._id, - } - if (datasource?.isSQL) { - expected.sql = true - } - if (isInternal) { - expected._rev = expect.stringMatching(/^2-.+/) - } - expect(persistedTable).toEqual(expected) }) describe("user table", () => { - it("should add roleId and email field when adjusting user table schema", async () => { - const table = await config.api.table.save({ - ...basicTable(datasource), - _id: "ta_users", + isInternal && + it("should add roleId and email field when adjusting user table schema", async () => { + const table = await config.api.table.save({ + ...basicTable(datasource), + _id: "ta_users", + }) + expect(table.schema.email).toBeDefined() + expect(table.schema.roleId).toBeDefined() }) - expect(table.schema.email).toBeDefined() - expect(table.schema.roleId).toBeDefined() - }) }) it("should add a new column for an internal DB table", async () => { @@ -300,7 +244,10 @@ describe.each([ describe("import", () => { it("imports rows successfully", async () => { - const table = await config.api.table.save(basicTable(datasource)) + const name = generator.guid() + const table = await config.api.table.save( + basicTable(datasource, { name }) + ) const importRequest = { schema: table.schema, rows: [{ name: "test-name", description: "test-desc" }], @@ -314,43 +261,12 @@ describe.each([ expect(events.rows.imported).toHaveBeenCalledTimes(1) expect(events.rows.imported).toHaveBeenCalledWith( expect.objectContaining({ - name: "TestTable", + name, _id: table._id, }), 1 ) }) - - it("should update Auto ID field after bulk import", async () => { - const table = await config.api.table.save( - tableForDatasource(datasource, { - name: "TestTable", - schema: { - autoId: { - name: "id", - type: FieldType.NUMBER, - subtype: AutoFieldSubType.AUTO_ID, - autocolumn: true, - constraints: { - type: "number", - presence: false, - }, - }, - }, - }) - ) - - let row = await config.api.row.save(table._id!, {}) - expect(row.autoId).toEqual(1) - - await config.api.row.bulkImport(table._id!, { - rows: [{ autoId: 2 }], - identifierFields: [], - }) - - row = await config.api.row.save(table._id!, {}) - expect(row.autoId).toEqual(3) - }) }) describe("fetch", () => { @@ -396,7 +312,8 @@ describe.each([ const table = res.find(t => t._id === testTable._id) expect(table).toBeDefined() expect(table!.views![viewV2.name]).toBeDefined() - expect(table!.views![viewV2.name!]).toEqual({ + + const expectedViewV2: ViewV2 = { ...viewV2, schema: { description: { @@ -404,7 +321,7 @@ describe.each([ type: "string", }, name: "description", - type: "string", + type: FieldType.STRING, visible: false, }, name: { @@ -412,11 +329,22 @@ describe.each([ type: "string", }, name: "name", - type: "string", + type: FieldType.STRING, visible: false, }, }, - }) + } + + if (!isInternal) { + expectedViewV2.schema!.id = { + name: "id", + type: FieldType.NUMBER, + visible: false, + autocolumn: true, + } + } + + expect(table!.views![viewV2.name!]).toEqual(expectedViewV2) if (isInternal) { expect(table!.views![legacyView.name!]).toBeDefined() @@ -440,23 +368,16 @@ describe.each([ }, }) } + }) + }) - // expect(res).toEqual( - // expect.arrayContaining([ - // expect.objectContaining({ - // _id: tableId, - // views: { - // view1: { - // version: 2, - // name: "view1", - // schema: {}, - // id: "new_view_id", - // tableId, - // }, - // }, - // }), - // ]) - // ) + describe("get", () => { + it("returns a table", async () => { + const table = await config.api.table.save( + basicTable(datasource, { name: generator.guid() }) + ) + const res = await config.api.table.get(table._id!) + expect(res).toEqual(table) }) }) @@ -781,33 +702,31 @@ describe.each([ describe("unhappy paths", () => { let table: Table beforeAll(async () => { - table = await config.api.table.save({ - name: "table", - type: "table", - sourceId: INTERNAL_TABLE_SOURCE_ID, - sourceType: TableSourceType.INTERNAL, - schema: { - "user relationship": { - type: FieldType.LINK, - fieldName: "test", - name: "user relationship", - constraints: { - type: "array", - presence: false, + table = await config.api.table.save( + tableForDatasource(datasource, { + schema: { + "user relationship": { + type: FieldType.LINK, + fieldName: "test", + name: "user relationship", + constraints: { + type: "array", + presence: false, + }, + relationshipType: RelationshipType.MANY_TO_ONE, + tableId: InternalTable.USER_METADATA, }, - relationshipType: RelationshipType.MANY_TO_ONE, - tableId: InternalTable.USER_METADATA, - }, - num: { - type: FieldType.NUMBER, - name: "num", - constraints: { - type: "number", - presence: false, + num: { + type: FieldType.NUMBER, + name: "num", + constraints: { + type: "number", + presence: false, + }, }, }, - }, - }) + }) + ) }) it("should fail if the new column name is blank", async () => { diff --git a/packages/server/src/sdk/app/tables/external/index.ts b/packages/server/src/sdk/app/tables/external/index.ts index 0ace19d00e..65cd4a07c1 100644 --- a/packages/server/src/sdk/app/tables/external/index.ts +++ b/packages/server/src/sdk/app/tables/external/index.ts @@ -48,6 +48,18 @@ export async function save( oldTable = await getTable(tableId) } + if ( + !oldTable && + (tableToSave.primary == null || tableToSave.primary.length === 0) + ) { + tableToSave.primary = ["id"] + tableToSave.schema.id = { + type: FieldType.NUMBER, + autocolumn: true, + name: "id", + } + } + if (hasTypeChanged(tableToSave, oldTable)) { throw new Error("A column type has changed.") } @@ -183,6 +195,10 @@ export async function save( // that the datasource definition changed const updatedDatasource = await datasourceSdk.get(datasource._id!) + if (updatedDatasource.isSQL) { + tableToSave.sql = true + } + return { datasource: updatedDatasource, table: tableToSave } } diff --git a/packages/types/src/documents/app/table/schema.ts b/packages/types/src/documents/app/table/schema.ts index 45e39268ac..42bac7d673 100644 --- a/packages/types/src/documents/app/table/schema.ts +++ b/packages/types/src/documents/app/table/schema.ts @@ -10,10 +10,14 @@ import { } from "./constants" export interface UIFieldMetadata { + constraints?: FieldConstraints + name?: string + type?: FieldType order?: number width?: number visible?: boolean icon?: string + autocolumn?: boolean } interface BaseRelationshipFieldMetadata From de2afa01b98044338fc0239cc04c8bf1717e2772 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Fri, 5 Apr 2024 16:54:29 +0100 Subject: [PATCH 04/13] table.spec.ts passing with MySQL --- packages/server/src/api/routes/tests/table.spec.ts | 2 +- packages/server/src/integrations/base/sqlTable.ts | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/server/src/api/routes/tests/table.spec.ts b/packages/server/src/api/routes/tests/table.spec.ts index 6d2dd791b1..360c76d593 100644 --- a/packages/server/src/api/routes/tests/table.spec.ts +++ b/packages/server/src/api/routes/tests/table.spec.ts @@ -30,7 +30,7 @@ const ISO_REGEX_PATTERN = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/ describe.each([ ["internal", undefined], [DatabaseName.POSTGRES, getDatasource(DatabaseName.POSTGRES)], - // [DatabaseName.MYSQL, getDatasource(DatabaseName.MYSQL)], + [DatabaseName.MYSQL, getDatasource(DatabaseName.MYSQL)], // [DatabaseName.SQL_SERVER, getDatasource(DatabaseName.SQL_SERVER)], // [DatabaseName.MARIADB, getDatasource(DatabaseName.MARIADB)], ])("/tables (%s)", (_, dsProvider) => { diff --git a/packages/server/src/integrations/base/sqlTable.ts b/packages/server/src/integrations/base/sqlTable.ts index 0feecefb89..9718da0ad2 100644 --- a/packages/server/src/integrations/base/sqlTable.ts +++ b/packages/server/src/integrations/base/sqlTable.ts @@ -224,9 +224,8 @@ class SqlTableQueryBuilder { const tableName = schemaName ? `\`${schemaName}\`.\`${json.table.name}\`` : `\`${json.table.name}\`` - const externalType = json.table.schema[updatedColumn].externalType! return { - sql: `alter table ${tableName} change column \`${json.meta.renamed.old}\` \`${updatedColumn}\` ${externalType};`, + sql: `alter table ${tableName} rename column \`${json.meta.renamed.old}\` to \`${updatedColumn}\``, bindings: [], } } From 7d8cadb47f9c5f40e53724e0ff48eeebb87d07d7 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Fri, 5 Apr 2024 16:55:00 +0100 Subject: [PATCH 05/13] table.spec.ts passing with MariaDB --- packages/server/src/api/routes/tests/table.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/server/src/api/routes/tests/table.spec.ts b/packages/server/src/api/routes/tests/table.spec.ts index 360c76d593..b8528487ec 100644 --- a/packages/server/src/api/routes/tests/table.spec.ts +++ b/packages/server/src/api/routes/tests/table.spec.ts @@ -32,7 +32,7 @@ describe.each([ [DatabaseName.POSTGRES, getDatasource(DatabaseName.POSTGRES)], [DatabaseName.MYSQL, getDatasource(DatabaseName.MYSQL)], // [DatabaseName.SQL_SERVER, getDatasource(DatabaseName.SQL_SERVER)], - // [DatabaseName.MARIADB, getDatasource(DatabaseName.MARIADB)], + [DatabaseName.MARIADB, getDatasource(DatabaseName.MARIADB)], ])("/tables (%s)", (_, dsProvider) => { let isInternal: boolean let datasource: Datasource | undefined From 9da10c790e827cb6b8976ad6cc8d3f3f08c2b513 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Fri, 5 Apr 2024 17:47:55 +0100 Subject: [PATCH 06/13] One failure left for MSSQL --- .../src/api/controllers/table/external.ts | 1 + .../server/src/api/routes/tests/table.spec.ts | 26 ++++++------------- .../server/src/integrations/base/sqlTable.ts | 22 ++++++++++++++++ 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/packages/server/src/api/controllers/table/external.ts b/packages/server/src/api/controllers/table/external.ts index 7c036bec9d..0a6cf58bce 100644 --- a/packages/server/src/api/controllers/table/external.ts +++ b/packages/server/src/api/controllers/table/external.ts @@ -49,6 +49,7 @@ export async function save( builderSocket?.emitDatasourceUpdate(ctx, datasource) return table } catch (err: any) { + throw err if (err instanceof Error) { ctx.throw(400, err.message) } else { diff --git a/packages/server/src/api/routes/tests/table.spec.ts b/packages/server/src/api/routes/tests/table.spec.ts index b8528487ec..8162e08eb6 100644 --- a/packages/server/src/api/routes/tests/table.spec.ts +++ b/packages/server/src/api/routes/tests/table.spec.ts @@ -28,11 +28,11 @@ const { basicTable } = setup.structures const ISO_REGEX_PATTERN = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/ describe.each([ - ["internal", undefined], - [DatabaseName.POSTGRES, getDatasource(DatabaseName.POSTGRES)], - [DatabaseName.MYSQL, getDatasource(DatabaseName.MYSQL)], - // [DatabaseName.SQL_SERVER, getDatasource(DatabaseName.SQL_SERVER)], - [DatabaseName.MARIADB, getDatasource(DatabaseName.MARIADB)], + // ["internal", undefined], + // [DatabaseName.POSTGRES, getDatasource(DatabaseName.POSTGRES)], + // [DatabaseName.MYSQL, getDatasource(DatabaseName.MYSQL)], + [DatabaseName.SQL_SERVER, getDatasource(DatabaseName.SQL_SERVER)], + // [DatabaseName.MARIADB, getDatasource(DatabaseName.MARIADB)], ])("/tables (%s)", (_, dsProvider) => { let isInternal: boolean let datasource: Datasource | undefined @@ -114,7 +114,7 @@ describe.each([ expect(events.table.updated).toHaveBeenCalledWith(updatedTable) }) - it("updates all the row fields for a table when a schema key is renamed", async () => { + it.only("updates all the row fields for a table when a schema key is renamed", async () => { const testTable = await config.api.table.save(basicTable(datasource)) await config.createLegacyView({ name: "TestView", @@ -130,24 +130,14 @@ describe.each([ }) const updatedTable = await config.api.table.save({ - _id: testTable._id, - _rev: testTable._rev, - type: "table", - sourceId: datasource ? datasource._id! : INTERNAL_TABLE_SOURCE_ID, - sourceType: isInternal - ? TableSourceType.INTERNAL - : TableSourceType.EXTERNAL, - name: "TestTable", + ...testTable, _rename: { old: "name", updated: "updatedName", }, - schema: { - updatedName: { type: FieldType.STRING, name: "updatedName" }, - }, }) - expect(updatedTable.name).toEqual("TestTable") + expect(updatedTable.name).toEqual(testTable.name) const res = await config.api.row.get(testTable._id!, testRow._id!) expect(res.updatedName).toEqual("test") diff --git a/packages/server/src/integrations/base/sqlTable.ts b/packages/server/src/integrations/base/sqlTable.ts index 9718da0ad2..32d0c38599 100644 --- a/packages/server/src/integrations/base/sqlTable.ts +++ b/packages/server/src/integrations/base/sqlTable.ts @@ -229,6 +229,7 @@ class SqlTableQueryBuilder { bindings: [], } } + query = buildUpdateTable( client, json.table, @@ -236,6 +237,27 @@ class SqlTableQueryBuilder { json.meta.table, json.meta.renamed! ) + + // renameColumn for SQL Server returns a parameterised `sp_rename` query, + // which is not supported by SQL Server and gives a syntax error. + if (this.sqlClient === SqlClient.MS_SQL && json.meta.renamed) { + const oldColumn = json.meta.renamed.old + const updatedColumn = json.meta.renamed.updated + const tableName = schemaName + ? `${schemaName}.${json.table.name}` + : `${json.table.name}` + const sql = query.toSQL() + if (Array.isArray(sql)) { + for (const query of sql) { + if (query.sql.startsWith("exec sp_rename")) { + query.sql = `exec sp_rename '${tableName}.${oldColumn}', '${updatedColumn}', 'COLUMN'` + query.bindings = [] + } + } + } + + return sql + } break case Operation.DELETE_TABLE: query = buildDeleteTable(client, json.table) From 2876085b61c3bcc1c34fcff9d7931fc10704f3bc Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 5 Apr 2024 18:35:56 +0100 Subject: [PATCH 07/13] Fixing test case to update schema when updating column name. --- packages/server/src/api/routes/tests/table.spec.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/server/src/api/routes/tests/table.spec.ts b/packages/server/src/api/routes/tests/table.spec.ts index 8162e08eb6..2f8f83896b 100644 --- a/packages/server/src/api/routes/tests/table.spec.ts +++ b/packages/server/src/api/routes/tests/table.spec.ts @@ -129,12 +129,20 @@ describe.each([ name: "test", }) + const { name, ...otherColumns } = testTable.schema const updatedTable = await config.api.table.save({ ...testTable, _rename: { old: "name", updated: "updatedName", }, + schema: { + ...otherColumns, + updatedName: { + ...name, + name: "updatedName", + }, + }, }) expect(updatedTable.name).toEqual(testTable.name) From 4347667cb31a568598583e8fe299a8bbd5902272 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Mon, 8 Apr 2024 10:44:08 +0100 Subject: [PATCH 08/13] Remove .only --- packages/server/src/api/routes/tests/table.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/server/src/api/routes/tests/table.spec.ts b/packages/server/src/api/routes/tests/table.spec.ts index 2f8f83896b..d96e76874f 100644 --- a/packages/server/src/api/routes/tests/table.spec.ts +++ b/packages/server/src/api/routes/tests/table.spec.ts @@ -114,7 +114,7 @@ describe.each([ expect(events.table.updated).toHaveBeenCalledWith(updatedTable) }) - it.only("updates all the row fields for a table when a schema key is renamed", async () => { + it("updates all the row fields for a table when a schema key is renamed", async () => { const testTable = await config.api.table.save(basicTable(datasource)) await config.createLegacyView({ name: "TestView", From c30cc06d47dcc5b9e11676662f6d506f936384dc Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Mon, 8 Apr 2024 11:06:43 +0100 Subject: [PATCH 09/13] Fix lint. --- packages/server/src/api/controllers/table/external.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/server/src/api/controllers/table/external.ts b/packages/server/src/api/controllers/table/external.ts index 0a6cf58bce..7c036bec9d 100644 --- a/packages/server/src/api/controllers/table/external.ts +++ b/packages/server/src/api/controllers/table/external.ts @@ -49,7 +49,6 @@ export async function save( builderSocket?.emitDatasourceUpdate(ctx, datasource) return table } catch (err: any) { - throw err if (err instanceof Error) { ctx.throw(400, err.message) } else { From 83c7f08c5a56c81da75e5a2ede5f92443e1bb49d Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Mon, 8 Apr 2024 11:11:06 +0100 Subject: [PATCH 10/13] Fix sql.spec.ts --- packages/server/src/integrations/base/sqlTable.ts | 2 +- packages/server/src/integrations/tests/sql.spec.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/server/src/integrations/base/sqlTable.ts b/packages/server/src/integrations/base/sqlTable.ts index 32d0c38599..80f3864438 100644 --- a/packages/server/src/integrations/base/sqlTable.ts +++ b/packages/server/src/integrations/base/sqlTable.ts @@ -225,7 +225,7 @@ class SqlTableQueryBuilder { ? `\`${schemaName}\`.\`${json.table.name}\`` : `\`${json.table.name}\`` return { - sql: `alter table ${tableName} rename column \`${json.meta.renamed.old}\` to \`${updatedColumn}\``, + sql: `alter table ${tableName} rename column \`${json.meta.renamed.old}\` to \`${updatedColumn}\`;`, bindings: [], } } diff --git a/packages/server/src/integrations/tests/sql.spec.ts b/packages/server/src/integrations/tests/sql.spec.ts index c0b92b3849..dc2a06446b 100644 --- a/packages/server/src/integrations/tests/sql.spec.ts +++ b/packages/server/src/integrations/tests/sql.spec.ts @@ -722,7 +722,7 @@ describe("SQL query builder", () => { }) expect(query).toEqual({ bindings: [], - sql: `alter table \`${TABLE_NAME}\` change column \`name\` \`first_name\` varchar(45);`, + sql: `alter table \`${TABLE_NAME}\` rename column \`name\` to \`first_name\`;`, }) }) From 249e9e59f5f1e1e598dbee4c659688a1bd02b087 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Mon, 8 Apr 2024 12:09:14 +0100 Subject: [PATCH 11/13] Remove extra fields from UIFieldMetadata. --- packages/types/src/documents/app/table/schema.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/types/src/documents/app/table/schema.ts b/packages/types/src/documents/app/table/schema.ts index 42bac7d673..45e39268ac 100644 --- a/packages/types/src/documents/app/table/schema.ts +++ b/packages/types/src/documents/app/table/schema.ts @@ -10,14 +10,10 @@ import { } from "./constants" export interface UIFieldMetadata { - constraints?: FieldConstraints - name?: string - type?: FieldType order?: number width?: number visible?: boolean icon?: string - autocolumn?: boolean } interface BaseRelationshipFieldMetadata From e1da9cbfa447df5641d197fa1177f8ce81d7bdd7 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Mon, 8 Apr 2024 16:11:05 +0100 Subject: [PATCH 12/13] Fix types. --- packages/server/src/api/routes/tests/table.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/server/src/api/routes/tests/table.spec.ts b/packages/server/src/api/routes/tests/table.spec.ts index d96e76874f..71ff673881 100644 --- a/packages/server/src/api/routes/tests/table.spec.ts +++ b/packages/server/src/api/routes/tests/table.spec.ts @@ -13,7 +13,7 @@ import { TableSourceType, User, ViewCalculation, - ViewV2, + ViewV2Enriched, } from "@budibase/types" import { checkBuilderEndpoint } from "./utilities/TestFunctions" import * as setup from "./utilities" @@ -311,7 +311,7 @@ describe.each([ expect(table).toBeDefined() expect(table!.views![viewV2.name]).toBeDefined() - const expectedViewV2: ViewV2 = { + const expectedViewV2: ViewV2Enriched = { ...viewV2, schema: { description: { From 6a657625ae364a273d4b4c44c8b288c89edaf2af Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Mon, 8 Apr 2024 16:46:27 +0100 Subject: [PATCH 13/13] =?UTF-8?q?Uncomment=20tests=20=F0=9F=99=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/server/src/api/routes/tests/table.spec.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/server/src/api/routes/tests/table.spec.ts b/packages/server/src/api/routes/tests/table.spec.ts index 71ff673881..7639b840dc 100644 --- a/packages/server/src/api/routes/tests/table.spec.ts +++ b/packages/server/src/api/routes/tests/table.spec.ts @@ -28,11 +28,11 @@ const { basicTable } = setup.structures const ISO_REGEX_PATTERN = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/ describe.each([ - // ["internal", undefined], - // [DatabaseName.POSTGRES, getDatasource(DatabaseName.POSTGRES)], - // [DatabaseName.MYSQL, getDatasource(DatabaseName.MYSQL)], + ["internal", undefined], + [DatabaseName.POSTGRES, getDatasource(DatabaseName.POSTGRES)], + [DatabaseName.MYSQL, getDatasource(DatabaseName.MYSQL)], [DatabaseName.SQL_SERVER, getDatasource(DatabaseName.SQL_SERVER)], - // [DatabaseName.MARIADB, getDatasource(DatabaseName.MARIADB)], + [DatabaseName.MARIADB, getDatasource(DatabaseName.MARIADB)], ])("/tables (%s)", (_, dsProvider) => { let isInternal: boolean let datasource: Datasource | undefined