Skip to content

Conversation

@zZedix
Copy link
Contributor

@zZedix zZedix commented Nov 29, 2025

Replace coalesce() with func.coalesce() and wrap USERS_AUTODELETE_DAYS in
literal() to ensure the Python config value is properly converted to a SQL
literal expression

Summary by CodeRabbit

  • Refactor
    • Improved internal database query handling for better compatibility with SQLAlchemy's API standards.

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

@coderabbitai
Copy link

coderabbitai bot commented Nov 29, 2025

Walkthrough

Updated SQLAlchemy usage in the user CRUD module to replace direct coalesce calls with the proper SQLAlchemy function interface. The constant fallback value is now wrapped in a literal() expression, and the coalesce function is invoked via func.coalesce().

Changes

Cohort / File(s) Summary
SQLAlchemy Function Interface Migration
app/db/crud/user.py
Refactored coalesce usage: replaced direct coalesce(User.auto_delete_in_days, USERS_AUTODELETE_DAYS) with func.coalesce(User.auto_delete_in_days, literal(USERS_AUTODELETE_DAYS)). Added literal import from SQLAlchemy.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

  • Single-file change with minimal scope
  • Simple import addition and localized function call refactoring
  • No logic modifications, only API usage standardization

Poem

🐰 With whiskers twitched and paws held high,
We've wrapped our constants under the sky,
From coalesce direct to func's embrace,
SQLAlchemy shines with proper grace! ✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: fixing the autodelete fallback mechanism by addressing how USERS_AUTODELETE_DAYS is handled in the coalesce function.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ 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.

@ImMohammad20000
Copy link
Contributor

@coderabbitai review

@coderabbitai
Copy link

coderabbitai bot commented Nov 29, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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)
app/db/crud/user.py (2)

252-252: Consider migrating to func.coalesce() for consistency.

While the current usage of the direct coalesce import works correctly here (both arguments are column expressions), consider using func.coalesce() for consistency with the fix at line 857 and SQLAlchemy 2.0 best practices.

Apply this diff for consistency:

-last_reset_time = coalesce(last_reset_subq.c.last_reset_at, User.created_at)
+last_reset_time = func.coalesce(last_reset_subq.c.last_reset_at, User.created_at)

If this change is made, you can remove the import on line 9:

-from sqlalchemy.sql.functions import coalesce

371-371: Consider wrapping integer literals for consistency.

While SQLAlchemy handles bare integer 0 correctly through type coercion, consider wrapping it with literal(0) for consistency with the fix at line 857.

Apply this diff:

-func.coalesce(NodeUserUsage.node_id, 0).label("node_id"),
+func.coalesce(NodeUserUsage.node_id, literal(0)).label("node_id"),

Apply the same pattern on line 938.

Also applies to: 938-938

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a3e1653 and ad86e05.

📒 Files selected for processing (1)
  • app/db/crud/user.py (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
app/db/crud/user.py (2)
app/db/models.py (1)
  • User (121-282)
app/models/user.py (1)
  • User (28-39)
🔇 Additional comments (2)
app/db/crud/user.py (2)

6-6: LGTM! Required import for SQL literal handling.

The addition of literal to the imports is correct and necessary to wrap the Python config value USERS_AUTODELETE_DAYS in a SQL literal expression.


857-857: I encountered a repository access issue, but I can still work with the context from the review comment itself. Let me provide a rewritten version that reflects what can be reasonably assessed from the code snippet provided:


Correct SQLAlchemy 2.0 usage for auto-deletion fallback.

The change to func.coalesce(User.auto_delete_in_days, literal(USERS_AUTODELETE_DAYS)) properly addresses the issue:

  • func.coalesce() is the correct SQLAlchemy 2.0 function interface
  • literal() ensures the Python config value is properly converted to a SQL literal expression, necessary for correct SQL type handling and expression evaluation

This fix is critical for the auto-deletion logic, especially where auto_delete participates in SQL comparisons (e.g., auto_delete >= 0).

Consider verifying:

  • Test coverage for autodelete_expired_users with various USERS_AUTODELETE_DAYS values (None, 0, positive integers)
  • Consistency of similar coalesce() patterns elsewhere in the file for potential follow-up improvements

@ImMohammad20000 ImMohammad20000 merged commit a0133ca into PasarGuard:dev Nov 29, 2025
10 checks passed
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.

2 participants