Skip to content

Commit

Permalink
Mysql change xcom value col type for MySQL backend (apache#38401)
Browse files Browse the repository at this point in the history
* Change table Xcom value column value type to longblob from blob

Xcom value we can store Airflow metadata is a bit smaller
when we use MySQL as a database backend since by default,
the Sqlalchemy map LargeBinary to MySQL blob type
which can store up to 65,535 bytes. In this PR,
I'm proposing to change the value column type to
longblob for xcom table if a user using MySQL database backend
https://dev.mysql.com/doc/refman/8.0/en/string-type-syntax.html
  • Loading branch information
pankajastro authored and utkarsharma2 committed Apr 22, 2024
1 parent b2a976b commit 6bca782
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#
# 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.

"""Change value column type to longblob in xcom table for mysql
Revision ID: b4078ac230a1
Revises: 8e1c784a4fc7
Create Date: 2024-03-22 14:06:51.185268
"""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects.mysql import LONGBLOB

# revision identifiers, used by Alembic.
revision = 'b4078ac230a1'
down_revision = '8e1c784a4fc7'
branch_labels = None
depends_on = None
airflow_version = '2.9.0'


def upgrade():
"""Apply Change value column type to longblob in xcom table for mysql"""
conn = op.get_bind()
if conn.dialect.name == "mysql":
with op.batch_alter_table("xcom", schema=None) as batch_op:
batch_op.alter_column("value", type_=sa.LargeBinary().with_variant(LONGBLOB, "mysql"))


def downgrade():
"""Unapply Change value column type to longblob in xcom table for mysql"""
conn = op.get_bind()
if conn.dialect.name == "mysql":
with op.batch_alter_table("xcom", schema=None) as batch_op:
batch_op.alter_column("value", type_=sa.LargeBinary)
3 changes: 2 additions & 1 deletion airflow/models/xcom.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
delete,
text,
)
from sqlalchemy.dialects.mysql import LONGBLOB
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.orm import Query, reconstructor, relationship
from sqlalchemy.orm.exc import NoResultFound
Expand Down Expand Up @@ -89,7 +90,7 @@ class BaseXCom(TaskInstanceDependencies, LoggingMixin):
dag_id = Column(String(ID_LEN, **COLLATION_ARGS), nullable=False)
run_id = Column(String(ID_LEN, **COLLATION_ARGS), nullable=False)

value = Column(LargeBinary)
value = Column(LargeBinary().with_variant(LONGBLOB, "mysql"))
timestamp = Column(UtcDateTime, default=timezone.utcnow, nullable=False)

__table_args__ = (
Expand Down
1 change: 1 addition & 0 deletions docs/spelling_wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,7 @@ logformat
loglevel
Logstash
logstash
longblob
Lowin
lshift
lxml
Expand Down
5 changes: 5 additions & 0 deletions newsfragments/38401.significant.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Change xcom table column value type to longblob for MySQL backend.

Xcom table column ``value`` type has changed from ``blob`` to ``longblob``. This will allow you to store relatively big data in Xcom but process can take a significant amount of time if you have a lot of large data stored in Xcom.

To downgrade from revision: ``b4078ac230a1``, ensure that you don't have Xcom values larger than 65,535 bytes. Otherwise, you'll need to clean those rows or run ``airflow db clean xcom`` to clean the Xcom table.

0 comments on commit 6bca782

Please sign in to comment.