Skip to content

Commit

Permalink
Run syncdb programmatically instead of subprocess
Browse files Browse the repository at this point in the history
  • Loading branch information
aloftus23 committed Nov 20, 2024
1 parent 5d52d56 commit 4056867
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions backend/src/xfd_django/xfd_api/tasks/run_syncdb.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
# Standard Python Libraries
import os
import subprocess

# Third-Party Libraries
import django
from django.core.management import call_command

def handler(event, context):
"""Lambda handler to trigger the Django syncdb management command."""
"""
Lambda handler to trigger syncdb.
"""
# Set the Django settings module
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xfd_django.settings")
os.environ.setdefault("PYTHONPATH", "/var/task/src/xfd_django:/var/task:/var/task/.requirements")
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"

# Initialize Django
django.setup()

# Parse arguments from the event
dangerouslyforce = event.get("dangerouslyforce", False)
populate = event.get("populate", False)

command = ["python3", "src/xfd_django/manage.py", "syncdb"]
command_args = []
if dangerouslyforce:
command.append("--dangerouslyforce")
command_args.append("--dangerouslyforce")
if populate:
command.append("--populate")
command_args.append("--populate")

try:
subprocess.run(command, check=True)
# Run the custom syncdb management command
call_command("syncdb", *command_args)
return {
"statusCode": 200,
"body": "Database synchronization completed successfully.",
}
except subprocess.CalledProcessError as e:
return {"statusCode": 500, "body": f"Database synchronization failed: {str(e)}"}
except Exception as e:
return {
"statusCode": 500,
"body": f"Database synchronization failed: {str(e)}",
}

0 comments on commit 4056867

Please sign in to comment.