kaxil commented on a change in pull request #17719:
URL: https://github.com/apache/airflow/pull/17719#discussion_r693006135



##########
File path: 
airflow/migrations/versions/7b2661a43ba3_taskinstance_keyed_to_dagrun.py
##########
@@ -0,0 +1,211 @@
+#
+# 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.
+
+"""TaskInstance keyed to DagRun
+
+Revision ID: 7b2661a43ba3
+Revises: 142555e44c17
+Create Date: 2021-07-15 15:26:12.710749
+
+"""
+
+import sqlalchemy as sa
+from alembic import op
+from sqlalchemy.sql import and_, column, select, table
+
+from airflow.models.base import COLLATION_ARGS
+
+ID_LEN = 250
+
+# revision identifiers, used by Alembic.
+revision = '7b2661a43ba3'
+down_revision = '142555e44c17'
+branch_labels = None
+depends_on = None
+
+
+def _mssql_datetime():
+    from sqlalchemy.dialects import mssql
+
+    return mssql.DATETIME2(precision=6)
+
+
+# Just Enough Table to run the conditions for update.
+task_instance = table(
+    'task_instance',
+    column('task_id', sa.String),
+    column('dag_id', sa.String),
+    column('run_id', sa.String),
+    column('execution_date', sa.TIMESTAMP),
+)
+task_reschedule = table(
+    'task_reschedule',
+    column('task_id', sa.String),
+    column('dag_id', sa.String),
+    column('run_id', sa.String),
+    column('execution_date', sa.TIMESTAMP),
+)
+dag_run = table(
+    'dag_run',
+    column('dag_id', sa.String),
+    column('run_id', sa.String),
+    column('execution_date', sa.TIMESTAMP),
+)
+
+
+def upgrade():
+    """Apply TaskInstance keyed to DagRun"""
+    dialect_name = op.get_bind().dialect.name
+
+    run_id_col_type = sa.String(length=ID_LEN, **COLLATION_ARGS)
+
+    # First create column nullable
+    op.add_column('task_instance', sa.Column('run_id', run_id_col_type, 
nullable=True))
+    op.add_column('task_reschedule', sa.Column('run_id', run_id_col_type, 
nullable=True))
+
+    # Then update the new column by selecting the right value from DagRun
+    update_query = _multi_table_update(dialect_name, task_instance, 
task_instance.c.run_id)
+    op.execute(update_query)
+
+    #
+    # TaskReschedule has a FK to TaskInstance, so we have to update that before
+    # we can drop the TI.execution_date column
+
+    update_query = _multi_table_update(dialect_name, task_reschedule, 
task_reschedule.c.run_id)
+    op.execute(update_query)
+
+    with op.batch_alter_table('task_reschedule', schema=None) as batch_op:
+        batch_op.alter_column('run_id', existing_type=run_id_col_type, 
existing_nullable=True, nullable=False)
+
+        batch_op.drop_constraint('task_reschedule_dag_task_date_fkey', 
'foreign')
+        batch_op.drop_index('idx_task_reschedule_dag_task_date')
+
+    with op.batch_alter_table('task_instance', schema=None) as batch_op:
+        # Then make it non-nullable
+        batch_op.alter_column('run_id', existing_type=run_id_col_type, 
existing_nullable=True, nullable=False)
+
+        # TODO: Is this right for non-postgres?
+        batch_op.drop_constraint('task_instance_pkey', type_='primary')

Review comment:
       https://alembic.sqlalchemy.org/en/latest/naming.html




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