Merge branch 'master' into fix/add-cron-validation

This commit is contained in:
Michael Drury 2024-02-12 13:27:14 +00:00 committed by GitHub
commit dfe9b61812
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 162 additions and 23 deletions

View File

@ -14,7 +14,6 @@ import { events } from "@budibase/backend-core"
import {
BulkImportRequest,
BulkImportResponse,
DocumentType,
FetchTablesResponse,
MigrateRequest,
MigrateResponse,
@ -25,7 +24,6 @@ import {
TableResponse,
TableSourceType,
UserCtx,
SEPARATOR,
} from "@budibase/types"
import sdk from "../../../sdk"
import { jsonFromCsvString } from "../../../utilities/csv"
@ -77,9 +75,10 @@ export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {
const table = ctx.request.body
const isImport = table.rows
const savedTable = await pickApi({ table }).save(ctx)
let savedTable = await pickApi({ table }).save(ctx)
if (!table._id) {
await events.table.created(savedTable)
savedTable = sdk.tables.enrichViewSchemas(savedTable)
} else {
await events.table.updated(savedTable)
}

View File

@ -19,10 +19,15 @@ export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {
} = {
_id: generateTableID(),
...rest,
type: "table",
sourceType: TableSourceType.INTERNAL,
views: {},
// Ensure these fields are populated, even if not sent in the request
type: rest.type || "table",
sourceType: rest.sourceType || TableSourceType.INTERNAL,
}
if (!tableToSave.views) {
tableToSave.views = {}
}
const renaming = tableToSave._rename
delete tableToSave._rename

View File

@ -5,6 +5,7 @@ import {
FieldType,
INTERNAL_TABLE_SOURCE_ID,
InternalTable,
NumberFieldMetadata,
RelationshipType,
Row,
SaveTableRequest,
@ -18,6 +19,12 @@ import * as setup from "./utilities"
import sdk from "../../../sdk"
import * as uuid from "uuid"
import tk from "timekeeper"
import { mocks } from "@budibase/backend-core/tests"
import { TableToBuild } from "../../../tests/utilities/TestConfiguration"
tk.freeze(mocks.date.MOCK_DATE)
const { basicTable } = setup.structures
describe("/tables", () => {
@ -60,6 +67,67 @@ describe("/tables", () => {
expect(events.table.created).toBeCalledWith(res.body)
})
it("creates all the passed fields", async () => {
const tableData: TableToBuild = {
name: "TestTable",
type: "table",
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.createTable(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,
},
},
},
sourceType: TableSourceType.INTERNAL,
sourceId: expect.any(String),
_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" }]
@ -152,6 +220,56 @@ describe("/tables", () => {
expect(res.body.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,
},
},
},
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,
name: "UpdatedName",
_rev: expect.stringMatching(/^2-.+/),
})
const persistedTable = await config.api.table.get(testTable._id!)
expect(persistedTable).toEqual({
...testTable,
name: "UpdatedName",
_rev: expect.stringMatching(/^2-.+/),
})
})
describe("user table", () => {
it("should add roleId and email field when adjusting user table schema", async () => {
const res = await request
@ -230,6 +348,7 @@ describe("/tables", () => {
describe("fetch", () => {
let testTable: Table
const enrichViewSchemasMock = jest.spyOn(sdk.tables, "enrichViewSchemas")
beforeEach(async () => {
testTable = await config.createTable(testTable)
@ -239,6 +358,10 @@ describe("/tables", () => {
delete testTable._rev
})
afterAll(() => {
enrichViewSchemasMock.mockRestore()
})
it("returns all the tables for that instance in the response body", async () => {
const res = await request
.get(`/api/tables`)
@ -287,7 +410,7 @@ describe("/tables", () => {
it("should enrich the view schemas for viewsV2", async () => {
const tableId = config.table!._id!
jest.spyOn(sdk.tables, "enrichViewSchemas").mockImplementation(t => ({
enrichViewSchemasMock.mockImplementation(t => ({
...t,
views: {
view1: {
@ -295,7 +418,7 @@ describe("/tables", () => {
name: "view1",
schema: {},
id: "new_view_id",
tableId,
tableId: t._id!,
},
},
}))
@ -362,11 +485,7 @@ describe("/tables", () => {
let testTable: Table
beforeEach(async () => {
testTable = await config.createTable(testTable)
})
afterEach(() => {
delete testTable._rev
testTable = await config.createTable()
})
it("returns a success response when a table is deleted.", async () => {

View File

@ -97,6 +97,7 @@ const environment = {
APP_MIGRATION_TIMEOUT: parseIntSafe(process.env.APP_MIGRATION_TIMEOUT),
JS_RUNNER_MEMORY_LIMIT:
parseIntSafe(process.env.JS_RUNNER_MEMORY_LIMIT) || 64,
LOG_JS_ERRORS: process.env.LOG_JS_ERRORS,
}
// threading can cause memory issues with node-ts in development

View File

@ -1,7 +1,7 @@
import vm from "vm"
import env from "../environment"
import { setJSRunner } from "@budibase/string-templates"
import { context, timers } from "@budibase/backend-core"
import { setJSRunner, setOnErrorLog } from "@budibase/string-templates"
import { context, logging, timers } from "@budibase/backend-core"
import tracer from "dd-trace"
type TrackerFn = <T>(f: () => T) => T
@ -58,4 +58,10 @@ export function init() {
)
})
})
if (env.LOG_JS_ERRORS) {
setOnErrorLog((error: Error) => {
logging.logWarn(JSON.stringify(error))
})
}
}

View File

@ -75,11 +75,13 @@ export async function save(
if (!tableView) continue
if (viewsSdk.isV2(tableView)) {
table.views[view] = viewsSdk.syncSchema(
oldTable!.views![view] as ViewV2,
table.schema,
renaming
)
if (oldTable?.views && oldTable.views[view]) {
table.views[view] = viewsSdk.syncSchema(
oldTable.views[view] as ViewV2,
table.schema,
renaming
)
}
continue
}

View File

@ -84,12 +84,12 @@ type DefaultUserValues = {
csrfToken: string
}
interface TableToBuild extends Omit<Table, "sourceId" | "sourceType"> {
export interface TableToBuild extends Omit<Table, "sourceId" | "sourceType"> {
sourceId?: string
sourceType?: TableSourceType
}
class TestConfiguration {
export default class TestConfiguration {
server: any
request: supertest.SuperTest<supertest.Test> | undefined
started: boolean
@ -912,4 +912,4 @@ class TestConfiguration {
}
}
export = TestConfiguration
module.exports = TestConfiguration

View File

@ -8,6 +8,9 @@ const { getJsHelperList } = require("./list")
let runJS
module.exports.setJSRunner = runner => (runJS = runner)
let onErrorLog
module.exports.setOnErrorLog = delegate => (onErrorLog = delegate)
// Helper utility to strip square brackets from a value
const removeSquareBrackets = value => {
if (!value || typeof value !== "string") {
@ -56,6 +59,8 @@ module.exports.processJS = (handlebars, context) => {
const res = { data: runJS(js, sandboxContext) }
return `{{${LITERAL_MARKER} js_result-${JSON.stringify(res)}}}`
} catch (error) {
onErrorLog && onErrorLog(error)
if (error.code === "ERR_SCRIPT_EXECUTION_TIMEOUT") {
return "Timed out while executing JS"
}

View File

@ -365,6 +365,7 @@ module.exports.doesContainString = (template, string) => {
}
module.exports.setJSRunner = javascript.setJSRunner
module.exports.setOnErrorLog = javascript.setOnErrorLog
module.exports.convertToJS = hbs => {
const blocks = exports.findHBSBlocks(hbs)

View File

@ -20,6 +20,7 @@ export const disableEscaping = templates.disableEscaping
export const findHBSBlocks = templates.findHBSBlocks
export const convertToJS = templates.convertToJS
export const setJSRunner = templates.setJSRunner
export const setOnErrorLog = templates.setOnErrorLog
export const FIND_ANY_HBS_REGEX = templates.FIND_ANY_HBS_REGEX
export const helpersToRemoveForJs = templates.helpersToRemoveForJs