Skip to content

wip(POC): @assert.constraint #988

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

Draft
wants to merge 118 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
118 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
3647378
use real refs as params, which are renamed by compiler in propagated …
patricebender Apr 11, 2025
d322e88
no dangling `and`
patricebender Apr 11, 2025
22372eb
for pg, error come back in different order
patricebender Apr 14, 2025
8638431
named and anonymous params
patricebender Apr 16, 2025
a65bb23
refactor collect constraints function
patricebender Apr 17, 2025
fda73f7
refactor check constraints
patricebender Apr 17, 2025
f1e8c19
add sibling bookshop to test constraints
patricebender Apr 22, 2025
23e99f0
navigations in params
patricebender Apr 22, 2025
37decb9
expressions as anonymous parameters
patricebender Apr 22, 2025
e861bea
simpler data structure for params
patricebender Apr 22, 2025
bcad437
some more robustenss
patricebender Apr 22, 2025
e645059
Merge remote-tracking branch 'origin/main' into patrice/assert-constr…
patricebender Apr 22, 2025
b1cdd35
use tx as weak map key
patricebender Apr 22, 2025
c17ea3c
some doc
patricebender Apr 22, 2025
a8c692d
no need for workaround anymore
patricebender Apr 22, 2025
6f9828a
back to original test
patricebender Apr 22, 2025
1b6095f
trailing ws
patricebender Apr 22, 2025
94f573d
back to main
patricebender Apr 22, 2025
98edf9a
better comment
patricebender Apr 22, 2025
d2f963f
better structure
patricebender Apr 22, 2025
93d256b
simplify resulting validation queries
patricebender Apr 22, 2025
26f74b1
if there are no queries, dont try to run them
patricebender Apr 22, 2025
5a376b1
more docsl, more tests, more features
patricebender Apr 23, 2025
8c76a8b
better var name
patricebender Apr 24, 2025
f282d61
new tests, constraints in own file, renamings
patricebender Apr 24, 2025
191ee0c
need to wait for #1156 before enabling the new test
patricebender Apr 24, 2025
682ac03
Merge remote-tracking branch 'origin/main' into patrice/assert-constr…
patricebender Apr 24, 2025
e588b33
enable test after fix has been merged
patricebender Apr 24, 2025
ade0107
improve doc
patricebender Apr 24, 2025
a935afd
btter name
patricebender Apr 24, 2025
7205807
test with calculated element
patricebender Apr 28, 2025
210f996
Merge branch 'main' into patrice/assert-constraint
patricebender Apr 28, 2025
249b922
run all tests
patricebender Apr 28, 2025
f0f1096
Merge branch 'main' into patrice/assert-constraint
patricebender Apr 30, 2025
4152c17
wip: dedup
patricebender Apr 30, 2025
f8c119a
dedup messages with naive approach
patricebender Apr 30, 2025
47cc895
better test
patricebender Apr 30, 2025
57ba656
better wording
patricebender Apr 30, 2025
336cc6e
use up_ link
patricebender Apr 30, 2025
b946609
Merge remote-tracking branch 'origin/main' into patrice/assert-constr…
patricebender May 7, 2025
051771e
Merge remote-tracking branch 'origin/main' into patrice/assert-constr…
patricebender May 19, 2025
7e2f4e5
unused var
patricebender May 19, 2025
8ed3e88
no need to select pk
patricebender May 19, 2025
bd50836
handle targets
patricebender May 19, 2025
24f8fdb
implicit target
patricebender May 19, 2025
8637e9d
more control with for…of
patricebender May 19, 2025
b05a621
request params only if constraints were violated
patricebender May 19, 2025
db83147
better structure
patricebender May 19, 2025
c5c9f8b
improved comments
patricebender May 19, 2025
8972d1f
proper params fetching
patricebender May 21, 2025
ec2e60b
remove unused import
patricebender May 23, 2025
8414153
only execute constraints if necessary
patricebender May 23, 2025
de73f8b
test for payload based constraint execution
patricebender May 23, 2025
5b93973
fix plain sql insert
patricebender May 26, 2025
66addc7
Merge remote-tracking branch 'origin/main' into patrice/assert-constr…
patricebender May 26, 2025
b65fa2a
adapt plain sql insert for quoted tests
patricebender May 27, 2025
d9d75ab
cleaner signature
patricebender May 27, 2025
c57248e
constraint with pseudo variable
patricebender May 28, 2025
e027c42
remove comment
patricebender May 28, 2025
fae4cee
Merge remote-tracking branch 'origin/main' into patrice/assert-constr…
patricebender Jul 3, 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 Down Expand Up @@ -37,6 +38,7 @@ const _hasProps = (obj) => {
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 @@ -67,6 +69,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
124 changes: 124 additions & 0 deletions db-service/lib/assert-constraint/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
'use strict'

const { getValidationQuery, buildMessage, getWhereOfPatch, getConstraintsByTarget } = require('./utils')

const constraintStorage = require('./storage')

/**
*
* “Before-hook” that gathers every `@assert.constraint` touched by the current
* request, optionally augments them with an extra filter (the
* `where` of an UPDATE/UPSERT), and merges the result into
* {@link constraintStorage} so it can be validated once per transaction in
* {@link checkConstraints}.
*
* 1. Skip CSV/multipart inserts.
* 2. Call `getConstraintsByTarget()` to obtain a `Map`
* `targetName → ConstraintDict`.
* 3. If the root entity is an UPDATE/UPSERT, retrieve its filter via
* `getWhereOfPatch(req)` and push it into the `where` array of every
* constraint that belongs to that root entity.
* 4. For each target entity, merge the (possibly augmented) constraints into
* the per-transaction storage with `constraintStorage.merge(this.tx, …)`.
*
* Notes
* ─────────────────────────────────────────────────────────────────
* • No validation queries are built here; we only stash **metadata**.
* • The function is idempotent within the same transaction because
* `constraintStorage.merge` deduplicates constraints and WHEREs.
*
* @this {import('@sap/cds').Service} CAP service or transaction object
* @param {*} _res (ignored — payload from previous hook)
* @param {import('@sap/cds').Request} req Current request being processed
*/
function attachConstraints(_res, req) {
if (Array.isArray(req.data?.[0])) return // ignore CSV / multipart

const byTarget = getConstraintsByTarget(req.target, req.data)
if (!byTarget.size) return

const patchWhere = getWhereOfPatch(req) // null | array
const rootName = req.target.name

for (const [targetName, constraints] of byTarget) {
if (patchWhere && targetName === rootName) {
Object.values(constraints).forEach(c => {
c.where.push([{ xpr: patchWhere }])
})
}
constraintStorage.merge(this.tx, targetName, constraints)
}
}

/**
* Validate all pending constraints for the current transaction.
*
* 1. Collect constraints from `constraintStorage`.
* 2. Build **one** SQL query per target and run them in the current tx.
* 3. For each failed row:
* - Lazily fetch the message params **only when** the constraint is violated.
* - Build a message, and register it with `req.error(...)`,
* including all affected targets for the UI (`@Common.additionalTargets`).
* 4. Clear the constraint cache for this transaction.
*
* @param {import('@sap/cds').Request} req – CDS request that will hold any validation errors.
*/
async function checkConstraints(req) {
const constraintsPerTarget = constraintStorage.get(this.tx)
if (!constraintsPerTarget.size) return

// build exactly one query per bucket
const queries = []
for (const [targetName, constraints] of constraintsPerTarget) {
queries.push(getValidationQuery(targetName, constraints))
}

const results = await this.run(queries)

// key = message text value = array of targets (order preserved)
// this way messages are deduplicated
const messages = new Map()

for (const [i, rows] of results.entries()) {
const constraints = queries[i].$constraints
const paramQuery = queries[i].$paramQuery
let params

for (const [name, meta] of Object.entries(constraints)) {
const col = `${name}_constraint`

// request params in separate query, because constraint has failed
if (paramQuery && rows.some(r => r[col] === false)) {
// use the same transaction as the query (or it would re-trigger the handler endlessly)
params = await this.tx().run(paramQuery)
}

for (const [j, r] of rows.entries()) {
if (r[col]) continue // row satisfied the constraint → no error

const row = params ? { ...r, ...params[j] } : r // merge only if needed

const text = buildMessage(name, meta, row)
const targets = meta.targets?.length ? meta.targets : [{ ref: [meta.element.name] }]

;(messages.get(text) ?? []).push(...targets)
messages.set(text, targets)
}
}
}

for (const [text, targetList] of messages) {
req.error(400, {
message: text,
target: targetList[0]?.ref.join('/'),
'@Common.additionalTargets': targetList.map(t => t.ref.join('/')),
})
}

constraintStorage.clear(this.tx)
}

module.exports = {
attachConstraints,
checkConstraints,
}
66 changes: 66 additions & 0 deletions db-service/lib/assert-constraint/storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use strict'

const _constraintStorage = new WeakMap() // tx → Map<targetName, ConstraintDict>

function dedupWhere(arr) {
const seen = new Set()
return arr.filter(x => {
const key = JSON.stringify(x) // keep track of expressions already seen
return !seen.has(key) && seen.add(key)
})
}

function mergeConstraint(a, b) {
const merged = { ...a, ...b }

const where = [...(a.where ?? []), ...(b.where ?? [])]
merged.where = dedupWhere(where)

return merged
}

/**
* constraintStorage
*
* In-memory repository that collects assert-constraint metadata
* _per CDS transaction_ and _per target entity_.
* The data is stored in a `WeakMap`, so everything is garbage-collected
* automatically when the transaction object becomes unreachable at the end of
* the request.
*
* Storage structure
* ─────────────────
* WeakMap
* └─ key : cds.Transaction
* └─ value : Map<string, ConstraintDict>
* └─ key : target entity name (e.g. 'bookshop.Books')
* └─ value : { [constraintName]: ConstraintMeta }
*
* `ConstraintMeta` is the object returned by `collectConstraints()` and may
* contain `condition`, `parameters`, `where`, … – see utils.js.
*/
module.exports = {
/**
* Merge a set of constraints into the bucket that belongs to this tx+entity.
* @param {import('@sap/cds').Transaction} tx
* @param {string} targetName
* @param {object} constraints result of utils.getConstraintsByTarget().get(targetName)
*/
merge(tx, targetName, constraints) {
const txMap = _constraintStorage.get(tx) ?? new Map()
const existingConstraintsForTarget = txMap.get(targetName) ?? {}

for (const [name, c] of Object.entries(constraints)) {
existingConstraintsForTarget[name] = existingConstraintsForTarget[name]
? mergeConstraint(existingConstraintsForTarget[name], c)
: { ...c }
}

txMap.set(targetName, existingConstraintsForTarget)
_constraintStorage.set(tx, txMap)
},

/** @returns Map<string,object> */
get: tx => _constraintStorage.get(tx) ?? new Map(),
clear: tx => _constraintStorage.delete(tx),
}
Loading