Skip to content

Conversation

@AchrefHemissi
Copy link

@AchrefHemissi AchrefHemissi commented Dec 3, 2025

  • Replace complex inline bash script with dedicated init script
  • Use MongoDB built-in /docker-entrypoint-initdb.d/ mechanism
  • Create scripts/mongo-init.sh for user creation
  • Improve readability and maintainability

Fixes #3632

Copilot AI review requested due to automatic review settings December 3, 2025 16:18
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Dec 3, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Dec 3, 2025

💕 Thank you for your contribution and please kindly read and sign our CLA. CLA Docs


I have read the CLA Document and I hereby sign the CLA


You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors MongoDB initialization in docker-compose by replacing a complex inline bash script with a dedicated initialization script. The changes leverage MongoDB's built-in /docker-entrypoint-initdb.d/ mechanism for better maintainability and readability.

Key Changes:

  • Created scripts/mongo-init.sh for MongoDB user initialization
  • Simplified MongoDB service configuration by removing inline bash command
  • Cleaned up Redis command formatting for consistency
  • Removed extraneous whitespace in docker-compose.yml

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
scripts/mongo-init.sh New dedicated script for creating MongoDB OpenIM user using mongosh
docker-compose.yml Simplified MongoDB service by mounting init script, refactored command structure, and cleaned up formatting

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 8 to 13
db.createUser({
user: "${MONGO_OPENIM_USERNAME}",
pwd: "${MONGO_OPENIM_PASSWORD}",
roles: [{role: "readWrite", db: "${MONGO_INITDB_DATABASE}"}]
})
print("OpenIM user created successfully")
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The MongoDB init script doesn't check if the user already exists before attempting to create it. Unlike the original inline script which included a !db.getUser() check, this new script will fail on subsequent container restarts when the data volume persists and the user already exists.

Consider adding idempotency by checking if the user exists first:

use ${MONGO_INITDB_DATABASE}
if (!db.getUser("${MONGO_OPENIM_USERNAME}")) {
  db.createUser({
    user: "${MONGO_OPENIM_USERNAME}",
    pwd: "${MONGO_OPENIM_PASSWORD}",
    roles: [{role: "readWrite", db: "${MONGO_INITDB_DATABASE}"}]
  })
  print("OpenIM user created successfully")
} else {
  print("User already exists: ${MONGO_OPENIM_USERNAME}")
}
Suggested change
db.createUser({
user: "${MONGO_OPENIM_USERNAME}",
pwd: "${MONGO_OPENIM_PASSWORD}",
roles: [{role: "readWrite", db: "${MONGO_INITDB_DATABASE}"}]
})
print("OpenIM user created successfully")
if (!db.getUser("${MONGO_OPENIM_USERNAME}")) {
db.createUser({
user: "${MONGO_OPENIM_USERNAME}",
pwd: "${MONGO_OPENIM_PASSWORD}",
roles: [{role: "readWrite", db: "${MONGO_INITDB_DATABASE}"}]
})
print("OpenIM user created successfully")
} else {
print("User already exists: ${MONGO_OPENIM_USERNAME}")
}

Copilot uses AI. Check for mistakes.
- Replace complex inline bash script with dedicated init script
- Use MongoDB built-in /docker-entrypoint-initdb.d/ mechanism
- Create scripts/mongo-init.sh for user creation
- Improve readability and maintainability

Fixes openimsdk#3632
@AchrefHemissi AchrefHemissi force-pushed the feat-simplify-mongodb-init branch from 49a61f8 to 686366b Compare December 3, 2025 16:38
@github-actions github-actions bot locked and limited conversation to collaborators Dec 3, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE REQUEST] Simplify MongoDB initialization in docker-compose

1 participant