Skip to content

feat: handle .env files when setting database context#29

Merged
olivermeyer merged 2 commits into
mainfrom
feat/db-env-file
Apr 1, 2026
Merged

feat: handle .env files when setting database context#29
olivermeyer merged 2 commits into
mainfrom
feat/db-env-file

Conversation

@olivermeyer
Copy link
Copy Markdown
Collaborator

Currently set_context() only sets the database context when {PREFIX}DB_URL is set. This doesn't work when using .env files. Additionally, DatabaseSettings wasn't being passed the env files so never read from them.

This PR fixes that by

  1. Checking if the DB URL is defined in .env files
  2. Using the env files when constructing the DatabaseSettings

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@olivermeyer olivermeyer requested a review from a team as a code owner April 1, 2026 09:05
@olivermeyer olivermeyer added the claude Trigger Claude Code automation label Apr 1, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 1, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

Files with missing lines Coverage Δ
src/aignostics_foundry_core/database.py 91.03% <100.00%> (+0.73%) ⬆️
src/aignostics_foundry_core/foundry.py 97.22% <100.00%> (+0.09%) ⬆️

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@claude
Copy link
Copy Markdown

claude Bot commented Apr 1, 2026

Claude finished @olivermeyer's task in 2m 37s —— View job


Code Review Complete ✅

Summary

This PR successfully enables .env file support for database configuration. The implementation is well-designed, thoroughly tested, and ready to merge. All quality gates have passed (Codecov ✓, SonarQube ✓).

Commits Reviewed

  1. 67d7cdd - fix(foundry): detect db url in .env files in from_package
  2. 230b785 - feat(database): resolve env_file from active context in DatabaseSettings

Both commits follow conventional commit standards ✓


🎯 Strengths

1. Excellent Test Coverage

  • 100% coverage on new code (verified by Codecov)
  • All tests properly marked with @pytest.mark.integration
  • Comprehensive test scenarios covering:
    • Reading from env file via context
    • Explicit env file override
    • Error handling (no context)
    • Regression tests (OS env without env file)
    • Pool settings from env file

Files: tests/aignostics_foundry_core/database_settings_test.py:156-189, tests/aignostics_foundry_core/foundry_test.py:680-748

2. Clean Implementation

  • Proper use of python-dotenv for env file parsing
  • Efficient _any_env_file_has() helper with early termination via any()
  • Maintains backward compatibility (no breaking changes)

File: src/aignostics_foundry_core/foundry.py:274-284

3. Comprehensive Documentation

  • Excellent docstring updates explaining the new _env_file parameter
  • Clear Raises section documenting error conditions
  • Detailed Args descriptions

File: src/aignostics_foundry_core/database.py:60-76

4. Code Quality Standards

  • SonarQube S1192 compliance: string literals properly extracted to constants
    • WRONG_SQLITE_URL, MYAPP_ENV_PREFIX, MYAPP_DB_URL_KEY (database_settings_test.py:15-17)
    • ENV_FILE_ENV_KEY (foundry_test.py:26)
  • No duplicate code or magic strings ✓

💡 Suggestions (Non-Blocking)

1. Minor: Conditional super() Pattern

Location: src/aignostics_foundry_core/database.py:89-92

if _env_file is not None:
    super().__init__(_env_prefix=_env_prefix, _env_file=_env_file, **kwargs)
else:
    super().__init__(_env_prefix=_env_prefix, **kwargs)

Observation: While this pattern works correctly (pydantic-settings doesn't accept _env_file=None), it duplicates the _env_prefix and **kwargs arguments.

Alternative approach (optional):

init_kwargs = {"_env_prefix": _env_prefix, **kwargs}
if _env_file is not None:
    init_kwargs["_env_file"] = _env_file
super().__init__(**init_kwargs)  # pyright: ignore[reportCallIssue]

Verdict: Current implementation is fine. This is a stylistic preference, not a defect.

2. Note: Version Conflict

Observation: The diff shows VERSION and CHANGELOG.md reverting from 0.6.20.6.1. This suggests the branch diverged before a release was merged to main.

Recommendation: Consider rebasing on main before merging to resolve version conflicts.


✅ Validation Checklist

Based on CLAUDE.md requirements:

  • SonarQube compliance: Quality gate passed, 0 new issues
  • Test markers: All new tests have @pytest.mark.integration
  • Test coverage: 100% on new code ✓
  • Type safety: All type hints present (_env_file: list[Path] | None, etc.) ✓
  • Documentation: Comprehensive docstrings ✓
  • Conventional commits: Commit messages follow standards ✓
  • No breaking changes: Backward compatible ✓
  • Security: No hardcoded secrets, no injection vulnerabilities ✓
  • Performance: Efficient implementation with early termination ✓

🚀 Pre-Merge Recommendations

Before merging, please run:

# Verify linting (MUST pass)
mise run lint

# Verify tests
mise run test_unit && mise run test_integration

# Optional: Rebase on main to resolve version conflict
git fetch origin main
git rebase origin/main

Note: I cannot run these commands myself (lack permissions), but CI has already validated the code via GitHub Actions.


📊 Final Verdict

Status:APPROVED - Ready to Merge

Summary:

  • Zero blocking issues
  • Excellent code quality and test coverage
  • Follows all project standards (CLAUDE.md)
  • Well-documented and maintainable
  • CI passing (Codecov ✓, SonarQube ✓)

This is high-quality work that successfully solves the stated problem. Great job! 🎉


@olivermeyer olivermeyer merged commit 1bb1483 into main Apr 1, 2026
12 checks passed
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Apr 1, 2026

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

claude Trigger Claude Code automation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant