Merge branch 'master' into new-table-selected-rows
This commit is contained in:
commit
fe93f2505e
|
@ -84,8 +84,8 @@ export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {
|
|||
}
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
import { context, events } from "@budibase/backend-core"
|
||||
import {
|
||||
AutoFieldSubType,
|
||||
Datasource,
|
||||
FieldSubtype,
|
||||
FieldType,
|
||||
INTERNAL_TABLE_SOURCE_ID,
|
||||
InternalTable,
|
||||
NumberFieldMetadata,
|
||||
RelationshipType,
|
||||
Row,
|
||||
SaveTableRequest,
|
||||
|
@ -13,31 +13,41 @@ import {
|
|||
TableSourceType,
|
||||
User,
|
||||
ViewCalculation,
|
||||
ViewV2Enriched,
|
||||
} 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"
|
||||
import { generator, mocks } from "@budibase/backend-core/tests"
|
||||
import { TableToBuild } from "../../../tests/utilities/TestConfiguration"
|
||||
|
||||
tk.freeze(mocks.date.MOCK_DATE)
|
||||
import { generator } from "@budibase/backend-core/tests"
|
||||
import { DatabaseName, getDatasource } from "../../../integrations/tests/utils"
|
||||
import { tableForDatasource } from "../../../tests/utilities/structures"
|
||||
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$/
|
||||
|
||||
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,102 +55,28 @@ 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."
|
||||
it("creates a table successfully", async () => {
|
||||
const name = generator.guid()
|
||||
const table = await config.api.table.save(
|
||||
tableForDatasource(datasource, { name })
|
||||
)
|
||||
expect(res.body.name).toEqual("TestTable")
|
||||
expect(table.name).toEqual(name)
|
||||
expect(events.table.created).toHaveBeenCalledTimes(1)
|
||||
expect(events.table.created).toHaveBeenCalledWith(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)
|
||||
expect(events.table.created).toHaveBeenCalledWith(table)
|
||||
})
|
||||
|
||||
it("creates a table via data import", async () => {
|
||||
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,21 +91,31 @@ describe("/tables", () => {
|
|||
|
||||
describe("update", () => {
|
||||
it("updates a table", async () => {
|
||||
const testTable = await config.createTable()
|
||||
const table = await config.api.table.save(
|
||||
tableForDatasource(datasource, {
|
||||
schema: {
|
||||
name: {
|
||||
type: FieldType.STRING,
|
||||
name: "name",
|
||||
constraints: {
|
||||
type: "string",
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
)
|
||||
|
||||
const res = await request
|
||||
.post(`/api/tables`)
|
||||
.send(testTable)
|
||||
.set(config.defaultHeaders())
|
||||
.expect("Content-Type", /json/)
|
||||
.expect(200)
|
||||
const updatedTable = await config.api.table.save({
|
||||
...table,
|
||||
name: generator.guid(),
|
||||
})
|
||||
|
||||
expect(events.table.updated).toHaveBeenCalledTimes(1)
|
||||
expect(events.table.updated).toHaveBeenCalledWith(res.body)
|
||||
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",
|
||||
|
@ -179,52 +125,37 @@ describe("/tables", () => {
|
|||
filters: [],
|
||||
})
|
||||
|
||||
const testRow = await request
|
||||
.post(`/api/${testTable._id}/rows`)
|
||||
.send({
|
||||
const testRow = await config.api.row.save(testTable._id!, {
|
||||
name: "test",
|
||||
})
|
||||
.set(config.defaultHeaders())
|
||||
.expect("Content-Type", /json/)
|
||||
.expect(200)
|
||||
|
||||
const updatedTable = await request
|
||||
.post(`/api/tables`)
|
||||
.send({
|
||||
_id: testTable._id,
|
||||
_rev: testTable._rev,
|
||||
name: "TestTable",
|
||||
key: "name",
|
||||
const { name, ...otherColumns } = testTable.schema
|
||||
const updatedTable = await config.api.table.save({
|
||||
...testTable,
|
||||
_rename: {
|
||||
old: "name",
|
||||
updated: "updatedName",
|
||||
},
|
||||
schema: {
|
||||
updatedName: { type: "string" },
|
||||
...otherColumns,
|
||||
updatedName: {
|
||||
...name,
|
||||
name: "updatedName",
|
||||
},
|
||||
},
|
||||
})
|
||||
.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 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.name)
|
||||
|
||||
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",
|
||||
await timekeeper.withFreeze(new Date(2021, 1, 1), async () => {
|
||||
const table = await config.api.table.save(
|
||||
tableForDatasource(datasource, {
|
||||
schema: {
|
||||
autoId: {
|
||||
name: "id",
|
||||
|
@ -237,53 +168,52 @@ describe("/tables", () => {
|
|||
},
|
||||
},
|
||||
},
|
||||
views: {
|
||||
view1: {
|
||||
id: "viewId",
|
||||
version: 2,
|
||||
name: "table view",
|
||||
tableId: "tableId",
|
||||
},
|
||||
},
|
||||
})
|
||||
)
|
||||
|
||||
const newName = generator.guid()
|
||||
|
||||
const updatedTable = await config.api.table.save({
|
||||
...table,
|
||||
name: newName,
|
||||
})
|
||||
|
||||
const response = await request
|
||||
.post(`/api/tables`)
|
||||
.send({
|
||||
...testTable,
|
||||
name: "UpdatedName",
|
||||
})
|
||||
.set(config.defaultHeaders())
|
||||
.expect("Content-Type", /json/)
|
||||
.expect(200)
|
||||
let expected: Table = {
|
||||
...table,
|
||||
name: newName,
|
||||
_id: expect.any(String),
|
||||
}
|
||||
if (isInternal) {
|
||||
expected._rev = expect.stringMatching(/^2-.+/)
|
||||
}
|
||||
|
||||
expect(response.body).toEqual({
|
||||
...testTable,
|
||||
name: "UpdatedName",
|
||||
_rev: expect.stringMatching(/^2-.+/),
|
||||
})
|
||||
expect(updatedTable).toEqual(expected)
|
||||
|
||||
const persistedTable = await config.api.table.get(testTable._id!)
|
||||
expect(persistedTable).toEqual({
|
||||
...testTable,
|
||||
name: "UpdatedName",
|
||||
_rev: expect.stringMatching(/^2-.+/),
|
||||
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)
|
||||
})
|
||||
})
|
||||
|
||||
describe("user table", () => {
|
||||
isInternal &&
|
||||
it("should add roleId and email field when adjusting user table schema", async () => {
|
||||
const res = await request
|
||||
.post(`/api/tables`)
|
||||
.send({
|
||||
...basicTable(),
|
||||
const table = await config.api.table.save({
|
||||
...basicTable(datasource),
|
||||
_id: "ta_users",
|
||||
})
|
||||
.set(config.defaultHeaders())
|
||||
.expect("Content-Type", /json/)
|
||||
.expect(200)
|
||||
expect(res.body.schema.email).toBeDefined()
|
||||
expect(res.body.schema.roleId).toBeDefined()
|
||||
expect(table.schema.email).toBeDefined()
|
||||
expect(table.schema.roleId).toBeDefined()
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -295,12 +225,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 +236,16 @@ 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 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" }],
|
||||
|
@ -327,83 +253,36 @@ 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)
|
||||
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.createTable({
|
||||
name: "TestTable",
|
||||
type: "table",
|
||||
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", () => {
|
||||
let testTable: Table
|
||||
const enrichViewSchemasMock = jest.spyOn(sdk.tables, "enrichViewSchemas")
|
||||
|
||||
beforeEach(async () => {
|
||||
testTable = await config.createTable(testTable)
|
||||
testTable = await config.api.table.save(
|
||||
basicTable(datasource, { name: generator.guid() })
|
||||
)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
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`)
|
||||
.set(config.defaultHeaders())
|
||||
.expect("Content-Type", /json/)
|
||||
.expect(200)
|
||||
|
||||
const table = res.body.find((t: Table) => t._id === testTable._id)
|
||||
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!.name).toEqual(testTable.name)
|
||||
expect(table!.type).toEqual("table")
|
||||
expect(table!.sourceType).toEqual(testTable.sourceType)
|
||||
})
|
||||
|
||||
it("should apply authorization to endpoint", async () => {
|
||||
|
@ -414,99 +293,110 @@ describe("/tables", () => {
|
|||
})
|
||||
})
|
||||
|
||||
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() }),
|
||||
]
|
||||
|
||||
const res = await request
|
||||
.get(`/api/tables`)
|
||||
.set(config.defaultHeaders())
|
||||
.expect("Content-Type", /json/)
|
||||
.expect(200)
|
||||
|
||||
expect(res.body).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", async () => {
|
||||
const viewV2 = await config.api.viewV2.create({
|
||||
tableId: testTable._id!,
|
||||
name: generator.guid(),
|
||||
})
|
||||
|
||||
it("should enrich the view schemas for viewsV2", async () => {
|
||||
const tableId = config.table!._id!
|
||||
enrichViewSchemasMock.mockImplementation(t => ({
|
||||
...t,
|
||||
views: {
|
||||
view1: {
|
||||
version: 2,
|
||||
name: "view1",
|
||||
const legacyView = await config.api.legacyView.save({
|
||||
tableId: testTable._id!,
|
||||
name: generator.guid(),
|
||||
filters: [],
|
||||
schema: {},
|
||||
id: "new_view_id",
|
||||
tableId: t._id!,
|
||||
},
|
||||
},
|
||||
}))
|
||||
|
||||
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,
|
||||
const table = res.find(t => t._id === testTable._id)
|
||||
expect(table).toBeDefined()
|
||||
expect(table!.views![viewV2.name]).toBeDefined()
|
||||
|
||||
const expectedViewV2: ViewV2Enriched = {
|
||||
...viewV2,
|
||||
schema: {
|
||||
description: {
|
||||
constraints: {
|
||||
type: "string",
|
||||
},
|
||||
name: "description",
|
||||
type: FieldType.STRING,
|
||||
visible: false,
|
||||
},
|
||||
name: {
|
||||
constraints: {
|
||||
type: "string",
|
||||
},
|
||||
name: "name",
|
||||
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()
|
||||
expect(table!.views![legacyView.name!]).toEqual({
|
||||
...legacyView,
|
||||
schema: {
|
||||
description: {
|
||||
constraints: {
|
||||
type: "string",
|
||||
},
|
||||
name: "description",
|
||||
type: "string",
|
||||
},
|
||||
name: {
|
||||
constraints: {
|
||||
type: "string",
|
||||
},
|
||||
name: "name",
|
||||
type: "string",
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
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)
|
||||
})
|
||||
})
|
||||
|
||||
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({
|
||||
await config.api.table.save({
|
||||
...table,
|
||||
_id: res.body._id,
|
||||
_rev: res.body._rev,
|
||||
_id: res._id,
|
||||
_rev: res._rev,
|
||||
})
|
||||
.set(config.defaultHeaders())
|
||||
.expect("Content-Type", /json/)
|
||||
.expect(200)
|
||||
// shouldn't have created a new index
|
||||
expect((await db.getIndexes()).total_rows).toEqual(indexCount + 1)
|
||||
})
|
||||
|
@ -521,12 +411,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 +446,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()
|
||||
})
|
||||
|
@ -816,11 +700,8 @@ describe("/tables", () => {
|
|||
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,
|
||||
table = await config.api.table.save(
|
||||
tableForDatasource(datasource, {
|
||||
schema: {
|
||||
"user relationship": {
|
||||
type: FieldType.LINK,
|
||||
|
@ -843,6 +724,7 @@ describe("/tables", () => {
|
|||
},
|
||||
},
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should fail if the new column name is blank", async () => {
|
||||
|
|
|
@ -224,12 +224,12 @@ 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: [],
|
||||
}
|
||||
}
|
||||
|
||||
query = buildUpdateTable(
|
||||
client,
|
||||
json.table,
|
||||
|
@ -237,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)
|
||||
|
|
|
@ -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\`;`,
|
||||
})
|
||||
})
|
||||
|
||||
|
|
|
@ -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 }
|
||||
}
|
||||
|
||||
|
|
|
@ -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<BulkImportResponse> => {
|
||||
return await this._post<BulkImportResponse>(
|
||||
`/api/tables/${tableId}/import`,
|
||||
{
|
||||
body: data,
|
||||
expectations,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
destroy = async (
|
||||
tableId: string,
|
||||
revId: string,
|
||||
expectations?: Expectations
|
||||
): Promise<void> => {
|
||||
return await this._delete<void>(`/api/tables/${tableId}/${revId}`, {
|
||||
expectations,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,15 +26,37 @@ import {
|
|||
WebhookActionType,
|
||||
} 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
|
||||
|
||||
export function basicTable(): Table {
|
||||
return {
|
||||
name: "TestTable",
|
||||
export function tableForDatasource(
|
||||
datasource?: Datasource,
|
||||
...extra: Partial<Table>[]
|
||||
): Table {
|
||||
return merge(
|
||||
{
|
||||
name: generator.guid(),
|
||||
type: "table",
|
||||
sourceId: INTERNAL_TABLE_SOURCE_ID,
|
||||
sourceType: TableSourceType.INTERNAL,
|
||||
sourceType: datasource
|
||||
? TableSourceType.EXTERNAL
|
||||
: TableSourceType.INTERNAL,
|
||||
sourceId: datasource ? datasource._id! : INTERNAL_TABLE_SOURCE_ID,
|
||||
schema: {},
|
||||
},
|
||||
...extra
|
||||
)
|
||||
}
|
||||
|
||||
export function basicTable(
|
||||
datasource?: Datasource,
|
||||
...extra: Partial<Table>[]
|
||||
): Table {
|
||||
return tableForDatasource(
|
||||
datasource,
|
||||
{
|
||||
name: "TestTable",
|
||||
schema: {
|
||||
name: {
|
||||
type: FieldType.STRING,
|
||||
|
@ -51,7 +73,9 @@ export function basicTable(): Table {
|
|||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
...extra
|
||||
)
|
||||
}
|
||||
|
||||
export function basicView(tableId: string) {
|
||||
|
|
Loading…
Reference in New Issue