MongoDB schema & indexes for messages and conversations#231
Conversation
| mongodb: | ||
| image: mongodb/mongodb-community-server:8.0-ubi8 | ||
| container_name: mongodb | ||
| ports: | ||
| - "27017:27017" | ||
| env_file: | ||
| - ./backend/.env | ||
| volumes: | ||
| - mongodb-data:/data/db | ||
|
|
||
| mongo-express: | ||
| image: mongo-express:1-20-alpine3.19 | ||
| container_name: mongo-express | ||
| ports: | ||
| - "8081:8081" | ||
| env_file: | ||
| - ./backend/.env | ||
| environment: | ||
| - ME_CONFIG_MONGODB_SERVER=mongodb | ||
| - ME_CONFIG_MONGODB_PORT=27017 | ||
| depends_on: | ||
| - mongodb |
There was a problem hiding this comment.
The PR exposes MongoDB (port 27017) and mongo-express (port 8081) on the host with no clear authentication or network restrictions. This can leak data or allow remote modification of the development database when docker-compose is started on any host.
Restrict exposure in docker-compose for non-local development (remove host ports or bind to localhost), enable authentication, and document the risk. Example change:
mongodb:
# remove "27017:27017" or change to "127.0.0.1:27017:27017" for local-only bind
ports:
- "127.0.0.1:27017:27017"
mongo-express:
ports:
- "127.0.0.1:8081:8081"
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_INITDB_ROOT_USERNAME}
- ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGO_INITDB_ROOT_PASSWORD}
Also add docs warning and ensure credentials are set via .env in CI/CD.
There was a problem hiding this comment.
Authentication is enabled by setting mongodb and mongo-exopress credentials in backend/.env, which containers load through env_file.
Originally i avoided setting those values directly in docker-compose in the way you suggested: ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_INITDB_ROOT_USERNAME}, etc, because in that case I would also need a top-level .env file next to the docker-compose file to supply those variables.
I have changed ports bindings and added docs backend/docs/services/mongodb.md.
I apologize if i have misunderstood you somewhere.
There was a problem hiding this comment.
The fixture is declared autouse=True, scope="session", so the MongoDB test DB is dropped only before and after the whole test session. Tests in tests/test_mongodb.py assume an empty DB per test (they assert count()==1). Running tests together will accumulate documents across tests and cause flakiness.
Change fixture to function-scoped or explicitly clean collections between tests. Example:
@pytest.fixture(autouse=True, scope="function")
def clean_test_mongo():
...
client.drop_database(db_name)
yield
client.drop_database(db_name)
Or drop collections at the start of each test to preserve session-level setup time if needed.
There was a problem hiding this comment.
changed scope to "function"
|
|
||
| python manage.py wait_for_db || exit 1 | ||
| python manage.py migrate --no-input || exit 1 | ||
| python manage.py migrate --database=mongodb --no-input || exit 1 |
There was a problem hiding this comment.
The script runs migrate --database=mongodb immediately after PostgreSQL migrations without ensuring the MongoDB service is ready. This can cause startup failures in environments where MongoDB takes longer to start.
Add a wait-for-mongo step (a management command or loop checking connection) before running the mongodb migrate, or rely on docker-compose depends_on with healthchecks. Example snippet:
# wait_for_mongo (pseudo)
python manage.py wait_for_mongo --timeout=60 || exit 1
python manage.py migrate --database=mongodb --no-input || exit 1
Or implement a simple loop using mongo --eval "db.adminCommand('ping')".
There was a problem hiding this comment.
added wait_for_mongodb command
MongoDBandMongo-Expressservices.mongodbtosettings.py DATABASES, configuredDATABASE_ROUTERSto avoid relations and migrations conflicts when using two databases. Configuring project to use MongoDB.conversationswith MongoDB documentsConversationandMessagewithdefault_auto_field = "django_mongodb_backend.fields.ObjectIdAutoField". Creating MongoDB documents.ConversationandMessagedocuments. Collections and Indexes are created automatically by running mongodb migrations. Adding MongoDB Indexes.pytestto usesettings_test.py. Added cleanup fixture for MongoDB tests.