Skip to content

#13555 - Fixed notifier query to correctly get the most recent version#13583

Merged
raulbob merged 1 commit intodevelopmentfrom
bugfix-13555-phone_dd_save_error
Sep 2, 2025
Merged

#13555 - Fixed notifier query to correctly get the most recent version#13583
raulbob merged 1 commit intodevelopmentfrom
bugfix-13555-phone_dd_save_error

Conversation

@raulbob
Copy link
Copy Markdown
Contributor

@raulbob raulbob commented Aug 28, 2025

Fixes data was changed error in manual DD notification entry.

Summary by CodeRabbit

  • Bug Fixes

    • Corrected retrieval of notifier details for a chosen date/time, ensuring the latest values as of that moment are displayed consistently.
  • Refactor

    • Streamlined backend query logic for historical notifier data to improve reliability and consistency without altering existing behavior or interfaces.

@raulbob raulbob requested a review from KarnaiahPesula August 28, 2025 14:11
@raulbob raulbob linked an issue Aug 28, 2025 that may be closed by this pull request
3 tasks
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Aug 28, 2025

Walkthrough

Rewrote NotifierService.getByUuidAndTime native SQL to use a UNION of notifier_history and notifier filtered by uuid and changeDate, then select the latest row by changedate DESC LIMIT 1. Removed per-column COALESCE logic in favor of selecting whole rows. Method signature and parameter bindings unchanged.

Changes

Cohort / File(s) Change summary
Notifier retrieval query refactor
sormas-backend/src/main/java/de/symeda/sormas/backend/person/notifier/NotifierService.java
Replaced left-join/COALESCE query with UNION of history and current tables, ordered by changedate DESC LIMIT 1; retained parameters and mapping; added terminating semicolon.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Service as NotifierService
  participant DB as Database

  Service->>DB: Native SQL (UNION notifier_history + notifier)\nWHERE uuid=:uuid AND changedate<=:changeDate
  DB-->>Service: Single row (ORDER BY changedate DESC LIMIT 1)
  Note over Service: Return mapped Notifier entity
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

A rabbit taps keys in a midnight den,
Unions two trails, then sorts them again.
Gone is the COALESCE, cleaner the view—
One latest whisper of data rings true.
Ears perked high, I ship with a grin:
The newest note hops neatly in.

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch bugfix-13555-phone_dd_save_error

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbit in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbit in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbit gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbit read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbit help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbit ignore or @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbit summary or @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbit or @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
sormas-backend/src/main/java/de/symeda/sormas/backend/person/notifier/NotifierService.java (1)

99-103: Handle no-row case; getSingleResult throws NoResultException

Requests earlier than the entity’s first change will yield 0 rows; getSingleResult() will raise NoResultException and bubble up. Return null instead.

-        return (Notifier) query.getSingleResult();
+        @SuppressWarnings("unchecked")
+        final java.util.List<Notifier> results = query.getResultList();
+        return results.isEmpty() ? null : results.get(0);
🧹 Nitpick comments (1)
sormas-backend/src/main/java/de/symeda/sormas/backend/person/notifier/NotifierService.java (1)

94-97: Ensure supporting index on (uuid, changedate) for both tables

Without a composite index on (uuid, changedate) for notifier and notifier_history, this query can degrade under load.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 143f82f and 0a3b2ff.

