Fix database location for package installation#14
Merged
Conversation
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.
Summary
This PR fixes a critical bug where the expense tracker application fails when installed via
uv tool install spendwise-trackerorpip install. The issue was caused by hardcoded relative paths (expense_tracker/data/transactions.db) that only work when running from the source directory.The solution migrates database storage to platform-specific user data directories following OS conventions, while maintaining full backward compatibility for existing users.
Changes
New Utilities
expense_tracker/utils/path.py - Platform-specific data directory resolution
get_data_directory(): Returns appropriate data directory based on OS (macOS:~/Library/Application Support/spendwise-tracker/, Linux:~/.local/share/spendwise-tracker/, Windows:%LOCALAPPDATA%\spendwise-tracker\)get_database_path(filename): Constructs full database file pathsexpense_tracker/utils/migration.py - Legacy database migration
migrate_legacy_databases(): Automatically copies databases from old location on first launchModified Files
expense_tracker/app.py:15-25 - Updated entry point
get_database_path()for both repositoriesCLAUDE.md - Updated documentation with new database location information
Tests
Database Locations
New Locations (Platform-Specific)
~/Library/Application Support/spendwise-tracker/~/.local/share/spendwise-tracker/%LOCALAPPDATA%\spendwise-tracker\Legacy Location
expense_tracker/data/(relative to source code directory)Migration Behavior
expense_tracker/data/to new location on first launchExample Usage
After installing via
uv tool install:$ expense-tracker Using data directory for databases. - Transactions DB: /Users/username/Library/Application Support/spendwise-tracker/transactions.db - Merchant Categories DB: /Users/username/Library/Application Support/spendwise-tracker/merchant_categories.dbThe application now works correctly regardless of where it's installed or from which directory it's launched.
Design Decisions
1. Platform-Specific Directories (No External Dependencies)
Used stdlib
pathlibandplatform.system()instead ofplatformdirslibrary to keep dependencies minimal. The logic is straightforward (~20 lines) and covers all major platforms.2. Copy (Not Move) During Migration
Migration copies legacy databases rather than moving them, reducing risk of data loss. Users can roll back to previous version if needed, and can manually delete legacy files when confident.
3. Automatic Directory Creation
Data directory is created automatically with
mkdir(parents=True, exist_ok=True), providing zero-configuration setup for users.4. No Configuration Override
Intentionally excluded environment variable or config file overrides for database location to maintain simplicity. This can be added later if there's user demand.
Testing
All tests pass, including:
Future Considerations
expense-tracker --db-pathfor debugging