Skip to content

Commit

Permalink
Call the syncdb management command inside lambda handler
Browse files Browse the repository at this point in the history
  • Loading branch information
aloftus23 committed Feb 4, 2025
1 parent c8ab1b4 commit 6c4d872
Showing 1 changed file with 3 additions and 32 deletions.
35 changes: 3 additions & 32 deletions backend/src/xfd_django/xfd_api/tasks/run_syncdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

# Set the Django settings module
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xfd_django.settings")
Expand All @@ -12,50 +13,20 @@
# Initialize Django
django.setup()

# Third-Party Libraries
from xfd_api.tasks.syncdb_helpers import (
drop_all_tables,
manage_elasticsearch_indices,
populate_sample_data,
sync_es_organizations,
synchronize,
)


def handler(event, context):
"""Trigger syncdb."""
dangerouslyforce = event.get("dangerouslyforce", False)
populate = event.get("populate", False)

try:
# Drop and recreate the database if dangerouslyforce is true
if dangerouslyforce:
print("Dropping and recreating the database...")
drop_all_tables()

# Generate and apply migrations dynamically
print("Applying migrations dynamically...")
synchronize()

# Elasticsearch Index Management
manage_elasticsearch_indices(dangerouslyforce)

# Populate Sample Data
if populate:
print("Populating the database with sample data...")
populate_sample_data()
print("Sample data population complete.")

# Step 4: Sync organizations in ES

sync_es_organizations()

call_command("syncdb", dangerouslyforce=dangerouslyforce, populate=populate)
return {
"statusCode": 200,
"body": "Database synchronization completed successfully.",
}
except Exception as e:
print("Error during syncdb: {}".format(str(e)))
print("Error during syncmdl: {}".format(str(e)))
return {
"statusCode": 500,
"body": "Database synchronization failed: {}".format(str(e)),
Expand Down

0 comments on commit 6c4d872

Please sign in to comment.