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 bytes to JSON #44533

Merged
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
56 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
0cade3a
Merge branch 'main' into dag_run_migrate_conf_column_as_json
vatsrahul1001 Dec 17, 2024
cce341a
update offline warning
vatsrahul1001 Dec 17, 2024
c3604d9
Merge branch 'dag_run_migrate_conf_column_as_json' of github.com:astr…
vatsrahul1001 Dec 17, 2024
1eb120a
resolving conflicts
vatsrahul1001 Dec 31, 2024
1bdc5cb
Merge branch 'main' into dag_run_migrate_conf_column_as_json
vatsrahul1001 Dec 31, 2024
8261e05
resolving conflicts
vatsrahul1001 Dec 31, 2024
9101a05
Merge branch 'main' into dag_run_migrate_conf_column_as_json
vatsrahul1001 Dec 31, 2024
a6ebc01
Merge branch 'dag_run_migrate_conf_column_as_json' of github.com:astr…
vatsrahul1001 Dec 31, 2024
7fb6509
resolving conflicts
vatsrahul1001 Dec 31, 2024
33e4c6e
Merge branch 'main' into dag_run_migrate_conf_column_as_json
vatsrahul1001 Dec 31, 2024
6b23d8a
resolving conflicts
vatsrahul1001 Dec 31, 2024
249aa7e
Merge branch 'dag_run_migrate_conf_column_as_json' of github.com:astr…
vatsrahul1001 Dec 31, 2024
fa28acd
Merge branch 'main' into dag_run_migrate_conf_column_as_json
vatsrahul1001 Jan 1, 2025
dcd8534
Merge branch 'main' into dag_run_migrate_conf_column_as_json
vatsrahul1001 Jan 1, 2025
71078d7
Merge branch 'main' into dag_run_migrate_conf_column_as_json
vatsrahul1001 Jan 1, 2025
2ee9e7b
Merge branch 'main' into dag_run_migrate_conf_column_as_json
vatsrahul1001 Jan 2, 2025
d2f2b0f
Merge branch 'main' of github.com:astronomer/airflow into dag_run_mig…
vatsrahul1001 Jan 2, 2025
4845c61
Merge branch 'main' of github.com:astronomer/airflow into dag_run_mig…
vatsrahul1001 Jan 2, 2025
141abfd
Merge branch 'main' into dag_run_migrate_conf_column_as_json
jedcunningham Jan 2, 2025
1276951
Merge branch 'main' of github.com:astronomer/airflow into dag_run_mig…
vatsrahul1001 Jan 2, 2025
8488cdf
Merge branch 'main' into dag_run_migrate_conf_column_as_json
vatsrahul1001 Jan 6, 2025
fbdfd66
Merge branch 'main' into dag_run_migrate_conf_column_as_json
vatsrahul1001 Jan 7, 2025
2b91c12
updating batch size
vatsrahul1001 Jan 7, 2025
a946d19
Merge branch 'dag_run_migrate_conf_column_as_json' of github.com:astr…
vatsrahul1001 Jan 7, 2025
4409c11
Merge branch 'main' into dag_run_migrate_conf_column_as_json
vatsrahul1001 Jan 7, 2025
a8ec33b
updaing conf type
vatsrahul1001 Jan 7, 2025
95da082
Merge branch 'main' into dag_run_migrate_conf_column_as_json
vatsrahul1001 Jan 7, 2025
8e1dc76
Merge branch 'main' of github.com:astronomer/airflow into dag_run_mig…
vatsrahul1001 Jan 8, 2025
0484923
Merge branch 'main' into dag_run_migrate_conf_column_as_json
vatsrahul1001 Jan 8, 2025
630e5f6
Merge branch 'main' of github.com:astronomer/airflow into dag_run_mig…
vatsrahul1001 Jan 8, 2025
ccf7c2f
Merge branch 'dag_run_migrate_conf_column_as_json' of github.com:astr…
vatsrahul1001 Jan 8, 2025
b30f8c1
Merge branch 'main' of github.com:astronomer/airflow into dag_run_mig…
vatsrahul1001 Jan 8, 2025
331455f
Merge branch 'main' into dag_run_migrate_conf_column_as_json
vatsrahul1001 Jan 13, 2025
80ca6ba
Merge branch 'main' into dag_run_migrate_conf_column_as_json
vatsrahul1001 Jan 13, 2025
56abe3a
Merge branch 'main' into dag_run_migrate_conf_column_as_json
vatsrahul1001 Jan 13, 2025
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,146 @@
#
# 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: 38770795785f
Create Date: 2024-12-01 08:33:15.425141

