-
Notifications
You must be signed in to change notification settings - Fork 17.4k
Add TaskMap and TaskInstance.map_id #20286
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
Changes from all commits
c8ba1ec
8a66a1b
d7e7450
d97c086
74dd354
fff3a1e
dcfd72d
be4212a
f726cf6
f1bd8d3
9e0f71d
0ab3226
d05053d
3fe7283
3c0941f
50c2cd7
9446b31
338a457
b7038e9
02b6bb4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| # | ||
| # 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. | ||
|
|
||
| """Add TaskMap and map_index on TaskInstance. | ||
|
|
||
| Revision ID: e655c0453f75 | ||
| Revises: 587bdf053233 | ||
| Create Date: 2021-12-13 22:59:41.052584 | ||
| """ | ||
|
|
||
| from alembic import op | ||
| from sqlalchemy import Column, ForeignKeyConstraint, Integer | ||
|
|
||
| from airflow.models.base import StringID | ||
| from airflow.utils.sqlalchemy import ExtendedJSON | ||
|
|
||
| # Revision identifiers, used by Alembic. | ||
| revision = "e655c0453f75" | ||
| down_revision = "587bdf053233" | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
|
|
||
| def upgrade(): | ||
| """Add TaskMap and map_index on TaskInstance.""" | ||
| # We need to first remove constraints on task_reschedule since they depend on task_instance. | ||
| with op.batch_alter_table("task_reschedule") as batch_op: | ||
| batch_op.drop_constraint("task_reschedule_ti_fkey", "foreignkey") | ||
| batch_op.drop_index("idx_task_reschedule_dag_task_run") | ||
|
|
||
| # Change task_instance's primary key. | ||
| with op.batch_alter_table("task_instance") as batch_op: | ||
| # I think we always use this name for TaskInstance after 7b2661a43ba3? | ||
| batch_op.drop_constraint("task_instance_pkey", type_="primary") | ||
| batch_op.add_column(Column("map_index", Integer, nullable=False, default=-1)) | ||
| batch_op.create_primary_key("task_instance_pkey", ["dag_id", "task_id", "run_id", "map_index"]) | ||
|
|
||
| # Re-create task_reschedule's constraints. | ||
| with op.batch_alter_table("task_reschedule") as batch_op: | ||
| batch_op.add_column(Column("map_index", Integer, nullable=False, default=-1)) | ||
| batch_op.create_foreign_key( | ||
| "task_reschedule_ti_fkey", | ||
| "task_instance", | ||
| ["dag_id", "task_id", "run_id", "map_index"], | ||
| ["dag_id", "task_id", "run_id", "map_index"], | ||
| ondelete="CASCADE", | ||
| ) | ||
| batch_op.create_index( | ||
| "idx_task_reschedule_dag_task_run", | ||
| ["dag_id", "task_id", "run_id", "map_index"], | ||
| unique=False, | ||
| ) | ||
|
|
||
| # Create task_map. | ||
| op.create_table( | ||
| "task_map", | ||
| Column("dag_id", StringID(), primary_key=True), | ||
| Column("task_id", StringID(), primary_key=True), | ||
| Column("run_id", StringID(), primary_key=True), | ||
| Column("map_index", Integer, primary_key=True), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not that it matters, but for consistency should we have the same default here as we do on TI's map_index column?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default on |
||
| Column("length", Integer, nullable=False), | ||
| Column("keys", ExtendedJSON, nullable=True), | ||
| ForeignKeyConstraint( | ||
| ["dag_id", "task_id", "run_id", "map_index"], | ||
| [ | ||
| "task_instance.dag_id", | ||
| "task_instance.task_id", | ||
| "task_instance.run_id", | ||
| "task_instance.map_index", | ||
| ], | ||
| name="task_map_task_instance_fkey", | ||
| ondelete="CASCADE", | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| def downgrade(): | ||
| """Remove TaskMap and map_index on TaskInstance.""" | ||
| op.drop_table("task_map") | ||
|
|
||
| with op.batch_alter_table("task_reschedule") as batch_op: | ||
| batch_op.drop_constraint("task_reschedule_ti_fkey", "foreignkey") | ||
| batch_op.drop_index("idx_task_reschedule_dag_task_run") | ||
| batch_op.drop_column("map_index") | ||
|
|
||
| op.execute("DELETE FROM task_instance WHERE map_index != -1") | ||
|
|
||
| with op.batch_alter_table("task_instance") as batch_op: | ||
| batch_op.drop_constraint("task_instance_pkey", type_="primary") | ||
| batch_op.drop_column("map_index") | ||
| batch_op.create_primary_key("task_instance_pkey", ["dag_id", "task_id", "run_id"]) | ||
|
|
||
| with op.batch_alter_table("task_reschedule") as batch_op: | ||
| batch_op.create_foreign_key( | ||
| "task_reschedule_ti_fkey", | ||
| "task_instance", | ||
| ["dag_id", "task_id", "run_id"], | ||
| ["dag_id", "task_id", "run_id"], | ||
| ondelete="CASCADE", | ||
| ) | ||
| batch_op.create_index( | ||
| "idx_task_reschedule_dag_task_run", | ||
| ["dag_id", "task_id", "run_id"], | ||
| unique=False, | ||
| ) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@uranusjr Did this need
server_default=-1instead? - re #20876There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m investigating if it works as expected on all engines (if not I’ll just do an
UPDATE SET map_index = -1after this line)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doing it with a server default is much preferred -- as otherwise an UPDATE requires us to re-write/touch every row, but a server default (at least on Postgres) doesn't.