Skip to content

Commit

Permalink
chore:Add DATABASE_URL and refactor tasks and migration
Browse files Browse the repository at this point in the history
Introduce DATABASE_URL environment variable for local PostgreSQL setup in Docker and Makefile. Update `rebuild_cache` task with a specific name and adjust its schedule. Refactor Django settings and Makefile commands for improved database management consistency.
  • Loading branch information
hareshkainthdbt committed Dec 10, 2024
1 parent 9c27d17 commit 3522a93
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@
"filename": "Makefile",
"hashed_secret": "afc848c316af1a89d49826c5ae9d00ed769415f3",
"is_verified": false,
"line_number": 102
"line_number": 65
}
]
},
"generated_at": "2024-09-09T12:08:54Z"
"generated_at": "2024-12-10T03:50:08Z"
}
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ first-use: # Initialise for local execution
@echo "$(COLOUR_GREEN)Destroy containers with 'make down'$(COLOUR_NONE)"

up: # Build, (re)create and start containers
export DATABASE_URL=postgres://postgres:postgres@localhost:5432/fbr
docker compose up -d
@echo "$(COLOUR_GREEN)Services are up - use 'make logs' to view service logs$(COLOUR_NONE)"

Expand Down Expand Up @@ -107,10 +108,12 @@ django-shell-local: # Run a Django shell (local django instance)
poetry run python manage.py shell

migrate: # Run Django migrate
docker compose run --rm web poetry run python manage.py migrate --noinput
export DATABASE_URL=postgres://postgres:postgres@localhost:5432/fbr && \
python manage.py migrate

migrations: # Run Django makemigrations
docker compose run --rm web poetry run python manage.py makemigrations --noinput
export DATABASE_URL=postgres://postgres:postgres@localhost:5432/fbr && \
python manage.py makemigrations

lint: # Run all linting
make black
Expand All @@ -136,6 +139,7 @@ setup_local: # Set up the local environment
@echo "$(COLOUR_GREEN)Running initial setup for local environment...$(COLOUR_NONE)"
$(MAKE) first-use
$(MAKE) start
#$(MAKE) migrate
#$(MAKE) migrations
$(MAKE) migrate
#$(MAKE) rebuild_cache
@echo "$(COLOUR_GREEN)Local setup complete.$(COLOUR_NONE)"
4 changes: 2 additions & 2 deletions celery_worker/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from app.search.utils.documents import clear_all_documents


@shared_task(bind=True)
def rebuild_cache() -> None:
@shared_task(name="celery_worker.tasks.rebuild_cache")
def rebuild_cache():
"""
Rebuilds the cache for search documents across various components by
clearing all existing documents. The process is timed, and the
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ services:
DEBUG: true
DJANGO_SETTINGS_MODULE: fbr.settings
RDS_POSTGRES_CREDENTIALS: '{"password":"postgres","dbname":"fbr","engine":"postgres","port":5432,"dbInstanceIdentifier":"xxx","host":"db","username":"postgres"}'
DATABASE_URL: postgres://postgres:[email protected]:5432/fbr # pragma: allowlist secret

celery-beats:
build:
Expand Down
2 changes: 1 addition & 1 deletion fbr/celery_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
celery_app.conf.beat_schedule = {
"schedule-fbr-cache-task": {
"task": "celery_worker.tasks.rebuild_cache",
"schedule": crontab(hour="1", minute="0"), # Runs daily at 1:00 AM
"schedule": crontab(hour="3", minute="46"), # Runs daily at 1:00 AM
},
}
3 changes: 2 additions & 1 deletion fbr/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"app",
"app.core",
"app.search",
"celery_worker",
]

THIRD_PARTY_APPS: list = [
Expand Down Expand Up @@ -120,7 +121,7 @@
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": SITE_ROOT / "db.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}

Expand Down

0 comments on commit 3522a93

Please sign in to comment.