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

Update conf column in dag_run table type from bytea to JSON #44533

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
b91446b
remove pickled data from dag run table
vatsrahul1001 Dec 1, 2024
cbde2ed
Merge branch 'main' of github.com:astronomer/airflow into dag_run_mig…
vatsrahul1001 Dec 3, 2024
e698769
fix downgrade + add news fragement
vatsrahul1001 Dec 3, 2024
f7f9155
Merge branch 'main' into dag_run_migrate_conf_column_as_json
vatsrahul1001 Dec 3, 2024
18c03fb
remove archive table if exits after downgrade
vatsrahul1001 Dec 3, 2024
2b6abf7
Merge branch 'dag_run_migrate_conf_column_as_json' of github.com:astr…
vatsrahul1001 Dec 3, 2024
a54c435
removing archiving data
vatsrahul1001 Dec 4, 2024
6b3789d
implementing review comments
vatsrahul1001 Dec 10, 2024
07258f5
Merge branch 'main' of github.com:astronomer/airflow into dag_run_mig…
vatsrahul1001 Dec 10, 2024
8b88d63
fixing static check
vatsrahul1001 Dec 10, 2024
730470b
Merge branch 'main' into dag_run_migrate_conf_column_as_json
vatsrahul1001 Dec 10, 2024
c2e0d09
fixing static checks
vatsrahul1001 Dec 10, 2024
c2b8dbd
Merge branch 'main' into dag_run_migrate_conf_column_as_json
vatsrahul1001 Dec 12, 2024
c8fa27d
simplying upgrade and downgrade as per review
vatsrahul1001 Dec 13, 2024
d27b7ed
simplying upgrade and downgrade as per review
vatsrahul1001 Dec 13, 2024
603ca17
Merge branch 'dag_run_migrate_conf_column_as_json' of github.com:astr…
vatsrahul1001 Dec 13, 2024
9406702
fixing failures
vatsrahul1001 Dec 13, 2024
01c453c
Merge branch 'main' of github.com:astronomer/airflow into dag_run_mig…
vatsrahul1001 Dec 14, 2024
219b694
Merge branch 'main' of github.com:astronomer/airflow into dag_run_mig…
vatsrahul1001 Dec 14, 2024
22a5dba
removing setting conf to null
vatsrahul1001 Dec 14, 2024
70ce5df
refactor approach to migrate values in conf
vatsrahul1001 Dec 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#
# 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.

"""
remove pickled data from dagrun table.

Revision ID: e39a26ac59f6
Revises: 038dc8bc6284
Create Date: 2024-12-01 08:33:15.425141

"""

from __future__ import annotations

import sqlalchemy as sa
from alembic import op
from sqlalchemy import text
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "e39a26ac59f6"
down_revision = "038dc8bc6284"
branch_labels = None
depends_on = None
airflow_version = "3.0.0"


def upgrade():
"""Apply remove pickled data from dagrun table."""
conn = op.get_bind()

# Update the dag_run.conf column value to NULL
conn.execute(text("UPDATE dag_run set conf=null WHERE conf IS NOT NULL"))

conf_type = sa.JSON().with_variant(postgresql.JSONB, "postgresql")

with op.batch_alter_table("dag_run", schema=None) as batch_op:
# Drop the existing column for SQLite (due to its limitations)
batch_op.drop_column("conf")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to set conf=null, if we are going to drop that column anyway?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth for you to take a look too @jedcunningham @ephraimbuddy

# Add the new column with the correct type for the all dialect
batch_op.add_column(sa.Column("conf", conf_type, nullable=True))


def downgrade():
"""Unapply Remove pickled data from dagrun table."""
conn = op.get_bind()
conn.execute(text("UPDATE dag_run set conf=null WHERE conf IS NOT NULL"))

