anitakar commented on a change in pull request #7217: [AIRFLOW-5946] Store 
source code in db
URL: https://github.com/apache/airflow/pull/7217#discussion_r383340478
 
 

 ##########
 File path: airflow/migrations/versions/788fcbd36a03_add_source_code_table.py
 ##########
 @@ -0,0 +1,85 @@
+#
+# 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 source code table
+
+Revision ID: 788fcbd36a03
+Revises: a4c2fd67d16b
+Create Date: 2020-02-20 14:40:23.257645
+
+"""
+
+import sqlalchemy as sa
+from alembic import op
+from sqlalchemy.dialects import mysql
+
+# revision identifiers, used by Alembic.
+revision = '788fcbd36a03'
+down_revision = 'a4c2fd67d16b'
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+    """Apply add source code table"""
+    op.create_table('dag_code',  # pylint: disable=no-member
+                    sa.Column('fileloc', sa.String(length=2000), 
nullable=False),
+                    sa.Column('fileloc_hash', sa.Integer(), nullable=False),
+                    sa.Column('source_code', sa.Text(), nullable=False),
+                    sa.Column('last_updated', sa.DateTime(), nullable=False),
+                    sa.PrimaryKeyConstraint('fileloc', 'fileloc_hash'))
+
+    op.create_index(   # pylint: disable=no-member
+        'idx_fileloc_code_hash', 'dag_code', ['fileloc_hash'])
+
+    conn = op.get_bind()  # pylint: disable=no-member
+    if conn.dialect.name == "mysql":
+        conn.execute("SET time_zone = '+00:00'")
+        cur = conn.execute("SELECT @@explicit_defaults_for_timestamp")
+        res = cur.fetchall()
+        if res[0][0] == 0:
+            raise Exception(
+                "Global variable explicit_defaults_for_timestamp needs to be 
on (1) for mysql"
+            )
+
+        op.alter_column(  # pylint: disable=no-member
+            table_name="dag_code",
+            column_name="last_updated",
+            type_=mysql.TIMESTAMP(fsp=6),
+            nullable=False,
+        )
+    else:
+        # sqlite and mssql datetime are fine as is.  Therefore, not converting
+        if conn.dialect.name in ("sqlite", "mssql"):
+            return
+
+        # we try to be database agnostic, but not every db (e.g. sqlserver)
+        # supports per session time zones
+        if conn.dialect.name == "postgresql":
+            conn.execute("set timezone=UTC")
+
+        op.alter_column(  # pylint: disable=no-member
+            table_name="dag_code",
+            column_name="last_updated",
+            type_=sa.TIMESTAMP(timezone=True),
+        )
 
 Review comment:
   Done

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


With regards,
Apache Git Services

Reply via email to