|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | + |
| 19 | +""" |
| 20 | +add dag versioning. |
| 21 | +
|
| 22 | +Revision ID: 2b47dc6bc8df |
| 23 | +Revises: d03e4a635aa3 |
| 24 | +Create Date: 2024-10-09 05:44:04.670984 |
| 25 | +
|
| 26 | +""" |
| 27 | + |
| 28 | +from __future__ import annotations |
| 29 | + |
| 30 | +import sqlalchemy as sa |
| 31 | +from alembic import op |
| 32 | +from sqlalchemy_utils import UUIDType |
| 33 | + |
| 34 | +from airflow.migrations.db_types import StringID |
| 35 | +from airflow.models.base import naming_convention |
| 36 | +from airflow.utils import timezone |
| 37 | +from airflow.utils.sqlalchemy import UtcDateTime |
| 38 | + |
| 39 | +# revision identifiers, used by Alembic. |
| 40 | +revision = "2b47dc6bc8df" |
| 41 | +down_revision = "d03e4a635aa3" |
| 42 | +branch_labels = None |
| 43 | +depends_on = None |
| 44 | +airflow_version = "3.0.0" |
| 45 | + |
| 46 | + |
| 47 | +def upgrade(): |
| 48 | + """Apply add dag versioning.""" |
| 49 | + op.create_table( |
| 50 | + "dag_version", |
| 51 | + sa.Column("id", UUIDType(binary=False), nullable=False), |
| 52 | + sa.Column("version_number", sa.Integer(), nullable=False), |
| 53 | + sa.Column("version_name", StringID()), |
| 54 | + sa.Column("dag_id", StringID(), nullable=False), |
| 55 | + sa.Column("created_at", UtcDateTime(), nullable=False, default=timezone.utcnow), |
| 56 | + sa.ForeignKeyConstraint( |
| 57 | + ("dag_id",), ["dag.dag_id"], name=op.f("dag_version_dag_id_fkey"), ondelete="CASCADE" |
| 58 | + ), |
| 59 | + sa.PrimaryKeyConstraint("id", name=op.f("dag_version_pkey")), |
| 60 | + sa.UniqueConstraint("dag_id", "version_number", name="dag_id_v_name_v_number_unique_constraint"), |
| 61 | + ) |
| 62 | + with op.batch_alter_table("dag_code", recreate="always", naming_convention=naming_convention) as batch_op: |
| 63 | + batch_op.drop_constraint("dag_code_pkey", type_="primary") |
| 64 | + batch_op.add_column( |
| 65 | + sa.Column("id", UUIDType(binary=False), primary_key=True), insert_before="fileloc_hash" |
| 66 | + ) |
| 67 | + batch_op.create_primary_key("dag_code_pkey", ["id"]) |
| 68 | + batch_op.add_column(sa.Column("dag_version_id", UUIDType(binary=False), nullable=False)) |
| 69 | + batch_op.create_foreign_key( |
| 70 | + batch_op.f("dag_code_dag_version_id_fkey"), |
| 71 | + "dag_version", |
| 72 | + ["dag_version_id"], |
| 73 | + ["id"], |
| 74 | + ondelete="CASCADE", |
| 75 | + ) |
| 76 | + batch_op.create_unique_constraint("dag_code_dag_version_id_uq", ["dag_version_id"]) |
| 77 | + |
| 78 | + with op.batch_alter_table( |
| 79 | + "serialized_dag", recreate="always", naming_convention=naming_convention |
| 80 | + ) as batch_op: |
| 81 | + batch_op.drop_constraint("serialized_dag_pkey", type_="primary") |
| 82 | + batch_op.add_column(sa.Column("id", UUIDType(binary=False), primary_key=True)) |
| 83 | + batch_op.drop_index("idx_fileloc_hash") |
| 84 | + batch_op.drop_column("fileloc_hash") |
| 85 | + batch_op.drop_column("fileloc") |
| 86 | + batch_op.create_primary_key("serialized_dag_pkey", ["id"]) |
| 87 | + batch_op.add_column(sa.Column("dag_version_id", UUIDType(binary=False), nullable=False)) |
| 88 | + batch_op.create_foreign_key( |
| 89 | + batch_op.f("serialized_dag_dag_version_id_fkey"), |
| 90 | + "dag_version", |
| 91 | + ["dag_version_id"], |
| 92 | + ["id"], |
| 93 | + ondelete="CASCADE", |
| 94 | + ) |
| 95 | + batch_op.create_unique_constraint("serialized_dag_dag_version_id_uq", ["dag_version_id"]) |
| 96 | + |
| 97 | + with op.batch_alter_table("task_instance", schema=None) as batch_op: |
| 98 | + batch_op.add_column(sa.Column("dag_version_id", UUIDType(binary=False))) |
| 99 | + batch_op.create_foreign_key( |
| 100 | + batch_op.f("task_instance_dag_version_id_fkey"), |
| 101 | + "dag_version", |
| 102 | + ["dag_version_id"], |
| 103 | + ["id"], |
| 104 | + ondelete="CASCADE", |
| 105 | + ) |
| 106 | + |
| 107 | + with op.batch_alter_table("task_instance_history", schema=None) as batch_op: |
| 108 | + batch_op.add_column(sa.Column("dag_version_id", UUIDType(binary=False))) |
| 109 | + |
| 110 | + with op.batch_alter_table("dag_run", schema=None) as batch_op: |
| 111 | + batch_op.add_column(sa.Column("dag_version_id", UUIDType(binary=False))) |
| 112 | + batch_op.create_foreign_key( |
| 113 | + batch_op.f("dag_run_dag_version_id_fkey"), |
| 114 | + "dag_version", |
| 115 | + ["dag_version_id"], |
| 116 | + ["id"], |
| 117 | + ondelete="CASCADE", |
| 118 | + ) |
| 119 | + batch_op.drop_column("dag_hash") |
| 120 | + |
| 121 | + |
| 122 | +def downgrade(): |
| 123 | + """Unapply add dag versioning.""" |
| 124 | + with op.batch_alter_table("task_instance_history", schema=None) as batch_op: |
| 125 | + batch_op.drop_column("dag_version_id") |
| 126 | + |
| 127 | + with op.batch_alter_table("task_instance", schema=None) as batch_op: |
| 128 | + batch_op.drop_constraint(batch_op.f("task_instance_dag_version_id_fkey"), type_="foreignkey") |
| 129 | + batch_op.drop_column("dag_version_id") |
| 130 | + |
| 131 | + with op.batch_alter_table("dag_code", schema=None) as batch_op: |
| 132 | + batch_op.drop_column("id") |
| 133 | + batch_op.drop_constraint(batch_op.f("dag_code_dag_version_id_fkey"), type_="foreignkey") |
| 134 | + batch_op.drop_column("dag_version_id") |
| 135 | + batch_op.create_primary_key("dag_code_pkey", ["fileloc_hash"]) |
| 136 | + |
| 137 | + with op.batch_alter_table("serialized_dag", schema=None, naming_convention=naming_convention) as batch_op: |
| 138 | + batch_op.drop_column("id") |
| 139 | + batch_op.add_column(sa.Column("fileloc", sa.String(length=2000), autoincrement=False, nullable=False)) |
| 140 | + batch_op.add_column(sa.Column("fileloc_hash", sa.BIGINT(), autoincrement=False, nullable=False)) |
| 141 | + batch_op.create_index("idx_fileloc_hash", ["fileloc_hash"], unique=False) |
| 142 | + batch_op.create_primary_key("serialized_dag_pkey", ["dag_id"]) |
| 143 | + batch_op.drop_constraint(batch_op.f("serialized_dag_dag_version_id_fkey"), type_="foreignkey") |
| 144 | + batch_op.drop_column("dag_version_id") |
| 145 | + |
| 146 | + with op.batch_alter_table("dag_run", schema=None) as batch_op: |
| 147 | + batch_op.add_column(sa.Column("dag_hash", sa.String(length=32), autoincrement=False, nullable=True)) |
| 148 | + batch_op.drop_constraint(batch_op.f("dag_run_dag_version_id_fkey"), type_="foreignkey") |
| 149 | + batch_op.drop_column("dag_version_id") |
| 150 | + |
| 151 | + op.drop_table("dag_version") |
0 commit comments