# Update the dag_run.conf column value to NULL to avoid issues during the type change
conn.execute(text("UPDATE dag_run set conf=null WHERE conf IS NOT NULL"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate?


conf_type = sa.LargeBinary().with_variant(postgresql.BYTEA, "postgresql")

# Apply the same logic for all dialects, including SQLite
with op.batch_alter_table("dag_run", schema=None) as batch_op:
# Drop the existing column for SQLite (due to its limitations)
batch_op.drop_column("conf")
batch_op.add_column(sa.Column("conf", conf_type, nullable=True))
5 changes: 3 additions & 2 deletions airflow/models/dagrun.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@

import re2
from sqlalchemy import (
JSON,
Boolean,
Column,
Enum,
ForeignKey,
ForeignKeyConstraint,
Index,
Integer,
PickleType,
PrimaryKeyConstraint,
String,
Text,
Expand All @@ -44,6 +44,7 @@
text,
update,
)
from sqlalchemy.dialects import postgresql
from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.orm import declared_attr, joinedload, relationship, synonym, validates
Expand Down Expand Up @@ -137,7 +138,7 @@ class DagRun(Base, LoggingMixin):
triggered_by = Column(
Enum(DagRunTriggeredByType, native_enum=False, length=50)
) # Airflow component that triggered the run.
conf = Column(PickleType)
conf = Column(JSON().with_variant(postgresql.JSONB, "postgresql"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we not need any more handling than these?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if the DagRun edit view works in the FAB UI

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you might need to change this line:

if item.conf:
item.conf = json.loads(item.conf)

Copy link
Member

@kaxil kaxil Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@kaxil kaxil Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# These two must be either both NULL or both datetime.
data_interval_start = Column(UtcDateTime)
data_interval_end = Column(UtcDateTime)
Expand Down
2 changes: 1 addition & 1 deletion airflow/utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class MappedClassProtocol(Protocol):
"2.9.2": "686269002441",
"2.10.0": "22ed7efa9da2",
"2.10.3": "5f2621c13b39",
"3.0.0": "038dc8bc6284",
"3.0.0": "e39a26ac59f6",
}


Expand Down
2 changes: 1 addition & 1 deletion docs/apache-airflow/img/airflow_erd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8f2fd91375c546b297490e701dc3853d7ba53c7cd1422ed7f7e57b9ac86f6eca
3c7e69f4098d4078941590dd3a21b78473e8586683595a592dafc08decf4d534
4 changes: 3 additions & 1 deletion docs/apache-airflow/migrations-ref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ Here's the list of all the Database Migrations that are executed via when you ru
+-------------------------+------------------+-------------------+--------------------------------------------------------------+
| Revision ID | Revises ID | Airflow Version | Description |
+=========================+==================+===================+==============================================================+
| ``038dc8bc6284`` (head) | ``e229247a6cb1`` | ``3.0.0`` | update trigger_timeout column in task_instance table to UTC. |
| ``e39a26ac59f6`` (head) | ``038dc8bc6284`` | ``3.0.0`` | remove pickled data from dagrun table. |
+-------------------------+------------------+-------------------+--------------------------------------------------------------+
| ``038dc8bc6284`` | ``e229247a6cb1`` | ``3.0.0`` | update trigger_timeout column in task_instance table to UTC. |
+-------------------------+------------------+-------------------+--------------------------------------------------------------+
| ``e229247a6cb1`` | ``eed27faa34e3`` | ``3.0.0`` | Add DagBundleModel. |
+-------------------------+------------------+-------------------+--------------------------------------------------------------+
Expand Down
21 changes: 21 additions & 0 deletions newsfragments/44533.significant.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Update conf column in dag_run table type from byte ( that store a python pickle ) to JSON

.. Provide additional contextual information

Column conf of the table dag_run is using the type byte ( and storing a python pickle ) on the database , since airflow only support postgres 12+ and mysql 8+ , we updated it to json type .

.. Check the type of change that applies to this change

* Types of change

* [ ] DAG changes
* [ ] Config changes
* [ ] API changes
* [ ] CLI changes
* [x] Behaviour changes
* [ ] Plugin changes
* [ ] Dependency change

.. List the migration rules needed for this change (see https://github.com/apache/airflow/issues/41641)

* Migrations rules needed
Loading