turbaszek commented on a change in pull request #8227:
URL: https://github.com/apache/airflow/pull/8227#discussion_r424973201



##########
File path: airflow/migrations/versions/3c20cacc0044_add_dagrun_run_type.py
##########
@@ -0,0 +1,93 @@
+#
+# 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 DagRun run_type
+
+Revision ID: 3c20cacc0044
+Revises: 952da73b5eff
+Create Date: 2020-04-08 13:35:25.671327
+
+"""
+
+import sqlalchemy as sa
+from alembic import op
+from sqlalchemy import Boolean, Column, Integer, PickleType, String
+from sqlalchemy.engine.reflection import Inspector
+from sqlalchemy.ext.declarative import declarative_base
+
+from airflow.models.base import ID_LEN
+from airflow.utils import timezone
+from airflow.utils.sqlalchemy import UtcDateTime
+from airflow.utils.state import State
+from airflow.utils.types import DagRunType
+
+# revision identifiers, used by Alembic.
+revision = "3c20cacc0044"
+down_revision = "952da73b5eff"
+branch_labels = None
+depends_on = None
+
+Base = declarative_base()
+
+
+class DagRun(Base):
+    """
+    DagRun describes an instance of a Dag. It can be created
+    by the scheduler (for regular runs) or by an external trigger
+    """
+    __tablename__ = "dag_run"
+
+    id = Column(Integer, primary_key=True)
+    dag_id = Column(String(ID_LEN))
+    execution_date = Column(UtcDateTime, default=timezone.utcnow)
+    start_date = Column(UtcDateTime, default=timezone.utcnow)
+    end_date = Column(UtcDateTime)
+    _state = Column('state', String(50), default=State.RUNNING)
+    run_id = Column(String(ID_LEN))
+    external_trigger = Column(Boolean, default=True)
+    run_type = Column(String(50))
+    conf = Column(PickleType)
+
+
+def upgrade():
+    """Apply Add DagRun run_type"""
+    op.add_column("dag_run", sa.Column("run_type", sa.String(length=50), 
nullable=True))
+
+    connection = op.get_bind()
+    sessionmaker = sa.orm.sessionmaker()
+    session = sessionmaker(bind=connection)
+    inspector = Inspector.from_engine(connection)
+    tables = inspector.get_table_names()
+
+    if 'dag_run' in tables:
+        for run_type in DagRunType:
+            
session.query(DagRun).filter(DagRun.run_id.like(f"{run_type.value}__%")).update(
+                {DagRun.run_type: run_type.value}, synchronize_session=False
+            )
+        session.commit()
+
+        session.query(DagRun).filter(DagRun.run_type.is_(None)).update(
+            {DagRun.run_type: DagRunType.MANUAL.value}, 
synchronize_session=False
+        )
+        session.commit()

Review comment:
       Should we also make it not nullable in model?




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

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to