You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "amoghrajesh (via GitHub)" <gi...@apache.org> on 2025/06/24 06:33:53 UTC
[PR] Extending `create_default_connections` to store in ENV vars too [airflow]
amoghrajesh opened a new pull request, #52129:
URL: https://github.com/apache/airflow/pull/52129
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!--
Thank you for contributing! Please make sure that your code changes
are covered with tests. And in case of new features or big changes
remember to adjust the documentation.
Feel free to ping committers for the review!
In case of an existing issue, reference it using one of the following:
closes: #ISSUE
related: #ISSUE
How to write a good git commit message:
http://chris.beams.io/posts/git-commit/
-->
related: https://github.com/apache/airflow/pull/51873
OK, hear me out on this one!
## Why?
The `create_default_connections` is a well know utility we use to setup the default connections in Airflow, be it for the breeze setup, test setup or generally, maybe in connections command too.
While working on moving BaseHook to the task SDK, i happened to notice 100's if not 1000's of failures of the form:
```
__ ERROR at setup of TestBigQueryHookMethods.test_query_results[a,b-result2] ___
providers/google/tests/unit/google/cloud/hooks/test_bigquery.py:82: in setup_method
self.hook = MockedBigQueryHook()
providers/google/src/airflow/providers/google/cloud/hooks/bigquery.py:168: in __init__
super().__init__(**kwargs)
providers/google/src/airflow/providers/google/common/hooks/base_google.py:284: in __init__
self.extras: dict = self.get_connection(self.gcp_conn_id).extra_dejson
task-sdk/src/airflow/sdk/bases/hook.py:61: in get_connection
conn = Connection.get(conn_id)
task-sdk/src/airflow/sdk/definitions/connection.py:152: in get
return _get_connection(conn_id)
task-sdk/src/airflow/sdk/execution_time/context.py:153: in _get_connection
from airflow.sdk.execution_time.task_runner import SUPERVISOR_COMMS
E ImportError: cannot import name 'SUPERVISOR_COMMS' from 'airflow.sdk.execution_time.task_runner' (/opt/airflow/task-sdk/src/airflow/sdk/execution_time/task_runner.py)
```
This is partly due to the fact that `mock_supervisor_comms` fixture isn't injected into them, and second being the fact that even if it is, I have to add `mock_supervisor_comms.send.return_value = conn_result` to each of those 1000 tests which is absolutely impractical.
So, if i could make the `mock_supervisor_comms` as `autouse` and have the tests utilitse the fact that Airflow does Env backend as default and create env's for those "default" connections, the tests would not only be faster because they perform env vs DB lookup (earlier) but also benefit from the fact that provider tests need not essentially use the DB at all. (or cannot in the longer run when we move them entirely to task SDK's execution flow)
Why not remove the DB part entirely?
- Hundreds of tests rely on connections being in the database
- UI doesn't show the env stuff, so we might miss out on that too
### Changes Made
- Added dual storage: connections are now stored in both the database AND as environment variables
- Used c.as_json() for environment variable values
- Enhanced clear_db_connections() to cleanup of environment variables with `AIRFLOW_CONN_ prefix`
### Testing
- All existing tests continue to pass (backward compatibility maintained)
- New functionality tested with environment variable cleanup
- No breaking changes to existing APIs or behavior
### Some more trivial examples here
1. `breeze start-airflow --backend postgres --executor CeleryExecutor --dev-mode --db-reset --load-default-connections` still works

