Repository navigation
[core] Add explicit database name settings - #8097
Conversation
Since 25.03.39 the database name in a MongoDB connection string is replaced with countly, countly_drill, countly_out or countly_fs, and the API always ignored mongodb.db, so there was no working way to run Countly on databases with other names. Add a "databases" config with one env var per database: COUNTLY_CONFIG__DATABASES_COUNTLY, ..._COUNTLY_DRILL, ..._COUNTLY_OUT, ..._COUNTLY_FS. Names are used as is and never parsed from or put into the connection string. Order of precedence per database: env var, databases.* in config.js, db in object form mongodb config (main database) or in the database's own config file (drill/out/fs), default. The name is applied to every connection: named connections, dashboard connection, job handler, shared connection and command line/export params, so API and dashboard always use the same main database. configextender now creates missing override sections with lower case keys, without it the new env vars landed in an unused DATABASES key. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
c13b11b to
0acf0de
Compare
Critical reviewReviewed commit 1. [P1] Backup/restore ignores the configured databasesWith Resolve each database through the shared configuration and verify a backup/restore round trip, including the other three database names. Source: bin/commands/countly.sh. 2. [P1] User-data exports write into the wrong database
Use the resolved main database as the merge destination and cover export, download, and cleanup with a custom-name test. Source: api/parts/mgmt/app_users.js. 3. [P2] Explicit config objects lose their database selectionThe new resolver reads global Resolve names from the supplied object when that overload is used. The current config-object test passes the same global object, so it does not expose this regression. Source: plugins/pluginManager.js. 4. [P2] CLI exports can authenticate against a different database from the applicationFor Preserve the effective authentication database when replacing Sources: getDbConnectionParams, MongoDB authenticationDatabase documentation. 5. [P2] Some valid environment-variable names silently select the fallback database
Preserve database-name environment values as strings and test boolean-like names through the actual environment override path. Source: getConfiguredDatabaseName. Validation and limits
|
CI sets COUNTLY_CONFIG__MONGODB_HOST. Env overrides are applied again
for drill/out/fs connections, so with the connection string these
tests use, the env var replaced it with {host: "mongodb"} and the
connections failed. Run the connection tests without COUNTLY_CONFIG*
env vars and restore config in finally blocks so one failure does not
cascade into the following tests.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Problem
Since 25.03.39 (#7381)
replaceDatabaseStringalways replaces the database name in a MongoDB connection string withcountly,countly_drill,countly_outorcountly_fs. Combined with the API always passing explicit names todbConnection(somongodb.dbin object form config was only used by the dashboard and job handler), there was no working way to run Countly on databases with other names.Installs upgrading from 25.03.38 or older whose data is in databases not named exactly
countly*(custom name in the connection string path, or names the old string parsing produced) end up on new, empty databases after the upgrade, and the dashboard shows the setup screen.Change
New
databasesconfig, one env var per database:Names are used as is, never parsed from or inserted into the connection string, so any valid MongoDB database name works.
Order of precedence per database (highest first):
COUNTLY_CONFIG__DATABASES_COUNTLYCOUNTLY_CONFIG__DATABASES_COUNTLY_DRILL/_OUT/_FSdatabases.countlyin config.jsdatabases.countly_drill/ ...dbin object formmongodbconfigdbin the database's own config file (plugins/drill/config.js,api/configs/config.db_out.js,config.db_fs.js)countlycountly_drill/countly_out/countly_fsThe resolved name is used by every connection: named connections (
dbConnection("countly_drill")etc., also used by enterprise plugin scripts), the dashboard connection, the job handler (singleDefaultConnection),shared_connection, andgetDbConnectionParams(exports,countly mongo).Other changes:
configextender: override sections missing from config.js are now created with lower case keys. Before,COUNTLY_CONFIG__COOKIE_SAMESITEwithout acookiesection produced an unusedCOOKIEkey; without this fix the new env vars would always land in an unusedDATABASESkey, asdatabasesis commented out in the samples.appnameshows the internal label (countly_drill) instead of the database name.COUNTLY_CONFIG__DATABASES_COUNTLY=2024, parsed to numbers by configextender) are accepted.databasesand the order of precedence; URL example without/countlypath (database names are not taken from the URL) and with anauthSourcehint, since the path db is the default auth database.Behaviour changes
mongodb.db(e.g.countly_base): previously the dashboard and job handler usedcountly_basewhile the API usedcountly. Now all connections usecountly_base. Data the API wrote tocountlyin such installs stays there.Not changed
COUNTLY_CONFIG_PLUGINDRILL_MONGODB) is still ignored.COUNTLY_CONFIG__MONGODB, anyCOUNTLY_CONFIG__MONGODB_*env var still replaces the string (existing configextender behaviour), hence theDATABASES_prefix.countly backup(bin/commands/countly.sh) still dumps--db countly.Testing
test/unit-tests/plugins.pluginManager.database-names.js(22 tests): env var mapping, precedence, every connection type, fallback when nothing is set, prototype keys, number-like names, drill config file level. Connections are checked without a server by stubbingMongoClient.connect.pluginManagerbefore and after the change; only the intended rows changed.🤖 Generated with Claude Code