Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip(POC): @assert.constraint #988

Draft
wants to merge 58 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
4bdde0d
wip: assert constraint poc
patricebender Jan 20, 2025
665e38d
wip: need to rollback transaction
patricebender Jan 21, 2025
dfc4a1b
rollback the tx
patricebender Jan 21, 2025
6c2528c
add insert test
patricebender Jan 21, 2025
6fe6973
batch handling for inserts
patricebender Jan 21, 2025
ebf8ae2
add removed file
patricebender Jan 21, 2025
b4df5e0
wip: aggregations
patricebender Jan 21, 2025
67988fc
validate an aggregation
patricebender Jan 23, 2025
a738b9d
Merge remote-tracking branch 'origin/main' into patrice/assert-constr…
patricebender Jan 23, 2025
f334fe6
no stock is okay
patricebender Jan 23, 2025
c133b03
dummy decimal for our POC
patricebender Jan 23, 2025
46c1a91
wip: deferred constraints test
patricebender Jan 30, 2025
db00424
proper test expectations
patricebender Jan 30, 2025
2254c3c
get test right
patricebender Jan 30, 2025
6756f60
wip: defer constraints
patricebender Jan 30, 2025
73c3dc8
defer constraint check to before commit
patricebender Jan 30, 2025
3a8b939
remove todo
patricebender Jan 30, 2025
b208fbd
Merge branch 'main' into patrice/assert-constraint
patricebender Jan 31, 2025
d640b19
try extra parens
patricebender Jan 31, 2025
5c1064b
workaround for hana
patricebender Jan 31, 2025
9752e1d
add price to a view
patricebender Jan 31, 2025
ea39c19
only needed for local testing
patricebender Jan 31, 2025
c27006f
Merge remote-tracking branch 'origin/main' into patrice/assert-constr…
patricebender Feb 12, 2025
fee4ddb
some comments
patricebender Feb 12, 2025
ca61be6
for now, no aggregation validation
patricebender Feb 12, 2025
72b6a31
focus on element level constraints
patricebender Feb 12, 2025
dc02461
correct wrong operator
patricebender Feb 12, 2025
2977e67
constraint shortcut / constraint via path
patricebender Feb 12, 2025
b9550ce
small code improvements
patricebender Feb 12, 2025
a175c52
no need to add `is null` check for unmanaged
patricebender Feb 12, 2025
7ebae5e
wip: nullability for unmanaged paths
patricebender Feb 12, 2025
65fd66c
constraint killed some tests
patricebender Feb 13, 2025
d9dfc02
add todo
patricebender Feb 14, 2025
9e3ebca
messages and parameters end to end
patricebender Feb 14, 2025
83d19ba
some more messages
patricebender Feb 14, 2025
8ca290e
allow longer genre names
patricebender Feb 14, 2025
48c6d2d
fix days_between function mapping for now
patricebender Feb 14, 2025
5ee719e
workaround
patricebender Feb 14, 2025
e576ec6
Merge remote-tracking branch 'origin/main' into patrice/assert-constr…
patricebender Feb 19, 2025
f55839f
Merge branch 'main' into patrice/assert-constraint
patricebender Mar 14, 2025
f36c922
use array for params
patricebender Mar 14, 2025
a5444e4
Merge remote-tracking branch 'origin/main' into patrice/assert-constr…
patricebender Mar 21, 2025
fd2ad86
for now no nullabiltiy 4 unmanagedf
patricebender Mar 21, 2025
ff01717
goerlsche negation
patricebender Mar 27, 2025
aa3d382
Merge remote-tracking branch 'origin/main' into patrice/assert-constr…
patricebender Mar 31, 2025
5b03c22
rm unused func
patricebender Apr 1, 2025
1f7aa5f
Merge remote-tracking branch 'origin/main' into patrice/assert-constr…
patricebender Apr 8, 2025
e626ad8
Merge remote-tracking branch 'origin/main' into patrice/assert-constr…
patricebender Apr 9, 2025
9cc9072
also collect constraints from entity
patricebender Apr 9, 2025
db13769
wip: deep insert
patricebender Apr 9, 2025
4a075ab
first working version for deep INSERTs
patricebender Apr 10, 2025
b13bda9
some more sophisticated tests
patricebender Apr 10, 2025
65563a0
more tests
patricebender Apr 10, 2025
ca5fbed
always return acc
patricebender Apr 10, 2025
84d4c73
some more resilience
patricebender Apr 10, 2025
a084e8a
some chores
patricebender Apr 10, 2025
1683345
run queries at once
patricebender Apr 10, 2025
0221547
be prepared for keyless entities
patricebender Apr 10, 2025
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
3 changes: 3 additions & 0 deletions db-service/lib/SQLService.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { pipeline } = require('stream/promises')
const { resolveView, getDBTable, getTransition } = require('@sap/cds/libx/_runtime/common/utils/resolveView')
const DatabaseService = require('./common/DatabaseService')
const cqn4sql = require('./cqn4sql')
const {attachConstraints, checkConstraints} = require('./assert-constraint')