3. `airflow connections list` still works and creates connections as needed
```
root@ab6aa532b249:/opt/airflow# airflow connections list
| | | | | | | | | | is_ex | |
| | | descr | | | | | | is_en | tra_e | extra |
| conn_ | conn_ | iptio | | schem | | passw | | crypt | ncryp | _dejs |
id | id | type | n | host | a | login | ord | port | ed | ted | on | get_uri
===+=======+=======+=======+=======+=======+=======+=======+=======+=======+=======+=======+========
1 | airfl | mysql | None | mysql | airfl | root | None | None | False | False | {} | mysql:/
| ow_db | | | | ow | | | | | | | /root@m
| | | | | | | | | | | | ysql/ai
| | | | | | | | | | | | rflow
2 | athen | athen | None | None | None | None | None | None | False | False | {} | athena:
| a_def | a | | | | | | | | | | //
| ault | | | | | | | | | | |
3 | aws_d | aws | None | None | None | None | None | None | False | False | {} | aws://
| efaul | | | | | | | | | | |
| t | | | | | | | | | | |
4 | azure | azure | None | None | None | <ACCO | None | None | False | False | {'acc | azure-b
| _batc | _batc | | | | UNT_N | | | | | ount_ | atch://
| h_def | h | | | | AME> | | | | | url': | %3CACCO
| ault | | | | | | | | | | | UNT_NAM
| | | | | | | | | | | '<ACC | E%3E@/?
| | | | | | | | | | | OUNT_ | account
| | | | | | | | | | | URL>' | _url=%3
| | | | | | | | | | | } | CACCOUN
| | | | | | | | | | | | T_URL%3
| | | | | | | | | | | | E
5 | azure | azure | None | None | None | None | None | None | False | False | {'dat | azure-c
| _cosm | _cosm | | | | | | | | | abase | osmos:/
| os_de | os | | | | | | | | | _name | //?data
| fault | | | | | | | | | | ': | base_na
| | | | | | | | | | | '<DAT | me=%3CD
| | | | | | | | | | | ABASE | ATABASE
| | | | | | | | | | | _NAME | _NAME%3
| | | | | | | | | | | >', | E&colle
| | | | | | | | | | | 'coll | ction_n
| | | | | | | | | | | ectio | ame=%3C
| | | | | | | | | | | n_nam | COLLECT
| | | | | | | | | | | e': | ION_NAM
| | | | | | | | | | | '<COL | E%3E
| | | | | | | | | | | LECTI |
| | | | | | | | | | | ON_NA |
| | | | | | | | | | | ME>'}
```
<!-- Please keep an empty line above the dashes. -->
---
**^ Add meaningful description above**
Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)** for more information.
In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
In case of backwards incompatible changes please leave a note in a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out creation of default Connections for tests and non-tests [airflow]
Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on PR #52129:
URL: https://github.com/apache/airflow/pull/52129#issuecomment-3003950688
Cool. I misunderstood the 'double' creation :) . The key is to run those tests with `none` backend - this way you fail with any db access :)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out creation of default Connections for tests and non-tests [airflow]
Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on PR #52129:
URL: https://github.com/apache/airflow/pull/52129#issuecomment-3003667052
> This is fine, but, we should make the tests work without the DB and remove all "db_test" markers for them eventually,
Yep! That is my goal too
> If I understand correctly - you now create both DB and ENV vars, so it should be just a matter of running conditional code to "only" clear and set "env variables" when DB is not available in the common pytest plugin code. The common plugin code already works fine when there is no DB - but I think what it does it will skip creation of the default connections - but if you **just** change the common code to skip all DB parts but only set the env vars, then I belive none of the "get_connection" calls should even try to make a DB call and they should simply **work** when you set `--backend none`
@potiuk quite the opposite actually, i have extracted out logic to not create stuff on DB for tests. (Trying to get to complete coverage).
Right now, for tests, it creates only default connections and regular connections on ENV only.
That is precisely what you propose I believe.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out creation of default Connections for tests and non-tests [airflow]
Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on PR #52129:
URL: https://github.com/apache/airflow/pull/52129#issuecomment-3003954271
Ah i see, I updated the PR desc a little later, you might have caught the older one!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out creation of default Connections for tests and non-tests [airflow]
Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on PR #52129:
URL: https://github.com/apache/airflow/pull/52129#issuecomment-3004039571
Alright! Finally green
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out creation of default Connections for tests and non-tests [airflow]
Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on PR #52129:
URL: https://github.com/apache/airflow/pull/52129#issuecomment-3001554421
This is fine, but, we should make the tests work without the DB and remove all "db_test" markers for them eventually,
I think this is possible.
If I understand correctly - you now create both DB and ENV vars, so it should be just a matter of running conditional code to "only" clear and set "env variables" when DB is not available in the common pytest plugin code. The common plugin code already works fine when there is no DB - but I think what it does it will skip creation of the default connections - but if you **just** change the common code to skip all DB parts but only set the env vars, then I belive none of the "get_connection" calls should even try to make a DB call and they should simply **work** when you set `--backend none`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out creation of default Connections for tests and non-tests [airflow]
Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh merged PR #52129:
URL: https://github.com/apache/airflow/pull/52129
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out creation of default Connections for tests and non-tests [airflow]
Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #52129:
URL: https://github.com/apache/airflow/pull/52129#discussion_r2163231642
##########
airflow-core/src/airflow/utils/db.py:
##########
@@ -125,9 +125,30 @@ def add_default_pool_if_not_exists(session: Session = NEW_SESSION):
@provide_session
def create_default_connections(session: Session = NEW_SESSION):
"""Create default Airflow connections."""
+ conns = get_default_connections()
+
+ for c in conns:
+ merge_conn(c, session)
+
+
+def create_default_connections_for_tests():
+ """
+ Create default Airflow connections for tests.
+
+ For testing purposes, we do not need to have the connections setup in the database, using environment
+ variables instead would provide better lookup speeds and is easier too.
+ """
+ conns = get_default_connections()
+
+ for c in conns:
+ envvar = f"AIRFLOW_CONN_{c.conn_id.upper()}"
+ os.environ[envvar] = c.as_json()
Review Comment:
Of course! That would only make sense
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out creation of default Connections for tests and non-tests [airflow]
Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on PR #52129:
URL: https://github.com/apache/airflow/pull/52129#issuecomment-3004001128
🤞🏽 hopefully it works now!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out creation of default Connections for tests and non-tests [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #52129:
URL: https://github.com/apache/airflow/pull/52129#discussion_r2163207501
##########
airflow-core/src/airflow/utils/db.py:
##########
@@ -125,9 +125,30 @@ def add_default_pool_if_not_exists(session: Session = NEW_SESSION):
@provide_session
def create_default_connections(session: Session = NEW_SESSION):
"""Create default Airflow connections."""
+ conns = get_default_connections()
+
+ for c in conns:
+ merge_conn(c, session)
+
+
+def create_default_connections_for_tests():
+ """
+ Create default Airflow connections for tests.
+
+ For testing purposes, we do not need to have the connections setup in the database, using environment
+ variables instead would provide better lookup speeds and is easier too.
+ """
+ conns = get_default_connections()
+
+ for c in conns:
+ envvar = f"AIRFLOW_CONN_{c.conn_id.upper()}"
+ os.environ[envvar] = c.as_json()
Review Comment:
Worth moving this in tests - `devel-common/src/tests_common/test_utils/db.py`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out creation of default Connections for tests and non-tests [airflow]
Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #52129:
URL: https://github.com/apache/airflow/pull/52129#discussion_r2166239181
##########
devel-common/src/tests_common/test_utils/db.py:
##########
@@ -338,6 +356,461 @@ def clear_dag_specific_permissions():
session.query(Resource).filter(Resource.id.in_(dag_resource_ids)).delete(synchronize_session=False)
+def create_default_connections_for_tests():
+ """
+ Create default Airflow connections for tests.
+
+ For testing purposes, we do not need to have the connections setup in the database, using environment
+ variables instead would provide better lookup speeds and is easier too.
+ """
+ import os
+
+ try:
+ from airflow.utils.db import get_default_connections
+
+ conns = get_default_connections()
+ except ImportError:
Review Comment:
I do not love the way i have achieved compat here but I might revisit this
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out creation of default Connections for tests and non-tests [airflow]
Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #52129:
URL: https://github.com/apache/airflow/pull/52129#discussion_r2163464699
##########
airflow-core/src/airflow/utils/db.py:
##########
@@ -125,9 +125,30 @@ def add_default_pool_if_not_exists(session: Session = NEW_SESSION):
@provide_session
def create_default_connections(session: Session = NEW_SESSION):
"""Create default Airflow connections."""
+ conns = get_default_connections()
+
+ for c in conns:
+ merge_conn(c, session)
+
+
+def create_default_connections_for_tests():
+ """
+ Create default Airflow connections for tests.
+
+ For testing purposes, we do not need to have the connections setup in the database, using environment
+ variables instead would provide better lookup speeds and is easier too.
+ """
+ conns = get_default_connections()
+
+ for c in conns:
+ envvar = f"AIRFLOW_CONN_{c.conn_id.upper()}"
+ os.environ[envvar] = c.as_json()
Review Comment:
Handled in [move it to test folder](https://github.com/apache/airflow/pull/52129/commits/0a19d83b00350267e1741cccf8e1b1e5469519d7)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org