#!/usr/bin/env node // db-eval.mjs — small MongoDB/FerretDB admin helper built on the `mongodb` driver // that WeKan already bundles, so the snap needs NO `mongosh` binary. Run it with // the bundled Node and NODE_PATH pointing at the WeKan bundle's node_modules so // `mongodb` resolves (same pattern as migrate-mongo3-to-ferretdb.mjs); the // db-eval wrapper script does this for you. // // Works against FerretDB v1 (SQLite) and MongoDB 6/7 alike — both speak the // MongoDB wire protocol. Pass ?directConnection=true in the URL so admin commands // reach a specific node even before a replica set is initiated. // // Usage: node db-eval.mjs [host] // ping exit 0 if the server answers adminCommand({ping:1}) // primary exit 0 if this node is a writable replica-set primary // rs-conf-host print "OK:" of member[0], else "ERROR:" // rs-initiate replSetInitiate rs0 with a single member // rs-reconfig force replSetReconfig rs0 to a single member // shutdown best-effort admin shutdownServer // product-name print the Admin Panel product name (settings.productName), if set // evidence print JSON: per-collection counts + the newest timestamp in the data // Resolve the mongodb driver via createRequire (CommonJS), NOT a bare ESM // `import { MongoClient } from 'mongodb'`. Node's ESM loader IGNORES NODE_PATH, but the // db-eval wrapper sets NODE_PATH=$SNAP/programs/server/node_modules to point at the bundle's // driver — so the ESM import resolved nothing and db-eval exited BEFORE connecting. Every // ping/primary/rs-* check therefore "failed", which made mongod-7 migration readiness fall // back to MongoDB 3.2, made WeKan loop "MongoDB not ready" / "FerretDB not ready" forever, and // made replica-set setup fail. createRequire's require() DOES honor NODE_PATH and the bundle // layout; anchor inside the modern bundle (npm-mongo's v6 driver) first, then NODE_PATH/local. import { createRequire } from 'node:module'; import { pathToFileURL } from 'node:url'; const _roots = []; const _push = (u) => { if (u) { try { _roots.push(new URL(u)); } catch {} } }; if (process.env.SNAP) _push(pathToFileURL(process.env.SNAP + '/')); if (process.env.NODE_PATH) _push(pathToFileURL(process.env.NODE_PATH.split(':')[0].replace(/\/programs\/server\/node_modules\/?$/, '') + '/')); _push(new URL('../', import.meta.url)); // $SNAP, if this is $SNAP/bin/db-eval.mjs const _subPaths = [ 'programs/server/npm/node_modules/meteor/npm-mongo/node_modules/_.cjs', 'programs/server/npm/node_modules/_.cjs', 'programs/server/node_modules/_.cjs', '_.cjs', ]; const _reqs = []; for (const r of _roots) for (const sp of _subPaths) { try { _reqs.push(createRequire(new URL(sp, r))); } catch {} } // createRequire(import.meta.url) uses CommonJS resolution, which honors NODE_PATH and walks up // node_modules from this file — the reliable fallback. _reqs.push(createRequire(import.meta.url)); let MongoClient; for (const req of _reqs) { try { const m = req('mongodb'); if (m && typeof m.MongoClient === 'function') { MongoClient = m.MongoClient; break; } } catch {} } if (typeof MongoClient !== 'function') { console.error('db-eval: FATAL — could not resolve the mongodb driver from the WeKan bundle.'); process.exit(1); } const [cmd, url, host] = process.argv.slice(2); if (!cmd || !url) { console.error('usage: db-eval [host]'); process.exit(2); } const rsConfig = (h) => ({ _id: 'rs0', members: [{ _id: 0, host: h }] }); const client = new MongoClient(url, { serverSelectionTimeoutMS: 5000, connectTimeoutMS: 5000, }); let code = 1; try { await client.connect(); const admin = client.db('admin').admin(); switch (cmd) { case 'ping': { const r = await admin.command({ ping: 1 }); code = r.ok === 1 ? 0 : 1; break; } case 'kind': { // WHICH database is answering here - 'ferretdb' or 'mongodb'. Both speak // the MongoDB wire protocol on the same port (the snap runs one at a // time), so the port cannot say, and #6583's comparison has to know: it // reads whichever one is already RUNNING rather than starting a second // copy of it, and reading FerretDB's evidence as MongoDB's would be worse // than reading neither. // // `buildInfo` is the answer both give: FerretDB names itself there (and // in the `ferretdb` field it adds), a real mongod does not. const info = await admin.command({ buildInfo: 1 }); const text = JSON.stringify(info || {}).toLowerCase(); process.stdout.write(text.includes('ferretdb') ? 'ferretdb' : 'mongodb'); code = 0; break; } case 'primary': { // Equivalent of the old `rs.status().ok === 1 && rs.isMaster().ismaster`. const s = await admin.command({ replSetGetStatus: 1 }); const h = await admin.command({ hello: 1 }); code = s.ok === 1 && h.isWritablePrimary ? 0 : 1; break; } case 'rs-conf-host': { const c = await admin.command({ replSetGetConfig: 1 }); console.log('OK:' + c.config.members[0].host); code = 0; break; } case 'rs-initiate': { if (!host) { console.error('rs-initiate needs a host argument'); code = 2; break; } await admin.command({ replSetInitiate: rsConfig(host) }); console.log('Replica set initialized.'); code = 0; break; } case 'rs-reconfig': { if (!host) { console.error('rs-reconfig needs a host argument'); code = 2; break; } let version = 1; try { const c = await admin.command({ replSetGetConfig: 1 }); version = (c.config.version || 1) + 1; } catch { /* not yet configured */ } await admin.command({ replSetReconfig: { ...rsConfig(host), version }, force: true }); console.log('Replica set reconfigured.'); code = 0; break; } case 'shutdown': { // The server drops the connection while shutting down, which surfaces as an // error here — that is the expected, successful path. try { await admin.command({ shutdown: 1 }); } catch { /* expected */ } code = 0; break; } case 'product-name': { // Print the Admin Panel product name if set, so callers can cache it for the // maintenance page (which runs when the database is stopped). Nothing printed if unset. try { const doc = await client.db('wekan').collection('settings') .findOne({ productName: { $type: 'string' } }); if (doc && typeof doc.productName === 'string' && doc.productName.trim()) { console.log(doc.productName.trim()); } } catch { /* ignore — leave output empty */ } code = 0; break; } case 'evidence': { // #6583/#6585: WHICH of two databases holds the work. When both MongoDB and // the migrated FerretDB have been written to since the migration, a file // timestamp cannot answer that - it says when a file was touched, not what // is in it. This asks the database itself, and prints JSON for // bin/database-choose.mjs to compare: // // counts documents per collection, so "one side has everything the // other has, and more" can be recognised // newest the newest timestamp any of them carries, so "which one has // been used more recently" is answered by DATA rather than by // an mtime that a service start also moves // // Read-only, and bounded: countDocuments plus one indexed-ish sort per // timestamped collection. const db = client.db('wekan'); const out = { counts: {}, newest: null, newestFrom: null }; const COLLECTIONS = ['boards', 'lists', 'swimlanes', 'cards', 'checklists', 'checklistItems', 'card_comments', 'activities', 'users', 'attachments']; for (const name of COLLECTIONS) { try { out.counts[name] = await db.collection(name).countDocuments(); } catch { /* a collection that does not exist counts as absent, not as 0 */ } } // The newest moment either database can show. dateLastActivity is what the // board view sorts by; createdAt covers a database whose cards predate it. const TIMESTAMPS = [ ['cards', 'dateLastActivity'], ['cards', 'modifiedAt'], ['cards', 'createdAt'], ['activities', 'createdAt'], ['card_comments', 'createdAt'], ['checklists', 'createdAt'], ['boards', 'modifiedAt'], ]; for (const [name, field] of TIMESTAMPS) { try { const doc = await db.collection(name) .find({ [field]: { $type: 'date' } }, { projection: { [field]: 1 } }) .sort({ [field]: -1 }).limit(1).next(); const value = doc && doc[field]; if (value instanceof Date && !Number.isNaN(value.getTime())) { const ms = value.getTime(); if (out.newest === null || ms > out.newest) { out.newest = ms; out.newestFrom = `${name}.${field}`; } } } catch { /* absent collection or field: nothing to learn here */ } } console.log(JSON.stringify(out)); code = 0; break; } default: console.error('unknown command: ' + cmd); code = 2; } } catch (e) { // rs-conf-host mirrors the old mongosh helper: emit ERROR: and exit 0 so // the caller branches on the printed value, not the exit code. if (cmd === 'rs-conf-host') { console.log('ERROR:' + (e && e.message ? e.message : String(e))); code = 0; } else { // #6746: say WHY. Every caller that polls redirects stderr, and the one // that does not (bin/database-ready) shows it, so "permission denied" and // "connection refused" stop looking identical. console.error('db-eval ' + cmd + ': ' + (e && e.message ? e.message : String(e))); code = 1; } } finally { try { await client.close(true); } catch { /* ignore */ } } process.exit(code);