Skip to content

Conversation

@mschmidoev
Copy link

@mschmidoev mschmidoev commented Aug 19, 2025

Summary
This PR updates the logic for constructing the elementary_database_and_schema identifier so that the database and schema are each wrapped in double quotes, e.g. "database"."schema". This ensures correct SQL syntax and avoids treating the entire string as a single identifier.

Details
Previously, the code would wrap the whole database.schema string in quotes, resulting in "database.schema", which is not valid in standard SQL.
Now, the code splits the identifier on the dot and wraps each part individually: "database"."schema".
If only a schema is present, it is wrapped as "schema".

#1986

Summary by CodeRabbit

  • Bug Fixes
    • Consistently quotes relation identifiers for both "database.schema" and single-name relations.
    • Improves formatting for case-sensitive names and special characters to avoid lookup/execution issues.
    • Logs now show the normalized quoted relation for clearer diagnostics.
    • Maintains existing fallback behavior on errors to preserve resilience.

@coderabbitai
Copy link

coderabbitai bot commented Aug 19, 2025

Walkthrough

Wrap retrieved relation identifier(s) in double quotes: if the relation contains a dot, split into database and schema and return "<db>"."<schema>"; if single-part, return "<relation>". The quoted relation is logged and returned. On exception, the function logs the error and returns the fallback <elementary_database>.<elementary_schema>.

Changes

Cohort / File(s) Summary
Relation quoting normalization
elementary/monitor/data_monitoring/data_monitoring.py
After obtaining the relation string, if it contains a dot split into two parts and wrap each in double quotes to produce "<db>"."<schema>"; if single-part, wrap as "<relation>". Log and return the quoted value. Exceptions still log the error and return the fallback <elementary_database>.<elementary_schema>. No public API changes.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant DataMonitor
    participant Logger

    Caller->>DataMonitor: get_relation()
    DataMonitor->>DataMonitor: retrieve relation string
    alt relation contains dot
        DataMonitor->>DataMonitor: split into db and schema
        DataMonitor->>DataMonitor: format as "<db>"."<schema>"
    else single identifier
        DataMonitor->>DataMonitor: format as "<relation>"
    end
    DataMonitor->>Logger: log quoted relation
    DataMonitor->>Caller: return quoted relation
    opt exception
        DataMonitor->>Logger: log error
        DataMonitor->>Caller: return fallback "<elementary_database>.<elementary_schema>"
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I nibbled names beneath the moon,
I split the dot and hummed a tune,
Two quotes for two, one for one,
I logged the song when work was done.
If errors hop, the fallback runs. 🐇✨


📜 Recent 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 60faf1c and 701198f.

📒 Files selected for processing (1)
  • elementary/monitor/data_monitoring/data_monitoring.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • elementary/monitor/data_monitoring/data_monitoring.py
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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 @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai 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:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai 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 @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @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.

@github-actions
Copy link
Contributor

👋 @mschmidoev
Thank you for raising your pull request.
Please make sure to add tests and document all user-facing changes.
You can do this by editing the docs files in this pull request.

Copy link

@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: 0

🧹 Nitpick comments (2)
elementary/monitor/data_monitoring/data_monitoring.py (2)

82-83: If embedding inside single-quoted SQL literals, escape single quotes instead

If the goal is to safely place the relation inside a single-quoted SQL string literal (e.g., in a Slack snippet), converting double quotes is unnecessary and potentially harmful. Instead, escape single quotes in the content:

-            relation = relation.replace('"', "'") if relation else relation
+            display_relation = relation.replace("'", "''") if relation else relation

This yields a string-literal-safe version while preserving any double quotes that are harmless inside single-quoted literals.


82-85: Scope quote-normalization for display only

We verified that the get_elementary_database_and_schema macro always returns an unquoted database.schema string, and that self.elementary_database_and_schema is used broadly (in tests, Slack messages, alert formatting, etc.). Replacing quotes on the canonical return value is a no-op today but could strip valid identifier quoting in the future. To avoid mutating the source-of-truth, only normalize for display:

In elementary/monitor/data_monitoring/data_monitoring.py (lines 82–85):

-            # Replace double quotes with single quotes for proper SQL compatibility
-            relation = relation.replace('"', "'") if relation else relation
-            logger.info(f"Elementary's database and schema: '{relation}'")
-            return relation
+            # Display-only: replace double quotes with single quotes in logs
+            display_relation = relation.replace('"', "'") if isinstance(relation, str) else relation
+            logger.info(f"Elementary's database and schema: '{display_relation}'")
+            return relation

If downstream callers (e.g. Slack templates) need a “safe” string, consider adding a derived elementary_database_and_schema_display property or performing .replace('"', "'") at the call site instead.

📜 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 4288f49 and f8ed8f1.

📒 Files selected for processing (1)
  • elementary/monitor/data_monitoring/data_monitoring.py (1 hunks)

@mschmidoev mschmidoev changed the title Fix: Replace double quotes with single quotes in elementary_database_and_schema Fix: Properly quote database and schema identifiers for SQL compatibility Aug 25, 2025
@mschmidoev mschmidoev force-pushed the fix/replace-double-quotes-with-single-quotes branch from 4c14bf7 to 6a66be5 Compare August 25, 2025 02:50
@elazarlachkar
Copy link
Contributor

@mschmidoev The CI is failing for some reason. Can you update your branch to the latest master?

ofek1weiss and others added 6 commits August 27, 2025 10:23
…and_schema

- Replace double quotes with single quotes for proper SQL compatibility
- Fixes SQL errors in Slack alert messages when database/schema names contain quotes
- Added conditional check to handle None values safely
@mschmidoev mschmidoev force-pushed the fix/replace-double-quotes-with-single-quotes branch from 6a66be5 to a3a3581 Compare August 27, 2025 01:23
@mschmidoev mschmidoev force-pushed the fix/replace-double-quotes-with-single-quotes branch from a3a3581 to 68defa0 Compare August 27, 2025 01:24
@mschmidoev mschmidoev force-pushed the fix/replace-double-quotes-with-single-quotes branch from 68defa0 to 60faf1c Compare August 28, 2025 01:57
@mschmidoev
Copy link
Author

@elazarlachkar should be good to go :)

@elazarlachkar
Copy link
Contributor

Hi @mschmidoev!

The CI still has the issue we solved on master.
Can you make sure you're updated to the latest elementary-data:master? I assume you updated from the fork's master (mschmidoev:master).

Thanks, and sorry for the trouble.

@mschmidoev mschmidoev force-pushed the fix/replace-double-quotes-with-single-quotes branch from 60faf1c to 701198f Compare September 1, 2025 01:33
@mschmidoev
Copy link
Author

Hey @elazarlachkar , no dramas, should be good now

@elazarlachkar
Copy link
Contributor

Hi @mschmidoev!
The CI issue is now fixed on the latest elementary-data:master. Sorry for the trouble, can you update your branch again?

@mschmidoev
Copy link
Author

Thanks @elazarlachkar, was off last week but this PR should be up to date with master branch now!

@mschmidoev mschmidoev temporarily deployed to elementary_test_env September 18, 2025 04:57 — with GitHub Actions Inactive
@mschmidoev
Copy link
Author

@elazarlachkar looks like that's worked this time around! Let me know if there's anything else you need :) )

@mschmidoev
Copy link
Author

Hey @elazarlachkar just checking if there's any progress here :)

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.

3 participants