Răsfoiți Sursa

mongo: initialize replica set and ensure Feature Compatibility Version

Yuki Takei 6 zile în urmă
părinte
comite
db01385659
1 a modificat fișierele cu 23 adăugiri și 2 ștergeri
  1. 23 2
      .devcontainer/compose.yml

+ 23 - 2
.devcontainer/compose.yml

@@ -18,6 +18,7 @@ services:
   mongo:
   mongo:
     image: mongo:8.2
     image: mongo:8.2
     restart: unless-stopped
     restart: unless-stopped
+    command: ["--replSet", "rs0", "--bind_ip_all"]
     ports:
     ports:
       - 27017
       - 27017
     volumes:
     volumes:
@@ -28,8 +29,9 @@ services:
       timeout: 5s
       timeout: 5s
       retries: 20
       retries: 20
 
 
-  # Ensures MongoDB Feature Compatibility Version matches the mongo image.
-  # Required when the mongo image is upgraded while existing data persists in the volume.
+  # Initializes the replica set and ensures Feature Compatibility Version matches the mongo image.
+  # The replica set (single node `rs0`) is required for transactions and change streams used in development.
+  # FCV update is required when the mongo image is upgraded while existing data persists in the volume.
   # https://www.mongodb.com/ja-jp/docs/upcoming/release-notes/8.2-upgrade-standalone/
   # https://www.mongodb.com/ja-jp/docs/upcoming/release-notes/8.2-upgrade-standalone/
   mongo-init:
   mongo-init:
     image: mongo:8.2
     image: mongo:8.2
@@ -44,6 +46,25 @@ services:
       - mongo:27017
       - mongo:27017
       - --eval
       - --eval
       - |
       - |
+        // 1. Initiate the replica set (single node) if not already initialized.
+        try {
+          const status = rs.status();
+          print(`Replica set already initialized: $${status.set}`);
+        } catch (e) {
+          if (e.codeName === 'NotYetInitialized') {
+            print('Initiating replica set rs0...');
+            rs.initiate({ _id: 'rs0', members: [{ _id: 0, host: 'mongo:27017' }] });
+            // Wait until this node is elected primary so subsequent admin commands succeed.
+            while (!db.hello().isWritablePrimary) {
+              sleep(500);
+            }
+            print('Replica set initiated and primary is ready');
+          } else {
+            throw e;
+          }
+        }
+
+        // 2. Ensure FCV matches the mongo image.
         const target = '8.2';
         const target = '8.2';
         const result = db.adminCommand({ getParameter: 1, featureCompatibilityVersion: 1 });
         const result = db.adminCommand({ getParameter: 1, featureCompatibilityVersion: 1 });
         if (result.featureCompatibilityVersion.version === target) {
         if (result.featureCompatibilityVersion.version === target) {