Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AIP-72 | "unsafe" level #45200

Open
1 of 2 tasks
raphaelauv opened this issue Dec 24, 2024 · 0 comments
Open
1 of 2 tasks

AIP-72 | "unsafe" level #45200

raphaelauv opened this issue Dec 24, 2024 · 0 comments
Labels
area:core kind:feature Feature Requests needs-triage label for new issues that we didn't triage yet

Comments

@raphaelauv
Copy link
Contributor

raphaelauv commented Dec 24, 2024

Description

I would like to still be able to write dag with a low level access to airflow database for operational purposes in airflow v3

it's convenient to be able to programmatically access the database session in a pythonoperator for specific cleaning / operational purposes

example

    def clear_dag_runs(dag_id, status_to_clear):
        context = get_current_context()
        session = settings.Session()

        query = session.query(DagRun).filter(
            DagRun.state == status_to_clear, DagRun.dag_id == dag_id)
        rst = query.all()

        dag_bag = DagBag(dag_folder=path.join(SRC_FOLDER, 'dags'), include_examples=False)
        dag: DAG = dag_bag.get_dag(dag_id, session)

        for dag_run in rst:
            dag.clear(
                start_date=dag_run.logical_date,
                end_date=dag_run.logical_date,
                task_ids=None,
                include_subdags=True,
                include_parentdag=True,
                only_failed=False,
            )
        session.close()

    def delete_dag(dag_id):
        context = get_current_context()
        session = settings.Session()
        keep_records_in_log = False

        for model in get_sqla_model_classes():
            if hasattr(model, "dag_id") and (not keep_records_in_log or model.__name__ != "Log"):
                session.execute(
                    delete(model)
                    .where(model.dag_id.__eq__(dag_id))
                    .execution_options(synchronize_session="fetch")
                )

    PythonOperator(
        task_id="delete",
        python_callable=delete_dag,
        op_kwargs={"dag_id": "{{params.dag_name}}"}
    )

wdyt ?

Use case/motivation

No response

Related issues

No response

Are you willing to submit a PR?

  • Yes I am willing to submit a PR!

Code of Conduct

@raphaelauv raphaelauv added kind:feature Feature Requests needs-triage label for new issues that we didn't triage yet labels Dec 24, 2024
@dosubot dosubot bot added the area:core label Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:core kind:feature Feature Requests needs-triage label for new issues that we didn't triage yet
Projects
None yet
Development

No branches or pull requests

1 participant