Skip to content

Commit

Permalink
Add load data step
Browse files Browse the repository at this point in the history
  • Loading branch information
arkid15r committed Sep 12, 2024
1 parent c3b7bea commit fb3b5cf
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ index:
@CMD="poetry run python manage.py algolia_reindex" $(MAKE) exec-backend-command

load-data:
@CMD="poetry run python manage.py loaddata data/nest.json" $(MAKE) exec-backend-command
@CMD="poetry run python manage.py load_data" $(MAKE) exec-backend-command

merge-migrations:
@CMD="poetry run python manage.py makemigrations --merge" $(MAKE) exec-backend-command
Expand Down
33 changes: 33 additions & 0 deletions backend/apps/common/management/commands/load_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""A command to load OWASP Nest data."""

import contextlib

from algoliasearch_django import register, unregister
from algoliasearch_django.registration import RegistrationError
from django.apps import apps
from django.core.management import call_command
from django.core.management.base import BaseCommand
from django.db import transaction


class Command(BaseCommand):
help = "Load OWASP Nest data."

def handle(self, *_args, **_options):
nest_apps = ("github", "owasp")

# Disable indexing
for nest_app in nest_apps:
for model in apps.get_app_config(nest_app).get_models():
with contextlib.suppress(RegistrationError):
unregister(model)

# Run loaddata
with transaction.atomic():
call_command("loaddata", "data/nest.json", "-v", "3")

# Enable indexing
for nest_app in nest_apps:
for model in apps.get_app_config(nest_app).get_models():
with contextlib.suppress(RegistrationError):
register(model)
28 changes: 7 additions & 21 deletions backend/apps/common/management/commands/purge_data.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
"""A command to purge OWASP Nest data."""

from django.apps import apps
from django.core.management.base import BaseCommand
from django.db import connection

from apps.github.models import Issue, Label, Organization, Release, Repository, User
from apps.owasp.models import Chapter, Committee, Event, Project

BATCH_SIZE = 10


class Command(BaseCommand):
help = "Purge OWASP Nest data."

def handle(self, *_args, **options):
with connection.cursor() as cursor:
models = (
Chapter,
Committee,
Event,
Issue,
Label,
Organization,
Project,
Release,
Repository,
User,
)
nest_apps = ("github", "owasp")

for model in models:
cursor.execute(f"TRUNCATE TABLE {model._meta.db_table} CASCADE") # noqa: SLF001
print(f"Purged GitHub {model._meta.verbose_name_plural}") # noqa: SLF001
with connection.cursor() as cursor:
for nest_app in nest_apps:
for model in apps.get_app_config(nest_app).get_models():
cursor.execute(f"TRUNCATE TABLE {model._meta.db_table} CASCADE") # noqa: SLF001
print(f"Purged GitHub {model._meta.verbose_name_plural}") # noqa: SLF001
16 changes: 8 additions & 8 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ services:
env_file: backend/.env/local
environment:
DJANGO_DB_HOST: ${DJANGO_DB_HOST:-db}
DJANGO_DB_NAME: ${DJANGO_DB_NAME:-nest_dev}
DJANGO_DB_PASSWORD: ${DJANGO_DB_PASSWORD:-nest_dev_password}
DJANGO_DB_NAME: ${DJANGO_DB_NAME:-nest_db_dev}
DJANGO_DB_PASSWORD: ${DJANGO_DB_PASSWORD:-nest_user_dev_password}
DJANGO_DB_PORT: ${DJANGO_DB_PORT:-5432}
DJANGO_DB_USER: ${DJANGO_DB_USER:-nest_user}
DJANGO_DB_USER: ${DJANGO_DB_USER:-nest_user_dev}
ports:
- '127.0.0.1:8000:8000'
volumes:
Expand All @@ -29,18 +29,18 @@ services:
container_name: nest-db
image: postgres:16.4
environment:
POSTGRES_DB: ${DJANGO_DB_NAME:-nest_dev}
POSTGRES_PASSWORD: ${DJANGO_DB_PASSWORD:-nest_dev_password}
POSTGRES_USER: ${DJANGO_DB_USER:-nest_user}
POSTGRES_DB: ${DJANGO_DB_NAME:-nest_db_dev}
POSTGRES_PASSWORD: ${DJANGO_DB_PASSWORD:-nest_user_dev_password}
POSTGRES_USER: ${DJANGO_DB_USER:-nest_user_dev}
healthcheck:
test:
[
'CMD',
'pg_isready',
'-U',
'${DJANGO_DB_USER:-nest_user}',
'${DJANGO_DB_USER:-nest_user_dev}',
'-d',
'${DJANGO_DB_NAME:-nest_dev}',
'${DJANGO_DB_NAME:-nest_db_dev}',
]
interval: 5s
timeout: 5s
Expand Down

0 comments on commit fb3b5cf

Please sign in to comment.