Skip to content

Commit

Permalink
wip: nullability for unmanaged paths
Browse files Browse the repository at this point in the history
  • Loading branch information
patricebender committed Feb 12, 2025
1 parent a175c52 commit 7ebae5e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
7 changes: 6 additions & 1 deletion db-service/lib/assert-constraint.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ function attachConstraints(_results, req) {
const xpr = []
// if the element is nullable, we prepend xpr with `<element> IS NULL OR …`
if (!element.notNull && !element.on) {
// if(element.on) --> REVISIT: HANA doesnt like this, what can we do to ensure nullability?
// xpr.unshift({ ...coalesce(condition.xpr) }, 'or')
xpr.unshift({ ref: [element.name] }, 'is', 'null', 'or')
}
xpr.push({ xpr: condition.xpr })
return {
// case … when … needed for hana compatibility
// REVISIT: can we move workaround to HANAService only?
xpr: ['case', 'when', { xpr }, 'then', { val: true }, 'else', { val: false }, 'end'],
xpr: wrapInCaseWhen(xpr),
as: name,
cast: {
type: 'cds.Boolean',
Expand Down Expand Up @@ -139,6 +141,9 @@ function attachConstraints(_results, req) {
function wrapInCaseWhen(xpr) {
return ['case', 'when', { xpr }, 'then', { val: true }, 'else', { val: false }, 'end']
}
function coalesce(xpr) {

Check warning on line 144 in db-service/lib/assert-constraint.js

View workflow job for this annotation

GitHub Actions / Tests (22)

'coalesce' is defined but never used
return { func: 'coalesce', args: [{ xpr }, { val: true }] }
}
}

async function checkConstraints(req) {
Expand Down
10 changes: 2 additions & 8 deletions test/bookshop/db/schema.cds
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,8 @@ entity Books : managed {
message: 'The stock must be greater than or equal to 0',
parameters: [] // to be inserted into the message
}
// @assert.constraint : {
// condition: ( stock <= price ),
// message: 'The stock must be less than or equal to price',
// parameters: [] // to be inserted into the message
// }
stock : Integer;
price : Decimal;
// one of the tests inserts a very big decimal which
// collides with our constraint above :D
dummyDecimal : Decimal;
currency : Currency;
image : LargeBinary @Core.MediaType: 'image/png';
Expand Down Expand Up @@ -55,9 +48,10 @@ entity Authors : managed {
/** Hierarchically organized Code List for Genres */
entity Genres : sap.common.CodeList {
key ID : Integer;
@assert.constraint: ( parent.name is not null)
parent : Association to Genres;
// make sure only our pre-defined genres are allowed
@assert.constraint: ( children.name in (
@assert.constraint: ( ( children.ID is null ) or children.name in (
'Fiction', 'Drama', 'Poetry', 'Fantasy', 'Science Fiction',
'Romance', 'Mystery', 'Thriller', 'Dystopia', 'Fairy Tale',
'Non-Fiction', 'Biography', 'Autobiography', 'Essay', 'Speech',
Expand Down
9 changes: 9 additions & 0 deletions test/scenarios/bookshop/assert-constraint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,14 @@ describe('Bookshop - assertions', () => {
// both should have been created
expect(genres).to.have.length(2)
})

test('genre without children works', async () => {
await POST('admin/Genres', {
ID: 102,
name: 'Genre without children',
}, { auth: { username: 'alice' } })
const genre = await SELECT.one.from(Genres).where({ ID: 102 })
expect(genre).to.exist
})
})
})

0 comments on commit 7ebae5e

Please sign in to comment.