Skip to content

Fix database URL null check issue and add missing ENV setting #450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Mirza-Samad-Ahmed-Baig
Copy link

@Mirza-Samad-Ahmed-Baig Mirza-Samad-Ahmed-Baig commented Jul 25, 2025

Fix: Critical Database URL Null Reference Bug

Problem

The application was experiencing a critical startup failure due to a null reference bug in the database configuration. Specifically:

  1. Database URL Null Check Issue: In apps/backend/app/core/database.py, the code attempted to call .startswith("sqlite") on potentially None values (SYNC_DATABASE_URL and ASYNC_DATABASE_URL) without proper null checking, causing AttributeError at application startup.

  2. Missing ENV Configuration: The setup_logging() function referenced a non-existent ENV attribute in the Settings class, which could lead to improper logging configuration.

Solution

  • Added null safety: Provided default SQLite database URLs when environment variables are not set
  • Added missing ENV attribute: Added ENV: str = "production" to the Settings class with a sensible default
  • Improved robustness: The application now starts successfully even without specific environment configuration

Changes Made

  1. apps/backend/app/core/config.py:

    • Added default None values for optional database URL settings
    • Added missing ENV attribute with "production" default
  2. apps/backend/app/core/database.py:

    • Added fallback default values for database URLs using the or operator
    • Ensures SQLite defaults are used when environment variables are not configured

Impact

  • Prevents startup crashes when environment variables are missing
  • Improves developer experience by providing working defaults
  • Production ready - handles edge cases gracefully
  • Backward compatible - existing configurations continue to work

Testing

The fix ensures the application can start successfully in environments where database URLs are not explicitly configured, while maintaining compatibility with existing setups.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Summary by CodeRabbit

  • New Features

    • Introduced a new environment configuration option to specify the application environment (defaults to "production").
  • Improvements

    • Configuration fields for database URLs and session secret key now have explicit default values, improving clarity and reliability.
    • If database URLs are not set, the system now automatically uses default SQLite connection strings.

Copy link
Contributor

coderabbitai bot commented Jul 25, 2025

Walkthrough

The changes explicitly assign default values of None to optional configuration fields in the settings module and introduce a new environment field with a default of "production". In the database module, fallback SQLite connection strings are provided for database URLs if not set in the configuration.

Changes

File(s) Change Summary
.../core/config.py Assigned None as default to optional config fields; added new ENV field with "production" default.
.../core/database.py Updated database URL settings to use SQLite defaults if config values are unset or falsy.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~6 minutes

Poem

A whisk of code, a dash of care,
Defaults set gently, floating in air.
SQLite awaits if configs are bare,
"Production" whispers, everywhere.
With settings snug and secrets tight,
This bunny hops in code delight!
🐇✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.

✨ 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.
    • Explain this complex logic.
    • 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. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • 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 src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

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

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai 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

Documentation and Community

  • 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
Contributor

@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)
apps/backend/app/core/config.py (1)

48-48: Consider removing the redundant hasattr check.

Since the ENV attribute is now always present in the Settings class (line 15), the hasattr(settings, "ENV") check is no longer necessary and can be simplified.

-    env = settings.ENV.lower() if hasattr(settings, "ENV") else "production"
+    env = settings.ENV.lower()
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 294d52a and ebde30e.

📒 Files selected for processing (2)
  • apps/backend/app/core/config.py (1 hunks)
  • apps/backend/app/core/database.py (1 hunks)
🔇 Additional comments (2)
apps/backend/app/core/config.py (1)

12-15: LGTM! Good fix for null safety.

The explicit None defaults for the optional database URL settings and the addition of the ENV field with a sensible default resolve the null reference issues mentioned in the PR objectives. This ensures the application can start even without explicit environment configuration.

apps/backend/app/core/database.py (1)

23-24: Excellent null safety implementation.

The fallback mechanism using the or operator elegantly solves the original AttributeError issue. When settings.SYNC_DATABASE_URL or settings.ASYNC_DATABASE_URL are None (as set in config.py), these will default to appropriate SQLite connection strings, ensuring the .startswith("sqlite") call on line 28 always operates on a valid string.

The SQLite fallback URLs are correctly formatted:

  • sqlite:///./app.db for synchronous operations
  • sqlite+aiosqlite:///./app.db for asynchronous operations

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.

1 participant