fix(db): strip whitespace from DATABASE_URL to prevent SQLAlchemy crash at startup#292
Open
kapil971390 wants to merge 1 commit into
Open
Conversation
os.getenv("DATABASE_URL") with a whitespace-only value (e.g. " ",
"\t", "\n") is truthy in Python, so the old guard silently passed the
raw string to SQLAlchemy's create_engine — causing an immediate
connection crash at startup.
Edge cases verified:
- " " (spaces) → truthy, was returned as-is → SQLAlchemy crash
- "\t" (tab) → truthy, was returned as-is → SQLAlchemy crash
- "\n" (newline) → truthy, was returned as-is → SQLAlchemy crash
- "" (empty) → falsy → SQLite fallback ✓ (already safe)
- unset → None → falsy → SQLite fallback ✓ (already safe)
Fix: call .strip() in both the guard and the return value so any
whitespace-only DATABASE_URL falls through to the SQLite default.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
_database_url()inBackend/db.pyused a bare truthiness check on the value returned byos.getenv("DATABASE_URL"). In Python a whitespace-only string (" ","\t","\n") is truthy, so the guard passed that raw value straight intocreate_engine()— which immediately crashes with an invalid connection URL.Root cause
Fix
Both the guard and the return value now strip surrounding whitespace, so any whitespace-only
DATABASE_URLfalls through to the safe SQLite default.Edge cases verified
DATABASE_URLvalue" "(spaces)"\t"(tab)"\n"(newline)""(empty string)Impact
DATABASE_URLvaluesDATABASE_URLis accidentally set to whitespace (e.g. trailing space in.env)