Repository navigation
Conversation
Previously, per-key failures during `airflow variables import` (e.g. a key exceeding the 250-char limit enforced by Postgres/MySQL, unlike SQLite) were counted and printed but never caused the command to exit non-zero, unlike the equivalent `pools import` command. A caller chaining `airflow variables import vars.json && next_step` would proceed as if the import had fully succeeded. Also fixes "already exists" -> "already exist" grammar in the two messages describing multiple keys. Co-Authored-By: Claude Sonnet 5 <[email protected]>
| # Test fail action - should fail when key1 already exists | ||
| Variable.set("key1", "original_value") | ||
| with pytest.raises(SystemExit, match="already exists"): | ||
| with pytest.raises(SystemExit, match="already exist"): |
There was a problem hiding this comment.
Thanks for the patch!
Just one nit:
pytest.raises(match=...) is re.search, so this still matches the old already exists. — with variable_command.py reverted, this assertion passes and only the new test fails.
| with pytest.raises(SystemExit, match="already exist"): | |
| with pytest.raises(SystemExit, match=r"already exist\."): |
| f"The variables with these keys: {list(sorted(skipped))} were skipped because they already exist" | ||
| ) | ||
| if fail_count: | ||
| raise SystemExit(f"{fail_count} variable(s) failed to be updated.") |
There was a problem hiding this comment.
pools import, the sibling this mirrors, names what failed (Failed to update pool(s): {', '.join(failed)}). Collecting the keys in the except above and listing them here would make a 200-variable import actionable — a count alone might isn't enough?
| value, description = v["value"], v.get("description") | ||
| Variable.set(k, value, description, serialize_json=not isinstance(value, str)) | ||
| except Exception as e: | ||
| print(f"Variable import failed: {e!r}") |
There was a problem hiding this comment.
| print(f"Variable import failed for key {k!r}: {e!r}") |
potiuk
left a comment
There was a problem hiding this comment.
Thanks, a failed import should not exit 0. Since this changes the exit code of an existing CLI command, it is effectively a clarification of the CLI's contract, and scripts that run airflow variables import will see it. So please add a newsfragment, airflow-core/newsfragments/72958.significant.rst. It should say that airflow variables import now exits non-zero when any variable fails to import, and that the variables that imported successfully stay saved (the import is not atomic), so anyone reacting to the exit code knows a partial import happened.
A few smaller points:
- The error says how many keys failed, never which ones, and the per-key
Variable import failed: {e!r}line doesn't include the key either. Collecting the failed keys and putting them in theSystemExitmessage, asairflow pools importdoes (Failed to update pool(s): ...), would make the failure actionable (see inline). airflow connections importhas the same "prints the error, exits 0" behaviour. It's fine to keep that out of this PR, but it would be good to fix it the same way in a follow-up so the two commands behave consistently.- The new test nests
with mock.patch.object(...); the repo convention is@mock.patchdecorators. It also doesn't check thatgood_keywas still saved and counted ("1 of 2 variables successfully updated."), which is the partial-import behaviour the newsfragment will describe.
Drafted-by: Claude Code (Opus 5.5); reviewed by @potiuk before posting
| if fail_count: | ||
| raise SystemExit(f"{fail_count} variable(s) failed to be updated.") |
There was a problem hiding this comment.
Listing the failed keys here (collected next to fail_count += 1 above) would tell the operator what to fix, like pools import does:
if failed_keys:
raise SystemExit(f"Failed to import variable(s): {', '.join(sorted(failed_keys))}")
airflow variables importcounted and printed per-key failures (e.g. a key exceeding the 250-char limit enforced by Postgres/MySQL, unlike SQLite) but never caused the command to exit non-zero. A caller chainingairflow variables import vars.json && next_stepwould proceed as if the import had fully succeeded.This mirrors the existing behavior of the sibling
pools importcommand, which already doesraise SystemExit(...)when any pool fails to import.Also fixes a small grammar issue ("already exists" -> "already exist") in the two messages that describe multiple keys.
Was generative AI tooling used to co-author this PR?