Lee-W commented on code in PR #48403:
URL: https://github.com/apache/airflow/pull/48403#discussion_r2024231216


##########
airflow-core/src/airflow/cli/commands/config_command.py:
##########
@@ -719,3 +720,146 @@ def lint_config(args) -> None:
         console.print("\n[red]Please update your configuration file 
accordingly.[/red]")
     else:
         console.print("[green]No issues found in your airflow.cfg. It is ready 
for Airflow 3![/green]")
+
+
+@providers_configuration_loaded
+def update_config(args) -> None:
+    """
+    Update the airflow.cfg file to migrate configuration changes from Airflow 
2.x to Airflow 3.
+
+    This command scans the current configuration file for parameters that have 
been renamed,
+    removed, or had their default values changed in Airflow 3.0, and 
automatically updates
+    the configuration file.
+
+    CLI Arguments:
+        --dry-run: flag (optional)
+            Dry-run mode (print the changes without modifying airflow.cfg)
+            Example: --dry-run
+
+        --section: str (optional)
+            Comma-separated list of configuration sections to update.
+            Example: --section core,database
+
+        --option: str (optional)
+            Comma-separated list of configuration options to update.
+            Example: --option sql_alchemy_conn,dag_concurrency
+
+        --ignore-section: str (optional)
+            Comma-separated list of configuration sections to ignore during 
update.
+            Example: --ignore-section webserver
+
+        --ignore-option: str (optional)
+            Comma-separated list of configuration options to ignore during 
update.
+            Example: --ignore-option check_slas
+
+    Examples:
+        1. Dry-run mode (print the changes without modifying airflow.cfg):
+            airflow config update --dry-run
+
+        2. Update the entire configuration file:
+            airflow config update
+
+        3. Update only the 'core' and 'database' sections:
+            airflow config update --section core,database
+
+        4. Update only specific options:
+            airflow config update --option sql_alchemy_conn,dag_concurrency
+
+        5. Ignore updates for a specific section:
+            airflow config update --ignore-section webserver
+
+    :param args: The CLI arguments for updating configuration.
+    """
+    console = AirflowConsole()
+    changes_applied: list[str] = []
+    modifications = ConfigModifications()
+
+    update_sections = args.section if args.section else None
+    update_options = args.option if args.option else None
+    ignore_sections = args.ignore_section if args.ignore_section else []
+    ignore_options = args.ignore_option if args.ignore_option else []
+
+    config_dict = conf.as_dict(
+        display_source=True,
+        include_env=True,
+        include_cmds=True,
+        include_secret=True,
+    )
+    for change in CONFIGS_CHANGES:
+        conf_section = change.config.section.lower()
+        conf_option = change.config.option.lower()
+        full_key = f"{conf_section}.{conf_option}"
+
+        if update_sections is not None and conf_section not in [s.lower() for 
s in update_sections]:
+            continue
+        if update_options is not None and full_key not in [opt.lower() for opt 
in update_options]:
+            continue
+        if conf_section in [s.lower() for s in ignore_sections] or full_key in 
[
+            opt.lower() for opt in ignore_options
+        ]:
+            continue
+
+        if conf_section not in config_dict or conf_option not in 
config_dict[conf_section]:
+            continue
+        value_data = config_dict[conf_section][conf_option]

Review Comment:
   When will this not be a tuple?



##########
airflow-core/src/airflow/cli/commands/config_command.py:
##########
@@ -719,3 +720,146 @@ def lint_config(args) -> None:
         console.print("\n[red]Please update your configuration file 
accordingly.[/red]")
     else:
         console.print("[green]No issues found in your airflow.cfg. It is ready 
for Airflow 3![/green]")
+
+
+@providers_configuration_loaded
+def update_config(args) -> None:
+    """
+    Update the airflow.cfg file to migrate configuration changes from Airflow 
2.x to Airflow 3.
+
+    This command scans the current configuration file for parameters that have 
been renamed,
+    removed, or had their default values changed in Airflow 3.0, and 
automatically updates
+    the configuration file.
+
+    CLI Arguments:
+        --dry-run: flag (optional)
+            Dry-run mode (print the changes without modifying airflow.cfg)
+            Example: --dry-run
+
+        --section: str (optional)
+            Comma-separated list of configuration sections to update.
+            Example: --section core,database
+
+        --option: str (optional)
+            Comma-separated list of configuration options to update.
+            Example: --option sql_alchemy_conn,dag_concurrency
+
+        --ignore-section: str (optional)
+            Comma-separated list of configuration sections to ignore during 
update.
+            Example: --ignore-section webserver
+
+        --ignore-option: str (optional)
+            Comma-separated list of configuration options to ignore during 
update.
+            Example: --ignore-option check_slas
+
+    Examples:
+        1. Dry-run mode (print the changes without modifying airflow.cfg):

Review Comment:
   Are we printing the changes or are we printing the updated `airflow.cfg`



##########
airflow-core/src/airflow/cli/commands/config_command.py:
##########
@@ -719,3 +720,146 @@ def lint_config(args) -> None:
         console.print("\n[red]Please update your configuration file 
accordingly.[/red]")
     else:
         console.print("[green]No issues found in your airflow.cfg. It is ready 
for Airflow 3![/green]")
+
+
+@providers_configuration_loaded
+def update_config(args) -> None:
+    """
+    Update the airflow.cfg file to migrate configuration changes from Airflow 
2.x to Airflow 3.
+
+    This command scans the current configuration file for parameters that have 
been renamed,
+    removed, or had their default values changed in Airflow 3.0, and 
automatically updates
+    the configuration file.

Review Comment:
   I probably would suggest mentioning that comments will be cleaned up



##########
airflow-core/tests/unit/cli/commands/test_config_command.py:
##########
@@ -491,3 +493,74 @@ def test_lint_detects_invalid_config_negative(self):
         normalized_output = re.sub(r"\s+", " ", output.strip())
 
         assert "Invalid value" not in normalized_output
+
+
+class TestCliConfigUpdate:
+    @classmethod
+    def setup_class(cls):
+        cls.parser = cli_parser.get_parser()
+
+    @pytest.fixture(autouse=True)
+    def setup_fake_airflow_cfg(self, tmp_path, monkeypatch):
+        fake_config = tmp_path / "airflow.cfg"
+        fake_config.write_text(
+            "[test_admin]\n"
+            "rename_key = legacy_value\n"
+            "remove_key = to_be_removed\n\n"
+            "[test_core]\n"
+            "dags_folder = /some/path/to/dags\n"
+            "default_key = OldDefault\n"
+        )
+        monkeypatch.setenv("AIRFLOW_CONFIG", str(fake_config))
+        monkeypatch.setattr(config_command, "AIRFLOW_CONFIG", str(fake_config))
+        conf.read(str(fake_config))
+        self.fake_config = fake_config

Review Comment:
   ```suggestion
       def fake_airflow_cfg(self, tmp_path, monkeypatch):
           fake_config = tmp_path / "airflow.cfg"
           fake_config.write_text(
   """
   [test_admin]
   rename_key = legacy_value
   remove_key = to_be_removed
   [test_core]
   dags_folder = /some/path/to/dags
   default_key = OldDefault"""
           )
           monkeypatch.setenv("AIRFLOW_CONFIG", str(fake_config))
           monkeypatch.setattr(config_command, "AIRFLOW_CONFIG", 
str(fake_config))
           conf.read(str(fake_config))
           return fake_config
   ```
   
   I think we can make it a fixture. also we probably could use textwrap to 
make it looks better



-- 
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