Skip to content

fix(settings): environment variables take precedence over datajoint.json for every setting - #1603

Open
dimitri-yatsenko wants to merge 5 commits into
masterfrom
fix/env-var-precedence
Open

dimitri-yatsenko wants to merge 5 commits into
masterfrom
fix/env-var-precedence

Conversation

@dimitri-yatsenko

@dimitri-yatsenko dimitri-yatsenko commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

Summary

Environment variables now take precedence over datajoint.json for 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.json sets 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) and database.reconnect (DJ_RECONNECT)
  • safemode, query_cache, download_path, ignore_config_file and allow_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_MAPPING is now built from the settings models. A field's variable is its validation_alias. Otherwise it is its model's env_prefix plus the field name. A setting added later is covered as soon as it is declared.
  • Models without an env_prefix (connection, display) contribute only their aliased fields, which matches the documented variables.
  • stores stays with _apply_stores_env, which already applies DJ_STORES after the file.
  • The names in the old list are unchanged.

Tests

  • tests/unit/test_settings.py::TestEnvPrecedence lists every setting that reads an environment variable and checks that the derived mapping matches it.
  • For each setting, in both flat and nested file layouts, it checks that the variable is read, that the file does not replace it, and that the file applies once the variable is unset.
  • A test reproduces the example from the issue.
  • These tests fail on master (29 failures) and pass with the change.
  • The unit suite passes. The connection, TLS and multi-backend integration tests pass on MySQL and PostgreSQL.

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.

…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 dimitri-yatsenko added this to the v2.4 milestone Oct 17, 2026
@dimitri-yatsenko dimitri-yatsenko added the bug Indicates an unexpected problem or unintended behavior label Oct 17, 2026
@dimitri-yatsenko
dimitri-yatsenko marked this pull request as draft October 21, 2026 16:46
@dimitri-yatsenko dimitri-yatsenko modified the milestones: v2.4, v2.3.7 Nov 6, 2026
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 dimitri-yatsenko changed the title fix(settings): environment variables beat datajoint.json for every setting fix(settings): environment variables take precedence over datajoint.json for every setting Nov 21, 2026
@dimitri-yatsenko
dimitri-yatsenko marked this pull request as ready for review November 21, 2026 01:08

This branch has not been deployed

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

Labels

bug Indicates an unexpected problem or unintended behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Environment variables lose to datajoint.json for settings missing from ENV_VAR_MAPPING

1 participant