Skip to content

Convert MongoDB from standalone to replica set#4184

Draft
Copilot wants to merge 8 commits intomasterfrom
copilot/convert-standalone-to-replica-set
Draft

Convert MongoDB from standalone to replica set#4184
Copilot wants to merge 8 commits intomasterfrom
copilot/convert-standalone-to-replica-set

Conversation

Copy link
Contributor

Copilot AI commented Feb 26, 2026

  • Investigate existing MongoDB configuration (development and Docker/Kubernetes)
  • Create scripts/startDatabase.js to start mongod with --replSet rs0 and initialize replica set
  • Update .gitignore to include startDatabase.js exception; remove setupMongo.js exception
  • Remove scripts/setupMongo.js (functionality absorbed into startDatabase.js)
  • Update package.json to use new database start script (remove predatabase, update database)
  • Create database/init/00-replica-set.js for first-time Docker replica set initialization
  • Update Kubernetes database.yaml to add --replSet rs0 argument and MONGO_INITDB_REPLICA_HOST env var
  • Update .vscode/tasks.json run-mongo task to invoke npm run database (includes replica set init)
  • Update connection strings in appsettings.json to reference replica set (?replicaSet=rs0)
  • Update README.md MongoDB Compass connection string
  • Fix prettier formatting in scripts/startDatabase.js and .vscode/tasks.json
  • Make startDatabase.js failures fatal: register SIGINT/SIGTERM before waiting, exit(1) if mongo doesn't start or replicaset init fails, propagate mongod exit code via process.exit(exitCode ?? 1)
Original prompt

This section details on the original issue you should resolve

<issue_title>[mongodb] Convert from standalone to a replica set</issue_title>
<issue_description>We are encountering various issues with our merge operations not being atomic. They involve coordinated changes to two different collections in the database (FrontierCollection and MergeCollection).

Problem: If new words are added before old words are deleted and the latter fails, we can have duplicate GUIDs in the project Frontier. If old words are deleted before new words are added and the latter fails, entries can be lost.

One solution is to use transactions, as implemented in #3484. However, that doesn't work with out MongoDB database set up as a standalone.

TODO:

Validation:

  • Both npm start and the Docker-Kubernetes build should work.</issue_description>

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.


This change is Reviewable

Summary by CodeRabbit

  • New Features

    • Database now runs in replica set mode for improved local development behavior.
    • Added automatic replica-set initialization on first startup.
  • Documentation

    • Connection instructions updated to include replica set parameters.
  • Chores

    • Local development startup tasks and scripts updated to use the project’s package scripts and new initialization flow.

Copilot AI and others added 2 commits February 26, 2026 14:26
Co-authored-by: imnasnainaec <6411521+imnasnainaec@users.noreply.github.com>
Co-authored-by: imnasnainaec <6411521+imnasnainaec@users.noreply.github.com>
Copilot AI changed the title [WIP] Convert MongoDB from standalone to replica set Convert MongoDB from standalone to replica set Feb 26, 2026
@imnasnainaec

This comment was marked as resolved.

Co-authored-by: imnasnainaec <6411521+imnasnainaec@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 26, 2026

@copilot Remove setupMongo.js and all references to it, if it's no longer needed.

Done in 34a4308. Removed scripts/setupMongo.js (its ensureDir logic is now handled inside startDatabase.js) and dropped the !scripts/setupMongo.js exception from .gitignore.

@github-actions github-actions bot added documentation Improvements or additions to documentation backend deployment dependencies Pull requests that update a dependency file labels Feb 26, 2026
@imnasnainaec

This comment was marked as resolved.

Co-authored-by: imnasnainaec <6411521+imnasnainaec@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 26, 2026

@copilot npm run fmt-frontend-check fails.

Fixed in 46d3f10. Ran prettier --write on scripts/startDatabase.js and .vscode/tasks.json. fmt-frontend-check now passes.

@imnasnainaec

This comment was marked as resolved.

@coderabbitai

This comment was marked as outdated.

@coderabbitai
Copy link

coderabbitai bot commented Feb 26, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 46d3f10 and 36d0f2f.

📒 Files selected for processing (2)
  • .vscode/tasks.json
  • scripts/startDatabase.js
🚧 Files skipped from review as they are similar to previous changes (1)
  • scripts/startDatabase.js

📝 Walkthrough
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely describes the primary change: converting MongoDB from standalone to replica set mode.
Linked Issues check ✅ Passed The PR implements all requirements from #4183: converts MongoDB to single-member replica set, enables transactions for atomic operations, supports both npm start and Docker-Kubernetes, and follows MongoDB conversion guidance.
Out of Scope Changes check ✅ Passed All changes are directly aligned with converting MongoDB to replica set; includes startup scripts, configuration updates, and deployment manifests with no unrelated modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch copilot/convert-standalone-to-replica-set

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
deploy/helm/thecombine/charts/database/templates/database.yaml (1)

49-54: Consider parameterizing replica-set values in chart config.