📒 Files selected for processing (1)
  • sormas-backend/src/main/java/de/symeda/sormas/backend/person/notifier/NotifierService.java (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: SORMAS CI

Comment on lines +94 to +97
final String sql = "SELECT * FROM ("
+ "SELECT * FROM (SELECT * FROM notifier_history b WHERE b.uuid = :uuid AND changedate <= CAST(:changeDate AS TIMESTAMP) ORDER BY changedate DESC LIMIT 1) "
+ "UNION SELECT * FROM notifier a WHERE a.uuid = :uuid) "
+ "WHERE uuid = :uuid AND changedate <= CAST(:changeDate AS TIMESTAMP) ORDER BY changedate DESC LIMIT 1;";
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Avoid SELECT * across UNION; use UNION ALL; drop trailing semicolon; simplify filters

  • SELECT * + UNION between base and history risks column-order/type mismatches when mapping to Notifier; verify both tables have identical column sets in identical order. Prefer enumerating columns explicitly.
  • UNION incurs unnecessary dedup; UNION ALL is faster here.
  • The outer WHERE repeats uuid/changedate; push changedate into both branches and keep a single ORDER BY/LIMIT.
  • Trailing semicolon can break some JPA providers.

Apply:

-final String sql = "SELECT * FROM ("
-    + "SELECT * FROM (SELECT * FROM notifier_history b WHERE b.uuid = :uuid AND changedate <= CAST(:changeDate AS TIMESTAMP) ORDER BY changedate DESC LIMIT 1) "
-    + "UNION SELECT * FROM notifier a WHERE a.uuid = :uuid) "
-    + "WHERE uuid = :uuid AND changedate <= CAST(:changeDate AS TIMESTAMP) ORDER BY changedate DESC LIMIT 1;";
+final String sql =
+    "SELECT t.* FROM (" +
+    "  SELECT b.* FROM notifier_history b " +
+    "  WHERE b.uuid = :uuid AND b.changedate <= CAST(:changeDate AS TIMESTAMP) " +
+    "  UNION ALL " +
+    "  SELECT a.* FROM notifier a " +
+    "  WHERE a.uuid = :uuid AND a.changedate <= CAST(:changeDate AS TIMESTAMP) " +
+    ") t " +
+    "ORDER BY t.changedate DESC LIMIT 1";

If history/base schemas diverge, explicitly list columns in the exact Notifier mapping order instead of using *.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
final String sql = "SELECT * FROM ("
+ "SELECT * FROM (SELECT * FROM notifier_history b WHERE b.uuid = :uuid AND changedate <= CAST(:changeDate AS TIMESTAMP) ORDER BY changedate DESC LIMIT 1) "
+ "UNION SELECT * FROM notifier a WHERE a.uuid = :uuid) "
+ "WHERE uuid = :uuid AND changedate <= CAST(:changeDate AS TIMESTAMP) ORDER BY changedate DESC LIMIT 1;";
final String sql =
"SELECT t.* FROM (" +
" SELECT b.* FROM notifier_history b " +
" WHERE b.uuid = :uuid AND b.changedate <= CAST(:changeDate AS TIMESTAMP) " +
" UNION ALL " +
" SELECT a.* FROM notifier a " +
" WHERE a.uuid = :uuid AND a.changedate <= CAST(:changeDate AS TIMESTAMP) " +
") t " +
"ORDER BY t.changedate DESC LIMIT 1";
🤖 Prompt for AI Agents
In
sormas-backend/src/main/java/de/symeda/sormas/backend/person/notifier/NotifierService.java
around lines 94-97, the query uses SELECT * with UNION and a trailing semicolon
and repeats filters; replace SELECT * with an explicit, ordered list of columns
matching the Notifier entity mapping (ensuring identical order and types for
both branches), change UNION to UNION ALL, move the changedate filter into both
the history and base SELECT branches (keep a single ORDER BY ... LIMIT in the
outer query), and remove the trailing semicolon so the JPA provider won’t choke;
if schemas diverge, enumerate columns exactly in the same order for both
SELECTs.

@raulbob raulbob merged commit 19af2d3 into development Sep 2, 2025
6 of 9 checks passed
@raulbob raulbob deleted the bugfix-13555-phone_dd_save_error branch September 2, 2025 10:23
@raulbob raulbob restored the bugfix-13555-phone_dd_save_error branch September 3, 2025 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ability to create doctor's notification by phone (edit Notified by box)

1 participant