const BINARY_TYPES = {
'cds.Binary': 1,
Expand All @@ -23,6 +24,7 @@ const BINARY_TYPES = {
class SQLService extends DatabaseService {
init() {
this.on(['INSERT', 'UPSERT', 'UPDATE'], require('./fill-in-keys')) // REVISIT should be replaced by correct input processing eventually
this.after(['INSERT', 'UPSERT', 'UPDATE'], attachConstraints)
this.on(['INSERT', 'UPSERT', 'UPDATE'], require('./deep-queries').onDeep)
if (cds.env.features.db_strict) {
this.before(['INSERT', 'UPSERT', 'UPDATE'], ({ query }) => {
Expand Down Expand Up @@ -53,6 +55,7 @@ class SQLService extends DatabaseService {
this.on(['DELETE'], this.onDELETE)
this.on(['CREATE ENTITY', 'DROP ENTITY'], this.onSIMPLE)
this.on(['BEGIN', 'COMMIT', 'ROLLBACK'], this.onEVENT)
this.before(['COMMIT'], checkConstraints)
this.on(['*'], this.onPlainSQL)
return super.init()
}
Expand Down
262 changes: 262 additions & 0 deletions db-service/lib/assert-constraint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
'use strict'

const cds = require('@sap/cds')

function attachConstraints(_results, req) {
if (!req.target || !this.model || req.target._unresolved) return
const { data } = req
if (Array.isArray(data[0])) return // REVISIT: what about csv inserts?
const constraintsPerTarget = {}
for (const [cName, c] of Object.entries(collectConstraints(req.target, req.data))) {
if (c.target.name in constraintsPerTarget) {
constraintsPerTarget[c.target.name][cName] = c
} else {
constraintsPerTarget[c.target.name] = { [cName]: c }
}
}
if (Object.keys(constraintsPerTarget).length === 0) return

// for UPDATE/UPSERT there is probably an additional where clause
let where = []
if (req.event === 'UPDATE' || req.event === 'UPSERT') {
const prop = req.event
if (req.query[prop]?.where) {
where = req.query[prop].where
} else if (req.query[prop]?.entity?.ref[0]?.where) {
where = req.query[prop].entity.ref[0].where
}
}

const validationQueries = []
for (const [targetName, constraints] of Object.entries(constraintsPerTarget)) {
const validationQuery = _getValidationQuery(targetName, constraints)
if (where.length > 0) {
if (validationQuery.SELECT.where.length > 0) validationQuery.SELECT.where.push('or', { xpr: where })
else validationQuery.SELECT.where.push(...where)
}
validationQueries.push(validationQuery)
}

if (this.tx.assert_constraints) this.tx.assert_constraints.push(validationQueries)
else this.tx.assert_constraints = [validationQueries]
return
function _getValidationQuery(target, constraints) {
const validationQuery = SELECT.from(target)
// each column represents a constraint
const columns = Object.keys(constraints).flatMap(name => {
const constraint = constraints[name]
const { condition, parameters } = constraint
const xpr = []
xpr.push({ xpr: condition.xpr })
const colsForConstraint = [
{
xpr: wrapInCaseWhen(xpr),
// avoid naming ambiguities for anonymous constraints,
// where the element itself is part of the msg params
as: name + '_constraint',
cast: {
type: 'cds.Boolean',
},
},
]
if (parameters) {
if (parameters.list) parameters.list.forEach(p => colsForConstraint.push(p))
else if (parameters.ref) colsForConstraint.push(parameters.ref)
else if (parameters.length) parameters.forEach(p => colsForConstraint.push({ ref: [p['=']] }))
}
return colsForConstraint
})

validationQuery.SELECT.columns = columns
// REVISIT: matchKeys for one entity should be the same for all constraints
// it should be more like { 'bookshop.Books' : { c1 : { ... }, c2: { ... } }, …, $matchKeys: [ ... ] }
const keyMatchingConditions = Object.values(constraints)[0].matchKeys
validationQuery.SELECT.where = keyMatchingConditions.flatMap((matchKey, i) =>
i > 0 ? ['or', ...matchKey] : matchKey,
)
Object.defineProperty(validationQuery, '$constraints', { value: constraints })
return validationQuery
}

/**
* Collects constraints for a request target and its elements.
*
*
* @param {CSN.entity} entity the target of the request (or a recursive child)
* @param {object} data the payload
* @returns {object} constraints
*/
function collectConstraints(entity, data) {
// Collect constraints defined on the entity itself.
let constraints = { ...getConstraintsFrom(entity) };

// Merge constraints from each element of the entity.
for (const element of Object.values(entity.elements)) {
const elementConstraints = getConstraintsFrom(element, entity);
Object.assign(constraints, elementConstraints);
}

// attach IDs derived from the payload.
const matchKeyConditions = matchKeys(entity, data);
for (const constraint of Object.values(constraints)) {
if (constraint.matchKeys) {
constraint.matchKeys.push(...matchKeyConditions);
} else {
// copying the array to avoid potential mutation issues.
constraint.matchKeys = [...matchKeyConditions];
}
}

// process compositions defined in the entity, if they are part of the payload.
const compositions = entity.compositions || {};
for (const compKey of Object.keys(compositions)) {
if (data[compKey]) {
const composition = compositions[compKey];
const compositionTarget = cds.model.definitions[composition.target];
const childrenData = data[compKey];
if (Array.isArray(childrenData)) {
for (const childData of childrenData) {
const childConstraints = collectConstraints(compositionTarget, childData);
mergeConstraints(constraints, childConstraints);
}
} else {
const childConstraints = collectConstraints(compositionTarget, childrenData);
mergeConstraints(constraints, childConstraints);
}
}
}
return constraints;

function mergeConstraints(baseConstraints, newConstraints) {
if (!newConstraints) return baseConstraints;
for (const key in newConstraints) {
const newConstraint = newConstraints[key];
// if the same element has constrains already, merge the match keys.
if (baseConstraints[key]?.element === newConstraint.element) {
baseConstraints[key].matchKeys.push(...newConstraint.matchKeys);
} else {
baseConstraints[key] = newConstraint;
}
}
return baseConstraints;
}

// Retrieve constraints from an entity or element.
function getConstraintsFrom(obj, target = null) {
const elmConstraints = {};

for (const key in obj) {
if (key.startsWith('@assert.constraint')) {
// Remove the fixed prefix.
let remainder = key.slice('@assert.constraint'.length);
// Remove a leading dot, if any.
if (remainder.startsWith('.')) {
remainder = remainder.slice(1);
}

// Split into constraint name and property parts.
const parts = remainder.split('.');
let constraintName, propertyName;
if (parts.length === 1) {
// No explicit name: use the object's name as the constraint name.
constraintName = obj.name;
if (!remainder.length) {
// When no extra information is given, check if an 'xpr' exists.
if (!obj['@assert.constraint'].xpr) continue;
// Otherwise, use the shorthand property 'condition'.
propertyName = 'condition';
} else {
propertyName = parts[0];
}
} else {
// First part is the constraint name; the rest form the property name.
constraintName = parts[0];
propertyName = parts.slice(1).join('.');
}

// Create or extend the constraint entry.
if (!elmConstraints[constraintName]) {
elmConstraints[constraintName] = {};
}
elmConstraints[constraintName][propertyName] = obj[key];
}
}

// Attach additional metadata to each constraint.
Object.keys(elmConstraints).forEach(name => {
elmConstraints[name].element = obj;
// The constraint target defaults to the current object unless overridden.
elmConstraints[name].target = target || obj;
});
return elmConstraints;
}
}

/**
* Constructs a condition which matches the primary keys of the entity for the given data.
*
* @param {CSN.entity} entity
* @param {object} data
* @returns {Array} conditions
*/
function matchKeys(entity, data) {
const primaryKeys = Object.keys(entity.keys || {})
const dataEntries = Array.isArray(data) ? data : [data] // Ensure batch handling

// construct {key:value} pairs holding information about the entry to check
return dataEntries
.map(entry =>
primaryKeys.reduce((identifier, key, i) => {

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

View workflow job for this annotation

GitHub Actions / Tests (22)

'i' is defined but never used
const value = entry?.[key]
if (identifier.length > 0) identifier.push('and')
else if (value !== undefined) identifier.push({ ref: [key] }, '=', { val: value })
return identifier
}, []),
)
.filter(Boolean)
}

function wrapInCaseWhen(xpr) {
return ['case', 'when', 'not', { xpr }, 'then', { val: false }, 'else', { val: true }, 'end']
}
}

async function checkConstraints(req) {
if (this.tx.assert_constraints) {
for (const validationQueries of this.tx.assert_constraints) {
// REVISIT: cds.run fails for some tests
const results = await this.run(validationQueries)
let i = 0
for (const result of results) {
const constraints = validationQueries[i].$constraints
if (!result?.length) continue
for (const key in constraints) {
const constraintCol = key + '_constraint'
for (const row of result) {
if (!row[constraintCol]) {
const { message, parameters } = constraints[key]
const msgParams = {}
if (parameters) {
Object.keys(row)
.filter(alias => alias !== constraintCol)
.forEach(alias => (msgParams[alias] = row[alias]))
}
const constraintValidationMessage = message
? cds.i18n.messages.for(message, msgParams) || message
: `@assert.constraint ”${key}” failed`
req.error(400, constraintValidationMessage)
}
}
}
i+=1
}
}
// REVISIT: we can probably get rid of this
this.tx.assert_constraints = []
}
}

module.exports = {
attachConstraints,
checkConstraints,
}
2 changes: 1 addition & 1 deletion db-service/lib/cqn4sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ function cqn4sql(originalQuery, model) {
outerAlias = transformedQuery.SELECT.from.as
const getInnermostTarget = q => q._target ? getInnermostTarget(q._target) : q
subqueryFromRef = [
...(transformedQuery.SELECT.from.ref || /* subq in from */ [getInnermostTarget(transformedQuery).name]),
...([getInnermostTarget(transformedQuery).name]),
...ref,
]
}
Expand Down
1 change: 1 addition & 0 deletions hana/test/param-views.cds
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ entity ParamBooks(available : Integer) as
// Take foreign key for author association
author.ID as author_ID,
// author, Compiler does not like associations in parameterized views
price,
}
where
stock <= :available;
4 changes: 4 additions & 0 deletions test/bookshop/_i18n/messages.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ASSERT_RANGE = Value {0} is not in specified range [{1}, {2}]
STOCK_NOT_EMPTY = Stock for book "{title}" ({ID}) must not be a negative number
LIFE_BEFORE_DEATH = The Birthday "{dateOfBirth}" of author "{name}" must not be after the Deathday "{dateOfDeath}"
GENRE_NAME_TOO_LONG = Genre name "{name}" exceeds maximum length of 20 characters
17 changes: 17 additions & 0 deletions test/bookshop/db/schema.cds
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,25 @@ entity Books : managed {
descr : localized String(1111);
author : Association to Authors;
genre : Association to Genres default 10;
@assert.constraint.stockNotEmpty : {
condition: ( stock >= 0 ),
message: 'STOCK_NOT_EMPTY',
parameters: [title, ID] // to be inserted into the message
}
stock : Integer;
price : Decimal;
dummyDecimal : Decimal;
currency : Currency;
image : LargeBinary @Core.MediaType: 'image/png';
footnotes : array of String;
authorsAddress : String = author.address;
}

@assert.constraint.dates : {
condition: ( days_between(dateOfBirth, dateOfDeath) >= 0 ),
message: 'LIFE_BEFORE_DEATH',
parameters: [name, dateOfBirth, dateOfDeath]
}
entity Authors : managed {
key ID : Integer;
name : String(111);
Expand All @@ -43,6 +54,8 @@ entity Genres : sap.common.CodeList {
on children.parent = $self;
}

annotate Genres:name with @assert.constraint: {condition: (length(name) <= 25), parameters: [name], message: 'GENRE_NAME_TOO_LONG'};

entity A : managed {
key ID : Integer;
B : Integer;
Expand All @@ -53,6 +66,10 @@ entity A : managed {
on toC.ID = $self.C;
}

@assert.constraint.foreign: {
condition: (A != 42),
message: 'A must not be 42',
}
entity B : managed {
key ID : Integer;
A : Integer;
Expand Down
7 changes: 6 additions & 1 deletion test/bookshop/srv/admin-service.cds
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ service AdminService @(requires:'admin', path:'/admin') {
entity Books as projection on my.Books;
entity Authors as projection on my.Authors;
entity A as projection on my.A;
entity B as projection on my.B;
entity Genres as projection on my.Genres;

@cds.redirection.target: false
entity RenameKeys as projection on my.Books {
key ID as foo,
author,
author.name
author.name,
stock,
title as myTitle,
price,
}
}
2 changes: 1 addition & 1 deletion test/bookshop/srv/cat-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CatalogService extends cds.ApplicationService {
let b = await SELECT`stock`.from(Books, book)
if (!b) return req.error(404, `Book #${book} doesn't exist`)
let { stock } = b
if (quantity > stock) return req.reject(409, `${quantity} exceeds stock for book #${book}`)
// if (quantity > stock) return req.reject(409, `${quantity} exceeds stock for book #${book}`)
await UPDATE(Books, book).with({ stock: (stock -= quantity) })
await this.emit('OrderedBook', { book, quantity, buyer: req.user.id })
return { stock }
Expand Down
Loading
Loading