Repository navigation
fix(settings): environment variables take precedence over datajoint.json for every setting - #1603
Open
dimitri-yatsenko wants to merge 5 commits into
Open
dimitri-yatsenko wants to merge 5 commits into
dimitri-yatsenko wants to merge 5 commits into
Conversation
…tting The module documents environment variables as taking precedence over `datajoint.json`, but `_update_from_flat_dict` enforced that only for keys in the hand-kept `ENV_VAR_MAPPING`. For every other setting the file replaced the value read from the environment: `jobs.*` (`DJ_JOBS_*`), `provenance.*` (`DJ_PROVENANCE_*`), `database.use_tls`, `database.reconnect`, and the top-level `safemode`, `query_cache`, `download_path` and `ignore_config_file`. A deployment could not, for example, turn off `safemode` or provenance capture for one worker when the project's file set it. `ENV_VAR_MAPPING` is now derived from the settings models: a field's variable is its `validation_alias`, otherwise its model's `env_prefix` plus the field name. A setting added later is covered as soon as it is declared, including one that has only a `validation_alias`. Models without an `env_prefix` (`connection`, `display`) contribute only their aliased fields, matching the documented variables, and `stores` stays with `_apply_stores_env`, which already runs after the file. The eleven names in the old mapping are unchanged. The tests list every setting that reads an environment variable, assert the derived mapping matches that list, and check for each one, in both flat and nested file layouts, that pydantic reads the variable and the file does not replace it. Closes #1585.
dimitri-yatsenko
requested review from
MilagrosMarin,
lum-agilyti and
ttngu207
October 17, 2026 22:02
dimitri-yatsenko
marked this pull request as draft
October 21, 2026 16:46
# Conflicts: # src/datajoint/settings.py
Conflict in tests/unit/test_settings.py: the import block. Kept both sides: json for the precedence tests, and os, signal, stat and warnings for master's config-file ownership tests. Master added the allow_filepath_outside_store setting. The mapping derived from the settings models now includes it as DJ_ALLOW_FILEPATH_OUTSIDE_STORE, so the variable takes precedence over datajoint.json like every other setting. Added its row to ENV_PRECEDENCE_CASES, which test_every_env_var_has_a_case requires.
dimitri-yatsenko
marked this pull request as ready for review
November 21, 2026 01:08
This was referenced Nov 21, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Environment variables now take precedence over
datajoint.jsonfor every setting that reads one. Before this change, that held only for the settings in a hand-kept list. For all other settings, the file replaced the value read from the environment.Behavior change. If a deployment sets an environment variable and
datajoint.jsonsets the same key, the environment value now wins. This affects these settings, where the file used to win:jobs.*(DJ_JOBS_*)provenance.*(DJ_PROVENANCE_*)database.use_tls(DJ_USE_TLS) anddatabase.reconnect(DJ_RECONNECT)safemode,query_cache,download_path,ignore_config_fileandallow_filepath_outside_store(DJ_SAFEMODE,DJ_QUERY_CACHE,DJ_DOWNLOAD_PATH,DJ_IGNORE_CONFIG_FILE,DJ_ALLOW_FILEPATH_OUTSIDE_STORE)This is the precedence the module already documents. A deployment that sets one of these variables and relied on the file to override it should unset the variable. Settings that were already covered behave as before.
Fixes #1585.
Changes
ENV_VAR_MAPPINGis now built from the settings models. A field's variable is itsvalidation_alias. Otherwise it is its model'senv_prefixplus the field name. A setting added later is covered as soon as it is declared.env_prefix(connection,display) contribute only their aliased fields, which matches the documented variables.storesstays with_apply_stores_env, which already appliesDJ_STORESafter the file.Tests
tests/unit/test_settings.py::TestEnvPrecedencelists every setting that reads an environment variable and checks that the derived mapping matches it.master(29 failures) and pass with the change.Documentation
datajoint/datajoint-docs#317 documents the new precedence in the configuration reference and in the Configure Database, Deploy to Production and Manage Secrets how-to pages, and merges after this PR.