Skip to content

Commit 6682633

Browse files
author
Kamil Breguła
committed
Warn about precedence of env var when geting variables
1 parent 543194d commit 6682633

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

airflow/models/variable.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
# under the License.
1818

1919
import json
20+
import logging
21+
import os
2022
from typing import Any, Optional
2123

2224
from cryptography.fernet import InvalidToken as InvalidFernetToken
@@ -30,6 +32,8 @@
3032
from airflow.utils.log.logging_mixin import LoggingMixin
3133
from airflow.utils.session import provide_session
3234

35+
log = logging.getLogger()
36+
3337

3438
class Variable(Base, LoggingMixin):
3539
"""
@@ -143,6 +147,14 @@ def set(cls, key: str, value: Any, serialize_json: bool = False, session: Sessio
143147
:param serialize_json: Serialize the value to a JSON string
144148
:param session: SQL Alchemy Sessions
145149
"""
150+
env_var_name = "AIRFLOW_VAR_" + key.upper()
151+
if env_var_name in os.environ:
152+
log.warning(
153+
"You have the environment variable %s defined, which takes precedence over reading "
154+
"from the database. The value will be saved, but to read it you have to delete "
155+
"the environment variable.",
156+
env_var_name,
157+
)
146158
if serialize_json:
147159
stored_value = json.dumps(value, indent=2)
148160
else:

tests/models/test_variable.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
# under the License.
1818

1919
import unittest
20+
from unittest import mock
2021

2122
from cryptography.fernet import Fernet
2223
from parameterized import parameterized
2324

2425
from airflow import settings
25-
from airflow.models import Variable, crypto
26+
from airflow.models import Variable, crypto, variable
2627
from tests.test_utils import db
2728
from tests.test_utils.config import conf_vars
2829

@@ -89,6 +90,23 @@ def test_variable_set_get_round_trip(self):
8990
Variable.set("tested_var_set_id", "Monday morning breakfast")
9091
self.assertEqual("Monday morning breakfast", Variable.get("tested_var_set_id"))
9192

93+
def test_variable_set_with_env_variable(self):
94+
Variable.set("key", "db-value")
95+
with self.assertLogs(variable.log) as log_context:
96+
with mock.patch.dict('os.environ', AIRFLOW_VAR_KEY="env-value"):
97+
Variable.set("key", "new-db-value")
98+
self.assertEqual("env-value", Variable.get("key"))
99+
self.assertEqual("new-db-value", Variable.get("key"))
100+
101+
self.assertEqual(
102+
log_context.records[0].message,
103+
(
104+
'You have the environment variable AIRFLOW_VAR_KEY defined, which takes precedence over '
105+
'reading from the database. The value will be saved, but to read it you have to delete '
106+
'the environment variable.'
107+
),
108+
)
109+
92110
def test_variable_set_get_round_trip_json(self):
93111
value = {"a": 17, "b": 47}
94112
Variable.set("tested_var_set_id", value, serialize_json=True)

0 commit comments

Comments
 (0)