Skip to content

Commit 287d856

Browse files
committed
Do not backfill old DagRun.created_at
Filling this field in the migration is much too costly. This field is now left as NULL for rows created prior to the migration. I couldn't find this field used anywhere.
1 parent b8c26ae commit 287d856

2 files changed

Lines changed: 9 additions & 13 deletions

File tree

airflow-core/src/airflow/migrations/versions/0106_3_2_0_add_partition_key_to_backfill_dag_run.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,9 @@
4444
def upgrade():
4545
"""Apply Add partition_key to backfill_dag_run."""
4646
op.add_column("dag_run", sa.Column("created_at", UtcDateTime(timezone=True), nullable=True))
47-
op.execute("update dag_run set created_at = run_after;")
48-
49-
with disable_sqlite_fkeys(op):
50-
with op.batch_alter_table("dag_run", schema=None) as batch_op:
51-
batch_op.alter_column("created_at", existing_type=UtcDateTime(timezone=True), nullable=False)
52-
53-
with op.batch_alter_table("backfill_dag_run", schema=None) as batch_op:
54-
batch_op.add_column(sa.Column("partition_key", StringID(), nullable=True))
55-
batch_op.alter_column("logical_date", existing_type=sa.TIMESTAMP(), nullable=True)
47+
with disable_sqlite_fkeys(op), op.batch_alter_table("backfill_dag_run", schema=None) as batch_op:
48+
batch_op.add_column(sa.Column("partition_key", StringID(), nullable=True))
49+
batch_op.alter_column("logical_date", existing_type=sa.TIMESTAMP(), nullable=True)
5650

5751

5852
def downgrade():
@@ -62,6 +56,5 @@ def downgrade():
6256
with op.batch_alter_table("backfill_dag_run", schema=None) as batch_op:
6357
batch_op.alter_column("logical_date", existing_type=sa.TIMESTAMP(), nullable=False)
6458
batch_op.drop_column("partition_key")
65-
66-
with op.batch_alter_table("dag_run", schema=None) as batch_op:
67-
batch_op.drop_column("created_at")
59+
with op.batch_alter_table("dag_run", schema=None) as batch_op:
60+
batch_op.drop_column("created_at")

airflow-core/src/airflow/models/dagrun.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,10 @@ class DagRun(Base, LoggingMixin):
191191
ForeignKey("log_template.id", name="task_instance_log_template_id_fkey", ondelete="NO ACTION"),
192192
default=select(func.max(LogTemplate.__table__.c.id)),
193193
)
194-
created_at: Mapped[datetime] = mapped_column(UtcDateTime, default=timezone.utcnow)
194+
# This is nullable because it's too costly to migrate dagruns created prior
195+
# to this column's addition (Airflow 3.2.0). If you want a reasonable
196+
# meaningful non-null value, use ``dr.created_at or dr.run_after``.
197+
created_at: Mapped[datetime] = mapped_column(UtcDateTime, nullable=True, default=timezone.utcnow)
195198
updated_at: Mapped[datetime] = mapped_column(
196199
UtcDateTime, default=timezone.utcnow, onupdate=timezone.utcnow
197200
)

0 commit comments

Comments
 (0)