Merge pull request #10593 from Budibase/fix/autoId-sync-on-create-row

Fix for autoid issue on create row
This commit is contained in:
deanhannigan 2023-05-16 12:55:22 +01:00 committed by GitHub
commit 73d5be0a7c
2 changed files with 64 additions and 2 deletions

View File

@ -118,8 +118,11 @@ export async function patch(ctx: UserCtx) {
combinedRow[key] = inputs[key] combinedRow[key] = inputs[key]
} }
// need to copy the table so it can be differenced on way out
const tableClone = cloneDeep(dbTable)
// this returns the table and row incase they have been updated // this returns the table and row incase they have been updated
let { table, row } = inputProcessing(ctx.user, dbTable, combinedRow) let { table, row } = inputProcessing(ctx.user, tableClone, combinedRow)
const validateResult = await utils.validate({ const validateResult = await utils.validate({
row, row,
table, table,
@ -163,7 +166,12 @@ export async function save(ctx: UserCtx) {
// this returns the table and row incase they have been updated // this returns the table and row incase they have been updated
const dbTable = await db.get(inputs.tableId) const dbTable = await db.get(inputs.tableId)
let { table, row } = inputProcessing(ctx.user, dbTable, inputs)
// need to copy the table so it can be differenced on way out
const tableClone = cloneDeep(dbTable)
let { table, row } = inputProcessing(ctx.user, tableClone, inputs)
const validateResult = await utils.validate({ const validateResult = await utils.validate({
row, row,
table, table,

View File

@ -79,6 +79,60 @@ describe("/rows", () => {
await assertQueryUsage(queryUsage + 1) await assertQueryUsage(queryUsage + 1)
}) })
it("Increment row autoId per create row request", async () => {
const rowUsage = await getRowUsage()
const queryUsage = await getQueryUsage()
const newTable = await config.createTable({
name: "TestTableAuto",
type: "table",
key: "name",
schema: {
...table.schema,
"Row ID": {
name: "Row ID",
type: "number",
subtype: "autoID",
icon: "ri-magic-line",
autocolumn: true,
constraints: {
type: "number",
presence: false,
numericality: {
greaterThanOrEqualTo: "",
lessThanOrEqualTo: "",
},
},
},
}
})
const ids = [1,2,3]
// Performing several create row requests should increment the autoID fields accordingly
const createRow = async (id) => {
const res = await request
.post(`/api/${newTable._id}/rows`)
.send({
name: "row_" + id
})
.set(config.defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
expect(res.res.statusMessage).toEqual(`${newTable.name} saved successfully`)
expect(res.body.name).toEqual("row_" + id)
expect(res.body._rev).toBeDefined()
expect(res.body["Row ID"]).toEqual(id)
}
for (let i=0; i<ids.length; i++ ){
await createRow(ids[i])
}
await assertRowUsage(rowUsage + ids.length)
await assertQueryUsage(queryUsage + ids.length)
})
it("updates a row successfully", async () => { it("updates a row successfully", async () => {
const existing = await config.createRow() const existing = await config.createRow()
const rowUsage = await getRowUsage() const rowUsage = await getRowUsage()