rs0 and database:27017 are hardcoded here. Moving them to Helm values reduces drift risk across deployment, scripts, and connection strings.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@deploy/helm/thecombine/charts/database/templates/database.yaml` around lines
49 - 54, The template hardcodes the replica set name and host/port; change
database/templates/database.yaml to use Helm values instead of "rs0" and
"database:27017" by replacing the args entry ("--replSet", "rs0") and the env
var MONGO_INITDB_REPLICA_HOST value with templated values (e.g.,
.Values.database.replicaSetName and .Values.database.hostPort or similar), and
add corresponding defaults in values.yaml (e.g., database.replicaSetName: "rs0",
database.hostPort: "database:27017"); ensure the template uses proper Helm
quoting/template syntax so the rendered args and env.value are valid.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In @.vscode/tasks.json:
- Around line 30-38: The "run-mongo" VSCode task currently invokes "mongod"
directly with args ("--dbpath", "${workspaceFolder}/mongo_database",
"--replSet", "rs0") which enables replica-set mode but does not run
rs.initiate(), causing first-run connection failures; update the "run-mongo"
task to run the existing initialization script instead (invoke "npm run
database" or the equivalent npm script used to start and initialize the DB) so
the startup sequence performs replica set initialization automatically; keep the
task label "run-mongo" and replace the "command":"mongod" invocation with a
command that runs the npm script (ensuring the task type and args are adjusted
to execute the npm script).

In `@scripts/startDatabase.js`:
- Around line 51-66: The startup currently logs readiness/init failures and
drops mongod exit codes, allowing the process to succeed incorrectly; update the
logic around waitForMongo() and initReplicaSet() (functions referenced) to treat
failures as fatal (throw or return falsy should cause process.exit with a
non-zero code), ensure initReplicaSet() errors are awaited and checked (or
caught and cause process.exit(1)), and replace the final Promise that resolves
the mongod "close" event with logic that reads the child process exit code and
calls process.exit(code || 1) so any non-zero or unexpected termination results
in a non-zero exit; also ensure the SIGINT/SIGTERM handlers forward termination
(stop/kill mongod) but do not swallow failure—propagate error/exit accordingly.

---

Nitpick comments:
In `@deploy/helm/thecombine/charts/database/templates/database.yaml`:
- Around line 49-54: The template hardcodes the replica set name and host/port;
change database/templates/database.yaml to use Helm values instead of "rs0" and
"database:27017" by replacing the args entry ("--replSet", "rs0") and the env
var MONGO_INITDB_REPLICA_HOST value with templated values (e.g.,
.Values.database.replicaSetName and .Values.database.hostPort or similar), and
add corresponding defaults in values.yaml (e.g., database.replicaSetName: "rs0",
database.hostPort: "database:27017"); ensure the template uses proper Helm
quoting/template syntax so the rendered args and env.value are valid.

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bd576b9 and 46d3f10.

📒 Files selected for processing (9)
  • .gitignore
  • .vscode/tasks.json
  • Backend/appsettings.json
  • README.md
  • database/init/00-replica-set.js
  • deploy/helm/thecombine/charts/database/templates/database.yaml
  • package.json
  • scripts/setupMongo.js
  • scripts/startDatabase.js
💤 Files with no reviewable changes (1)
  • scripts/setupMongo.js

@imnasnainaec

This comment was marked as resolved.

Co-authored-by: imnasnainaec <6411521+imnasnainaec@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 26, 2026

@copilot
In .vscode/tasks.json around lines 30 - 38, The "run-mongo" VSCode task currently invokes "mongod" directly with args ("--dbpath", "${workspaceFolder}/mongo_database", "...

Fixed in d0942b0. The run-mongo task now runs npm run database (which executes scripts/startDatabase.js to spawn mongod and call rs.initiate()) instead of invoking mongod directly.

@imnasnainaec
Copy link
Collaborator

@copilot In @scripts/startDatabase.js around lines 51 - 66, The startup currently logs readiness/init failures and drops mongod exit codes, allowing the process to succeed incorrectly; update the logic around waitForMongo() and initReplicaSet() (functions referenced) to treat failures as fatal (throw or return falsy should cause process.exit with a non-zero code), ensure initReplicaSet() errors are awaited and checked (or caught and cause process.exit(1)), and replace the final Promise that resolves the mongod "close" event with logic that reads the child process exit code and calls process.exit(code || 1) so any non-zero or unexpected termination results in a non-zero exit; also ensure the SIGINT/SIGTERM handlers forward termination (stop/kill mongod) but do not swallow failure—propagate error/exit accordingly.

Co-authored-by: imnasnainaec <6411521+imnasnainaec@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 26, 2026

@copilot In @scripts/startDatabase.js around lines 51 - 66, The startup currently logs readiness/init failures and drops mongod exit codes, allowing the process to succeed incorrec...

Fixed in 2c9afee. Changes to scripts/startDatabase.js:

  • SIGINT/SIGTERM handlers are now registered before waitForMongo() so signals are always handled
  • waitForMongo() returning false kills mongod and calls process.exit(1)
  • initReplicaSet() failure also kills mongod and calls process.exit(1)
  • The final mongod.on("close") now calls process.exit(exitCode || 1) so any non-zero or unexpected termination propagates as a failure

Co-authored-by: imnasnainaec <6411521+imnasnainaec@users.noreply.github.com>
@codecov
Copy link

codecov bot commented Feb 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.95%. Comparing base (bd576b9) to head (36d0f2f).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4184   +/-   ##
=======================================
  Coverage   74.95%   74.95%           
=======================================
  Files         302      302           
  Lines       11090    11090           
  Branches     1394     1394           
=======================================
  Hits         8312     8312           
  Misses       2378     2378           
  Partials      400      400           
Flag Coverage Δ
backend 86.52% <ø> (ø)
frontend 65.97% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@imnasnainaec
Copy link
Collaborator

@coderabbitai review

@coderabbitai
Copy link

coderabbitai bot commented Feb 27, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend dependencies Pull requests that update a dependency file deployment documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[mongodb] Convert from standalone to a replica set

2 participants