Skip to content

fix: populate CREATEDBY from session user instead of CURRENT_USER() - #375

Merged
rjayasinghe merged 16 commits into
mainfrom
fix/h2-createdby-from-session-user
Oct 5, 2026
Merged

rjayasinghe merged 16 commits into
mainfrom
fix/h2-createdby-from-session-user

Conversation

@rjayasinghe

Copy link
Copy Markdown
Contributor

Summary

  • Add getUser() to AbstractChangeTrackingTrigger which reads the user_id session variable set by the CAP runtime, falling back to CURRENT_USER() when it is absent
  • Replace the inline CURRENT_USER() SQL function in every changelog INSERT with a bind parameter ?, bound to the createdBy variable resolved at trigger runtime
  • Ensures the recorded CREATEDBY value reflects the authenticated application user rather than the DB connection user

Test plan

  • Verify changelog entries for insert, update, and delete operations record the application user in CREATEDBY
  • Verify fallback to CURRENT_USER() when user_id session variable is not set
  • Run existing H2 integration tests to check for regressions

Add `getUser()` to `AbstractChangeTrackingTrigger` which reads the
`user_id` session variable (set by the CAP runtime) and falls back to
`CURRENT_USER()`. The H2 codegen now passes `createdBy` as a bind
parameter for every changelog INSERT instead of embedding
`CURRENT_USER()` directly in the SQL, so the recorded user reflects
the authenticated application user rather than the DB connection user.
@rjayasinghe
rjayasinghe requested a review from a team as a code owner September 21, 2026 08:22
@hyperspace-pr-bot

Copy link
Copy Markdown
Contributor

Summary

The following content is AI-generated and provides a summary of the pull request:


Fix changelog CREATEDBY to use CAP session user

Bug Fix

🐛 Updated H2 change-tracking trigger generation so changelog entries store the authenticated CAP application user in CREATEDBY instead of always using the database connection user. If no CAP session user is available, the logic falls back to CURRENT_USER().

Changes

  • lib/h2/java-templates/AbstractChangeTrackingTrigger.java: Added getUser(conn) helper to read the user_id session variable and fallback to SELECT CURRENT_USER() when unavailable.
  • lib/h2/java-codegen.js: Resolves createdBy once during trigger execution and binds it into changelog INSERT statements.
  • lib/h2/java-codegen.js: Replaced inline CURRENT_USER() usage in generated changelog SQL with a bind parameter for create, update, and delete tracking entries.
  • lib/h2/java-codegen.js: Extended generated binding lists to include createdBy for both parent composition and non-composition changelog inserts.

  • 🔄 Regenerate and Update Summary
  • ✏️ Insert as PR Description (deletes this comment)
  • 🗑️ Delete comment
PR Bot Information

Version: 1.31.43

  • File Content Strategy: Full file content
  • Output Template: Default Template
  • Event Trigger: pull_request.opened
  • Summary Prompt: Default Prompt
  • Correlation ID: 946d6d70-b595-11f1-9ee2-63706edf22e5
  • LLM: gpt-5.5

@hyperspace-pr-bot hyperspace-pr-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The review has been concluded!

PR Bot Information

Version: 1.31.43

  • LLM: gpt-5.5
  • Event Trigger: pull_request.opened
  • File Content Strategy: Full file content
  • Correlation ID: 946d6d70-b595-11f1-9ee2-63706edf22e5

with that we can use the session variable already set
by default by the cap java runtime.
Schmarvinius
Schmarvinius previously approved these changes Sep 22, 2026
Comment thread package.json Outdated
this is actually not needed here.
Schmarvinius
Schmarvinius previously approved these changes Sep 22, 2026
swaldmann
swaldmann previously approved these changes Sep 22, 2026
@rjayasinghe
rjayasinghe added this pull request to the merge queue Sep 22, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Sep 22, 2026
@rjayasinghe

Copy link
Copy Markdown
Contributor Author

@Schmarvinius @swaldmann tests for HANA fail with timeout. I am pretty sure that this is not caused by my change as this is Java only and should not influence any of the generation for HANA.

@rjayasinghe
rjayasinghe dismissed stale reviews from swaldmann and Schmarvinius via 9426554 September 23, 2026 07:23
…DS 9 stack

npm i <tgz> in the "Install branch" step re-resolves deps inside perf-bookshop,
picking up @cap-js/sqlite@3.x which requires @sap/cds@^10, conflicting with the
CDS 9 matrix. Adding an overrides entry forces npm to use ^2 during that install.
stefanrudi
stefanrudi previously approved these changes Sep 29, 2026
Schmarvinius
Schmarvinius previously approved these changes Sep 29, 2026
@stefanrudi
stefanrudi added this pull request to the merge queue Sep 29, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Sep 29, 2026

@agoerler agoerler left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this trigger runs in the same transaction as the tx that does the changes on H2 the session context variables @applicationuser and @now (the logical transaction timestamp) should always be set. Therefore we should be able just to use @applicationuser and @now in the SQL.

Moreover, we can't use CURRENT_TIMESTAMP() as this determines the timestamp on the DB but we need the logical transaction timestamp. Also we can't use CURRENT_USER() as this would be the user we use to connect to H2. But we need the application user instead.

Comment thread lib/h2/java-codegen.js Outdated
Comment thread lib/h2/java-templates/AbstractChangeTrackingTrigger.java Outdated
Comment thread lib/h2/java-codegen.js Outdated
Co-authored-by: Adrian Görler <adrian.goerler@sap.com>
@rjayasinghe
rjayasinghe dismissed stale reviews from Schmarvinius and stefanrudi via a462ac0 September 30, 2026 09:41
@rjayasinghe
rjayasinghe requested a review from agoerler October 5, 2026 07:13
…rors

After `String locale` and `String createdBy` were removed from the
fire() method template, two generated-code bugs remained:

1. `_wrapInTryCatch` emitted `stmt.setString(N, locale)` for localized
   association columns (whose `labelRes.bindings` contains `'locale'`),
   causing `cannot find symbol: variable locale` at Java compile time.
   Fix: resolve the `'locale'` binding to `getLocale(conn)` inline.

2. The composition parent INSERT VALUES still used `CURRENT_TIMESTAMP(), ?`
   for CREATEDAT/CREATEDBY while `'createdBy'` had been dropped from
   allBindings, causing a PreparedStatement parameter count mismatch.
   Fix: use `@now, @applicationuser` session variable literals for the
   composition parent case too, consistent with the non-parent case.
@rjayasinghe
rjayasinghe dismissed agoerler’s stale review October 5, 2026 10:21

the requested changes were applied

@rjayasinghe
rjayasinghe enabled auto-merge October 5, 2026 10:22
@rjayasinghe

Copy link
Copy Markdown
Contributor Author

@stefanrudi @Schmarvinius I had to make some changes after last proposed changes from @agoerler - can you please re-approve this PR?

@rjayasinghe
rjayasinghe added this pull request to the merge queue Oct 5, 2026
Merged via the queue into main with commit e648034 Oct 5, 2026
23 checks passed
@rjayasinghe
rjayasinghe deleted the fix/h2-createdby-from-session-user branch October 5, 2026 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants