diff --git a/src/backend/management/commands/create_mongo_index.py b/src/backend/management/commands/create_mongo_index.py index a5edf0ad599c14005249e17321f6e5c5948ca930..848a24db5ff7a56d48eeea21f212be40d001bc1c 100644 --- a/src/backend/management/commands/create_mongo_index.py +++ b/src/backend/management/commands/create_mongo_index.py @@ -14,6 +14,7 @@ class Command(BaseCommand): {"key": [("name", ASCENDING)], "name": "name_1"}, {"key": [("created_at", DESCENDING)], "name": "created_at_-1"}, {"key": [("name", ASCENDING), ("created_at", DESCENDING)], "name": "name_1_created_at_-1"}, + {"key": [("context_id", HASHED), ("name", ASCENDING), ("created_at", DESCENDING)], "name": "context_name_1_created_at_-1"}, ], "statement": [ {"key": [("_id", ASCENDING)], "name": "_id_"}, @@ -25,6 +26,14 @@ class Command(BaseCommand): # Iterate through each collection and create indexes for collection_name, indexes in collections_indexes.items(): + if collection_name not in lrs_db.list_collection_names(): + self.stdout.write(self.style.WARNING( + f"Collection '{collection_name}' does not exist. Creating collection..." + )) + # Force collection creation by inserting and deleting a dummy doc + lrs_db[collection_name].insert_one({"_init": True}) + lrs_db[collection_name].delete_one({"_init": True}) + collection = lrs_db[collection_name] self.stdout.write(self.style.SUCCESS(f"Creating indexes for collection: {collection_name}")) for index in indexes: diff --git a/src/consents/tests/tests_consent_operations.py b/src/consents/tests/tests_consent_operations.py index 72887c8900856c3591b92fc17a3d4dc317f17474..93dc86920ff115f645002f4c3d376333c6d09f29 100644 --- a/src/consents/tests/tests_consent_operations.py +++ b/src/consents/tests/tests_consent_operations.py @@ -244,6 +244,7 @@ class BaseTestCase(TransactionTestCase): def setUp(self): call_command('check_and_apply_migrations') + call_command('create_mongo_index') normal_user = CustomUser.objects.create_user( self.test_user_email, self.test_user_password ) diff --git a/src/entrypoint.sh b/src/entrypoint.sh index 7be3fd29e9483da3abb52d2f41032e3658d18256..47930e539a5f6147e743816c04b22d208148390b 100644 --- a/src/entrypoint.sh +++ b/src/entrypoint.sh @@ -6,6 +6,9 @@ echo "Running database migrations and role setup..." python manage.py check_and_apply_migrations +echo "Running mongodb check..." +python manage.py create_mongo_index + echo "Starting Gunicorn server with $WORKERS workers..." exec env "$@" gunicorn \ --bind :80 \