kaxil commented on code in PR #44166:
URL: https://github.com/apache/airflow/pull/44166#discussion_r1848503768


##########
airflow/migrations/versions/0049_3_0_0_remove_pickled_data_from_xcom_table.py:
##########
@@ -0,0 +1,181 @@
+#
+# 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 xcom table.
+
+Revision ID: eed27faa34e3
+Revises: 9fc3fc5de720
+Create Date: 2024-11-18 18:41:50.849514
+
+"""
+
+from __future__ import annotations
+
+import sqlalchemy as sa
+from alembic import op
+from sqlalchemy import text
+from sqlalchemy.dialects.mysql import LONGBLOB
+
+from airflow.migrations.db_types import TIMESTAMP, StringID
+
+revision = "eed27faa34e3"
+down_revision = "9fc3fc5de720"
+branch_labels = None
+depends_on = None
+airflow_version = "3.0.0"
+
+
+def upgrade():
+    """Apply Remove pickled data from xcom table."""
+    # Summary of the change:
+    # 1. Create an archived table (`_xcom_archive`) to store the current 
"pickled" data in the xcom table
+    # 2. Extract and archive the pickled data using the condition
+    # 3. Delete the pickled data from the xcom table so that we can update the 
column type
+    # 4. Update the XCom.value column type to JSON from LargeBinary/LongBlob
+
+    conn = op.get_bind()
+    dialect = conn.dialect.name
+
+    # Create an archived table to store the current data
+    op.create_table(
+        "_xcom_archive",
+        sa.Column("dag_run_id", sa.Integer(), nullable=False, 
primary_key=True),
+        sa.Column("task_id", StringID(length=250), nullable=False, 
primary_key=True),
+        sa.Column("map_index", sa.Integer(), nullable=False, 
server_default=sa.text("-1"), primary_key=True),
+        sa.Column("key", StringID(length=512), nullable=False, 
primary_key=True),
+        sa.Column("dag_id", StringID(length=250), nullable=False),
+        sa.Column("run_id", StringID(length=250), nullable=False),
+        sa.Column("value", sa.LargeBinary().with_variant(LONGBLOB, "mysql"), 
nullable=True),
+        sa.Column("timestamp", TIMESTAMP(), nullable=False),
+        sa.PrimaryKeyConstraint("dag_run_id", "task_id", "map_index", "key"),
+    )
+
+    # Condition to detect pickled data for different databases
+    condition_templates = {
+        "postgresql": "get_byte(value, 0) = 128",
+        "mysql": "HEX(value) LIKE '80%'",

Review Comment:
   good point, fixed in 
https://github.com/apache/airflow/pull/44166/commits/e38956e5cc21334aa50babc569f9d2cb7d24d897



##########
airflow/migrations/versions/0049_3_0_0_remove_pickled_data_from_xcom_table.py:
##########
@@ -0,0 +1,181 @@
+#
+# 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 xcom table.
+
+Revision ID: eed27faa34e3
+Revises: 9fc3fc5de720
+Create Date: 2024-11-18 18:41:50.849514
+
+"""
+
+from __future__ import annotations
+
+import sqlalchemy as sa
+from alembic import op
+from sqlalchemy import text
+from sqlalchemy.dialects.mysql import LONGBLOB
+
+from airflow.migrations.db_types import TIMESTAMP, StringID
+
+revision = "eed27faa34e3"
+down_revision = "9fc3fc5de720"
+branch_labels = None
+depends_on = None
+airflow_version = "3.0.0"
+
+
+def upgrade():
+    """Apply Remove pickled data from xcom table."""
+    # Summary of the change:
+    # 1. Create an archived table (`_xcom_archive`) to store the current 
"pickled" data in the xcom table
+    # 2. Extract and archive the pickled data using the condition
+    # 3. Delete the pickled data from the xcom table so that we can update the 
column type
+    # 4. Update the XCom.value column type to JSON from LargeBinary/LongBlob
+
+    conn = op.get_bind()
+    dialect = conn.dialect.name
+
+    # Create an archived table to store the current data
+    op.create_table(
+        "_xcom_archive",
+        sa.Column("dag_run_id", sa.Integer(), nullable=False, 
primary_key=True),
+        sa.Column("task_id", StringID(length=250), nullable=False, 
primary_key=True),
+        sa.Column("map_index", sa.Integer(), nullable=False, 
server_default=sa.text("-1"), primary_key=True),
+        sa.Column("key", StringID(length=512), nullable=False, 
primary_key=True),
+        sa.Column("dag_id", StringID(length=250), nullable=False),
+        sa.Column("run_id", StringID(length=250), nullable=False),
+        sa.Column("value", sa.LargeBinary().with_variant(LONGBLOB, "mysql"), 
nullable=True),
+        sa.Column("timestamp", TIMESTAMP(), nullable=False),
+        sa.PrimaryKeyConstraint("dag_run_id", "task_id", "map_index", "key"),
+    )
+
+    # Condition to detect pickled data for different databases
+    condition_templates = {
+        "postgresql": "get_byte(value, 0) = 128",
+        "mysql": "HEX(value) LIKE '80%'",
+        "sqlite": "substr(value, 1, 1) = char(128)",
+    }
+
+    condition = condition_templates.get(dialect)
+    if not condition:
+        raise RuntimeError(f"Unsupported dialect: {dialect}")
+
+    # Key is a reserved keyword in MySQL, so we need to quote it
+    quoted_key = "`key`" if dialect == "mysql" else '"key"'

Review Comment:
   fixed in 
https://github.com/apache/airflow/pull/44166/commits/e38956e5cc21334aa50babc569f9d2cb7d24d897



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