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
   
   
![image](https://github.com/user-attachments/assets/22a396b8-4491-49a9-87ce-7e9d4c9bf225)
   
   
   
   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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to