Skip to content
This repository has been archived by the owner on Jul 14, 2023. It is now read-only.

fix: support deep update for one to many composition #281

Merged
merged 2 commits into from
Mar 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions __tests__/__assets__/cap-proj/rest-client-test/beers.http
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,18 @@ Content-Type: application/json
{
"name": "Testbier with PUT"
}

### Deep Update
PUT https://localhost:4004/beershop/Breweries/4aeebbed-90c2-4bdd-aa70-d8eecb8eaebb
Content-Type: application/json

{
"name": "Rittmayer Hallerndorfz",
"beers": [
{
"name": "Weissen",
"ibu": 55,
"abv": 5.2
}
]
}
8 changes: 8 additions & 0 deletions __tests__/lib/pg/ql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ describe('QL to PostgreSQL', () => {
beforeEach(async () => {
await deploy(this._model, {}).to(this._dbProperties)
})

test('-> by using entries', async () => {
const { Beers } = cds.entities('csw')

Expand Down Expand Up @@ -187,12 +188,19 @@ describe('QL to PostgreSQL', () => {
beforeEach(async () => {
await deploy(this._model, {}).to(this._dbProperties)
})

test('-> Get affected rows ', async () => {
const { Beers } = cds.entities('csw')
const affectedRows = await cds.run(
UPDATE(Beers).set({ name: 'TEST' }).where({ ID: '9e1704e3-6fd0-4a5d-bfb1-13ac47f7976b' })
)
expect(affectedRows).toStrictEqual(1)
})

test('-> multiple rows', async () => {
const { Beers } = cds.entities('csw')
const affectedRows = await cds.run(UPDATE(Beers).set({ abv: 1.0 }))
expect(affectedRows).toStrictEqual(11)
})
})
})
32 changes: 31 additions & 1 deletion __tests__/lib/pg/service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ describe.each(suiteEnvironments)(
})
})
describe('odata: PATCH -> DEEP UPDATE', () => {
test.skip('odata: deep update Brewery and beers -> sql: deep update into Breweries', async () => {
test('odata: deep update Brewery and beers -> sql: deep update into Breweries', async () => {
const response = await request
.patch('/beershop/Breweries/4aeebbed-90c2-4bdd-aa70-d8eecb8eaebb')
.send({
Expand All @@ -452,6 +452,36 @@ describe.each(suiteEnvironments)(
})
.set('content-type', 'application/json;charset=UTF-8;IEEE754Compatible=true')
expect(response.status).toStrictEqual(200)
// deep update deletes the other beers from Rittmayer Hallerndorf - they have to be restored, otherwise the user-defined-schema test fails
const restoreReponse = await request
.patch('/beershop/Breweries/4aeebbed-90c2-4bdd-aa70-d8eecb8eaebb')
.send({
name: 'Rittmayer Hallerndorf',
beers: [
{
name: 'Hallerndorfer Landbier Hell',
abv: 4.9,
ibu: 0
},
{
name: 'Hallerndorfer Hausbrauerbier',
abv: 5,
ibu: 0
},
{
name: 'Bitter 42',
abv: 5.5,
ibu: 42
},
{
name: 'Summer 69',
abv: 5.9,
ibu: 12
}
]
})
.set('content-type', 'application/json;charset=UTF-8;IEEE754Compatible=true')
expect(restoreReponse.status).toStrictEqual(200)
})
})
}
Expand Down
3 changes: 3 additions & 0 deletions lib/pg/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const DEBUG = cds.debug('cds-pg')
*/
const executeGenericCQN = (model, dbc, query, user, locale, txTimestamp) => {
const { sql, values = [] } = _cqnToSQL(model, query, user, locale, txTimestamp)
if (/^\s*insert/i.test(sql)) {
return executeInsertCQN(model, dbc, query, user, locale, txTimestamp)
}
const isOne = query.SELECT && query.SELECT.one
const postPropertyMapper = getPostProcessMapper(PG_TYPE_CONVERSION_MAP, model, query)
return _executeSQLReturningRows(dbc, sql, values, isOne, postPropertyMapper)
Expand Down