From 48ea0eb48e1a74904451b974bee6ea87f39d868b Mon Sep 17 00:00:00 2001 From: badbl0cks <4161747+badbl0cks@users.noreply.github.com> Date: Sun, 1 Jun 2025 19:05:56 -0700 Subject: [PATCH] fix(dev): Resolve Dev Debug Environment Issues and Streamline Local Setup This commit comprehensively addresses issues with the local development and debugging environment, ensuring a smoother and more reliable developer experience. Key changes include: - **VSCode Debugger:** Corrected launch configuration (`.vscode/launch.json`) to properly run the Django development server with debugging enabled. It now correctly sets `DEBUG=True`, uses `0.0.0.0:8000`, and specifies the correct working directory. - **Docker Compose:** Exposed the PostgreSQL port (`5432:5432`) in `docker-compose.yml` to allow direct connections from the host, facilitating local development and debugging without needing to run the full application stack. - **Environment Variables:** - Updated `.gitignore` to ignore all `.env.*` files, allowing for environment-specific configurations. - Modified `src/pkmntrade_club/django_project/settings.py` to use `localhost` for `DJANGO_DATABASE_URL` and `REDIS_URL` by default, aligning with the exposed Docker services for easier local development. Default `DISABLE_SIGNUPS` and `DISABLE_CACHE` are now `True` for a more typical local dev setup. - **Management Commands & Scripts:** - Adjusted `manage.py` to correctly append the project's root directory to `sys.path`, resolving potential import issues when running management commands. - Significantly improved `scripts/reset-db_make-migrations_seed-data.sh`: - Removed reliance on sourcing `.env` directly. - Ensured the database service (`db`) is started independently before migrations. - Added explicit steps for running `prebuild.sh`, migrations, and `collectstatic`. - Switched to using `uv run manage.py loaddata` for seeding, which is more consistent with the project's tooling. - **Django Settings:** Added `SESSION_ENGINE = "django.contrib.sessions.backends.signed_cookies"` and `SESSION_COOKIE_HTTPONLY = True` for improved session management and security. These changes collectively fix the previously problematic development setup, enabling straightforward debugging and a more efficient workflow for local development. --- .gitignore | 2 +- .vscode/launch.json | 8 ++++++-- docker-compose.yml | 2 ++ manage.py | 2 ++ scripts/reset-db_make-migrations_seed-data.sh | 19 ++++++++++--------- src/pkmntrade_club/django_project/settings.py | 13 ++++++++----- 6 files changed, 29 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 136b20f..867f999 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -.env +.env.* src/pkmntrade_club/staticfiles/* !src/pkmntrade_club/staticfiles/.gitkeep src/pkmntrade_club/media/* diff --git a/.vscode/launch.json b/.vscode/launch.json index 340195f..05cae64 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,10 +6,14 @@ "type": "debugpy", "request": "launch", "program": "${workspaceFolder}/manage.py", - "args": ["runserver"], + "cwd": "${workspaceFolder}", + "args": ["runserver", "0.0.0.0:8000"], "django": true, "justMyCode": true, - "preLaunchTask": "Run db standalone" + "preLaunchTask": "Run db standalone", + "env": { + "DEBUG": "True" + }, } ] } \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 40ea63c..ba0311a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -39,6 +39,8 @@ services: db: image: postgres:16 restart: always + ports: + - 5432:5432 volumes: - postgres_data:/var/lib/postgresql/data/ environment: diff --git a/manage.py b/manage.py index 696a354..138d284 100755 --- a/manage.py +++ b/manage.py @@ -6,6 +6,8 @@ import sys def main(): """Run administrative tasks.""" os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pkmntrade_club.django_project.settings") + sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + try: from django.core.management import execute_from_command_line except ImportError as exc: diff --git a/scripts/reset-db_make-migrations_seed-data.sh b/scripts/reset-db_make-migrations_seed-data.sh index 5ee4d66..407de29 100755 --- a/scripts/reset-db_make-migrations_seed-data.sh +++ b/scripts/reset-db_make-migrations_seed-data.sh @@ -4,22 +4,23 @@ set -e echo "Remaking migrations..." find . -path "*/migrations/0*.py" -delete -set -a -source .env -set +a uv run manage.py makemigrations --noinput echo "Resetting dev database... " docker compose down \ && docker volume rm -f pkmntradeclub_postgres_data \ - && ./scripts/rebuild-and-run.sh + && docker compose up -d db + +echo "Running prebuild..." +./scripts/prebuild.sh + +echo "Running migrations..." +uv run manage.py migrate --noinput -# Wait for the database to be ready. -echo "Waiting 15 seconds for migrations to be auto-run..." -sleep 15 echo "Loading seed data..." -docker compose exec -it web bash -c "django-admin loaddata /seed/0*" +uv run manage.py loaddata ./seed/0* -docker compose down +echo "Running collectstatic..." +uv run manage.py collectstatic -c --no-input echo "Done!" diff --git a/src/pkmntrade_club/django_project/settings.py b/src/pkmntrade_club/django_project/settings.py index c10dd53..270ecdc 100644 --- a/src/pkmntrade_club/django_project/settings.py +++ b/src/pkmntrade_club/django_project/settings.py @@ -5,12 +5,12 @@ import os import logging import sys -# set default values to dev values for environment variables +# set default values to local dev values env = environ.Env( DEBUG=(bool, False), # MUST STAY FALSE FOR DEFAULT FOR SECURITY REASONS (e.g. if app can't access .env, prevent showing debug output) - DISABLE_SIGNUPS=(bool, False), - DISABLE_CACHE=(bool, False), - DJANGO_DATABASE_URL=(str, 'postgresql://postgres@db:5432/postgres?sslmode=disable'), + DISABLE_SIGNUPS=(bool, True), + DISABLE_CACHE=(bool, True), + DJANGO_DATABASE_URL=(str, 'postgresql://postgres@localhost:5432/postgres?sslmode=disable'), DJANGO_EMAIL_HOST=(str, ''), DJANGO_EMAIL_PORT=(int, 587), DJANGO_EMAIL_USER=(str, ''), @@ -22,7 +22,7 @@ env = environ.Env( PUBLIC_HOST=(str, 'localhost'), ACCOUNT_EMAIL_VERIFICATION=(str, 'none'), SCHEME=(str, 'http'), - REDIS_URL=(str, 'redis://redis:6379'), + REDIS_URL=(str, 'redis://localhost:6379'), CACHE_TIMEOUT=(int, 604800), TIME_ZONE=(str, 'America/Los_Angeles'), ) @@ -366,6 +366,9 @@ SOCIALACCOUNT_EMAIL_AUTHENTICATION = False SOCIALACCOUNT_EMAIL_AUTHENTICATION_AUTO_CONNECT = False SOCIALACCOUNT_ONLY = False +SESSION_ENGINE = "django.contrib.sessions.backends.signed_cookies" +SESSION_COOKIE_HTTPONLY = True + # auto-detection doesn't work properly sometimes, so we'll just use the DEBUG setting DEBUG_TOOLBAR_CONFIG = {"SHOW_TOOLBAR_CALLBACK": lambda request: DEBUG}