You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by as...@apache.org on 2022/04/08 08:15:22 UTC

[airflow] branch main updated: Disable foreign keys on sqlite when modifying dag_run (#22848)

This is an automated email from the ASF dual-hosted git repository.

ash pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new d328dcfd0a Disable foreign keys on sqlite when modifying dag_run (#22848)
d328dcfd0a is described below

commit d328dcfd0a94a9018fea7a7248a799469e24356c
Author: Daniel Standish <15...@users.noreply.github.com>
AuthorDate: Fri Apr 8 01:15:08 2022 -0700

    Disable foreign keys on sqlite when modifying dag_run (#22848)
    
    If we do not disable FKs then it has the side effect of deleting all task instances.
---
 airflow/migrations/utils.py                                   | 11 +++++++++++
 .../0099_f9da662e7089_add_task_log_filename_template_model.py |  4 +++-
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/airflow/migrations/utils.py b/airflow/migrations/utils.py
index 3294ced388..f1f3ea0442 100644
--- a/airflow/migrations/utils.py
+++ b/airflow/migrations/utils.py
@@ -16,6 +16,7 @@
 # under the License.
 
 from collections import defaultdict
+from contextlib import contextmanager
 
 
 def get_mssql_table_constraints(conn, table_name):
@@ -41,3 +42,13 @@ def get_mssql_table_constraints(conn, table_name):
     for constraint, constraint_type, col_name in result:
         constraint_dict[constraint_type][constraint].append(col_name)
     return constraint_dict
+
+
+@contextmanager
+def disable_sqlite_fkeys(op):
+    if op.get_bind().dialect.name == 'sqlite':
+        op.execute("PRAGMA foreign_keys=off")
+        yield op
+        op.execute("PRAGMA foreign_keys=on")
+    else:
+        yield op
diff --git a/airflow/migrations/versions/0099_f9da662e7089_add_task_log_filename_template_model.py b/airflow/migrations/versions/0099_f9da662e7089_add_task_log_filename_template_model.py
index a2511a398d..05fc2767fa 100644
--- a/airflow/migrations/versions/0099_f9da662e7089_add_task_log_filename_template_model.py
+++ b/airflow/migrations/versions/0099_f9da662e7089_add_task_log_filename_template_model.py
@@ -26,6 +26,7 @@ Create Date: 2021-12-09 06:11:21.044940
 from alembic import op
 from sqlalchemy import Column, ForeignKey, Integer, Text
 
+from airflow.migrations.utils import disable_sqlite_fkeys
 from airflow.utils.sqlalchemy import UtcDateTime
 
 # Revision identifiers, used by Alembic.
@@ -50,7 +51,8 @@ def upgrade():
         Integer,
         ForeignKey("log_template.id", name="task_instance_log_template_id_fkey", ondelete="NO ACTION"),
     )
-    with op.batch_alter_table("dag_run") as batch_op:
+
+    with disable_sqlite_fkeys(op), op.batch_alter_table("dag_run") as batch_op:
         batch_op.add_column(dag_run_log_filename_id)