Skip to content

Conversation

@mschmid09
Copy link
Contributor

@mschmid09 mschmid09 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
    • Relation identifiers now have surrounding quotes removed before use, ensuring consistent handling of database/schema names.
    • Logs now display the cleaned (unquoted) relation value for clearer diagnostics.
    • Existing fallback behavior on errors is preserved to maintain resilience.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Aug 19, 2025

Walkthrough

Function now strips surrounding quotes from the retrieved relation identifier, logs the stripped value, and returns the stripped relation. On exception it logs the error, records via tracking if present, and returns the fallback <elementary_database>.<elementary_schema>.

Changes

Cohort / File(s) Summary
Relation quote stripping
elementary/monitor/data_monitoring/data_monitoring.py
After obtaining the relation string, strip surrounding quotes (if any), log the stripped value, and return the stripped relation. Error path unchanged: exceptions log the error, record via tracking if present, and return the fallback <elementary_database>.<elementary_schema>. No public API changes.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Review the logic that strips quotes to ensure it only removes surrounding quotes and preserves internal content.
  • Check logging statements and tracking/error-handling branch for unchanged behavior.

Poem

I hopped through names both clipped and bright,
I brushed away the quotes at night.
I logged the clean and kept it neat,
Returned the name to steady feet. 🐇✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: properly quoting database and schema identifiers for SQL compatibility by splitting and quoting them separately.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • 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

Comment @coderabbitai help to get the list of available commands and usage tips.

@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)

@mschmid09 mschmid09 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
@mschmid09 mschmid09 force-pushed the fix/replace-double-quotes-with-single-quotes branch from 4c14bf7 to 6a66be5 Compare August 25, 2025 02:50
@mschmid09 mschmid09 had a problem deploying to elementary_test_env August 25, 2025 02:50 — with GitHub Actions Failure
@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
@mschmid09 mschmid09 force-pushed the fix/replace-double-quotes-with-single-quotes branch from 6a66be5 to a3a3581 Compare August 27, 2025 01:23
@mschmid09 mschmid09 force-pushed the fix/replace-double-quotes-with-single-quotes branch from a3a3581 to 68defa0 Compare August 27, 2025 01:24
@mschmid09 mschmid09 force-pushed the fix/replace-double-quotes-with-single-quotes branch from 68defa0 to 60faf1c Compare August 28, 2025 01:57
@mschmid09 mschmid09 had a problem deploying to elementary_test_env August 28, 2025 01:57 — with GitHub Actions Failure
@mschmid09
Copy link
Contributor Author

@elazarlachkar should be good to go :)

@mschmid09 mschmid09 had a problem deploying to elementary_test_env August 31, 2025 08:10 — with GitHub Actions Failure
@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.

@mschmid09 mschmid09 force-pushed the fix/replace-double-quotes-with-single-quotes branch from 60faf1c to 701198f Compare September 1, 2025 01:33
@mschmid09
Copy link
Contributor 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?

@mschmid09
Copy link
Contributor Author

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

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

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

@mschmid09
Copy link
Contributor Author

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

@arbiv arbiv temporarily deployed to elementary_test_env November 11, 2025 09:40 — with GitHub Actions Inactive
Copy link
Contributor

@elazarlachkar elazarlachkar left a comment

Choose a reason for hiding this comment

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

Hi @mschmidoev!

I really apologize for the delay here. At the end of September I shifted my focus to other things, and I missed your check in message.

I checked your changes myself, and consulted one of my colleagues here at Elementary, and added a comment I think should be addressed.

Please let me know what you think? This PR is a priority for me now.

@mschmid09 mschmid09 temporarily deployed to elementary_test_env December 2, 2025 11:26 — with GitHub Actions Inactive
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 (1)
elementary/monitor/data_monitoring/data_monitoring.py (1)

82-85: LGTM! Implementation follows the agreed approach.

The solution correctly strips surrounding quotes from the relation to avoid case sensitivity issues and double-quoting problems, as discussed in the previous review. The implementation is clean and straightforward.

Optional: Consider the try-except-else pattern.

Static analysis suggests moving the return statement to an else block for slightly more idiomatic exception handling:

 try:
     relation = self.internal_dbt_runner.run_operation(
         "elementary_cli.get_elementary_database_and_schema", quiet=True
     )[0]
     # Strip quotes from relation to avoid case sensitivity issues and double-quoting
     strip_relation = relation.strip('"')
     logger.info(f"Elementary's database and schema: '{strip_relation}'")
-    return strip_relation
 except Exception as ex:
     logger.error("Failed to parse Elementary's database and schema.")
     if self.tracking:
         self.tracking.record_internal_exception(ex)
     return "<elementary_database>.<elementary_schema>"
+else:
+    return strip_relation

This pattern makes the success path more explicit, though the current code is perfectly valid.

Note: Identifier quoting assumptions.

The stripped relation (e.g., my_db.my_schema) will work correctly as long as the database and schema names don't contain special characters, spaces, or reserved words that require quoting in SQL. This should be fine for typical use cases, but if issues arise downstream with specific identifier names, this assumption may need revisiting.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • 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 701198f and a4d5a1c.

📒 Files selected for processing (1)
  • elementary/monitor/data_monitoring/data_monitoring.py (1 hunks)
🧰 Additional context used
🪛 Ruff (0.14.7)
elementary/monitor/data_monitoring/data_monitoring.py

85-85: Consider moving this statement to an else block

(TRY300)

@elazarlachkar elazarlachkar merged commit 34b875e into elementary-data:master Dec 2, 2025
5 checks passed
@elazarlachkar
Copy link
Contributor

@mschmidoev I merged the change, thanks for the PR!
It will be included in the next release (should be in the next few days).

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.

4 participants