"""

from __future__ import annotations

import json
import pickle
from textwrap import dedent

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

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


def upgrade():
"""Apply remove pickled data from dagrun table."""
conn = op.get_bind()
conf_type = sa.JSON().with_variant(postgresql.JSONB, "postgresql")
op.add_column("dag_run", sa.Column("conf_json", conf_type, nullable=True))

if context.is_offline_mode():
print(
dedent("""
------------
-- WARNING: Unable to migrate the data in the 'conf' column while in offline mode!
-- The 'conf' column will be set to NULL in offline mode.
-- Avoid using offline mode if you need to retain 'conf' values.
ephraimbuddy marked this conversation as resolved.
Show resolved Hide resolved
------------
""")
)
else:
BATCH_SIZE = 2
offset = 0
while True:
rows = conn.execute(
text(
f"SELECT id,conf FROM dag_run WHERE conf IS not NULL order by id LIMIT {BATCH_SIZE} OFFSET {offset}"
)
).fetchall()
if not rows:
break
for row in rows:
row_id, pickle_data = row

try:
original_data = pickle.loads(pickle_data)
json_data = json.dumps(original_data)
conn.execute(
text("""
UPDATE dag_run
SET conf_json = :json_data
WHERE id = :id
"""),
{"json_data": json_data, "id": row_id},
)
except Exception as e:
print(f"Error converting dagrun conf to json for dagrun ID {row_id}: {e}")
continue
offset += BATCH_SIZE

op.drop_column("dag_run", "conf")

op.alter_column("dag_run", "conf_json", existing_type=conf_type, new_column_name="conf")


def downgrade():
"""Unapply Remove pickled data from dagrun table."""
conn = op.get_bind()
conf_type = sa.LargeBinary().with_variant(postgresql.BYTEA, "postgresql")
op.add_column("dag_run", sa.Column("conf_pickle", conf_type, nullable=True))
vatsrahul1001 marked this conversation as resolved.
Show resolved Hide resolved

if context.is_offline_mode():
print(
dedent("""
------------
-- WARNING: Unable to migrate the data in the 'conf' column while in offline mode!
-- The 'conf' column will be set to NULL in offline mode.
-- Avoid using offline mode if you need to retain 'conf' values.
------------
""")
)

else:
BATCH_SIZE = 2
vatsrahul1001 marked this conversation as resolved.
Show resolved Hide resolved
offset = 0
while True:
rows = conn.execute(
text(
f"SELECT id,conf FROM dag_run WHERE conf IS not NULL order by id LIMIT {BATCH_SIZE} OFFSET {offset}"
)
).fetchall()
if not rows:
break
for row in rows:
row_id, json_data = row

try:
pickled_data = pickle.dumps(json_data, protocol=pickle.HIGHEST_PROTOCOL)
ephraimbuddy marked this conversation as resolved.
Show resolved Hide resolved
conn.execute(
text("""
UPDATE dag_run
SET conf_pickle = :pickle_data
WHERE id = :id
"""),
{"pickle_data": pickled_data, "id": row_id},
)
except Exception as e:
print(f"Error pickling dagrun conf for dagrun ID {row_id}: {e}")
continue
offset += BATCH_SIZE

op.drop_column("dag_run", "conf")

op.alter_column("dag_run", "conf_pickle", existing_type=conf_type, new_column_name="conf")
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 @@ -45,6 +45,7 @@
tuple_,
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 @@ -138,7 +139,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"))
vatsrahul1001 marked this conversation as resolved.
Show resolved Hide resolved
# 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": "38770795785f",
"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 @@
f4ad824c8d9ff45e86002506edd83b540a88dab45bb292b1af96cd86dec5ecab
a78bedd5d2f54614a920db77044f6013b86de9792ee8fafc6a7e3f30fbd4c602
2 changes: 1 addition & 1 deletion docs/apache-airflow/img/airflow_erd.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
+=========================+==================+===================+==============================================================+
| ``38770795785f`` (head) | ``5c9c0231baa2`` | ``3.0.0`` | Add asset reference models. |
| ``e39a26ac59f6`` (head) | ``38770795785f`` | ``3.0.0`` | remove pickled data from dagrun table. |
+-------------------------+------------------+-------------------+--------------------------------------------------------------+
| ``38770795785f`` | ``5c9c0231baa2`` | ``3.0.0`` | Add asset reference models. |
+-------------------------+------------------+-------------------+--------------------------------------------------------------+
| ``5c9c0231baa2`` | ``237cef8dfea1`` | ``3.0.0`` | Remove processor_subdir. |
+-------------------------+------------------+-------------------+--------------------------------------------------------------+
Expand Down
5 changes: 5 additions & 0 deletions newsfragments/44533.significant.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
During offline migration, ``DagRun.conf`` is cleared

.. Provide additional contextual information

The ``conf`` column is changing from pickle to json, thus, the values in that column cannot be migrated during offline migrations. If you want to retain ``conf`` values for existing DagRuns, you must do a normal, non-offline, migration.