ephraimbuddy commented on code in PR #47592:
URL: https://github.com/apache/airflow/pull/47592#discussion_r2156617886


##########
airflow-core/src/airflow/migrations/versions/0074_3_1_0_make_bundle_name_not_nullable.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.
+
+"""
+Make bundle_name not nullable.
+
+Revision ID: 9abbd92d95c9
+Revises: 583e80dfcef4
+Create Date: 2025-06-18 20:46:43.538828
+"""
+
+from __future__ import annotations
+
+import sqlalchemy as sa
+from alembic import op
+from sqlalchemy.sql import text
+
+# revision identifiers, used by Alembic.
+revision = "9abbd92d95c9"
+down_revision = "583e80dfcef4"
+branch_labels = None
+depends_on = None
+airflow_version = "3.1.0"
+
+
+def upgrade():
+    """Apply Make bundle_name not nullable."""
+    dialect_name = op.get_bind().dialect.name
+    if dialect_name == "postgresql":
+        op.execute("""
+                    INSERT INTO dag_bundle (name) VALUES
+                      ('example_dags'),
+                      ('dags-folder')
+                    ON CONFLICT (name) DO NOTHING;
+                    """)
+    if dialect_name == "mysql":
+        op.execute("""
+                    INSERT IGNORE INTO dag_bundle (name) VALUES
+                      ('example_dags'),
+                      ('dags-folder');
+                    """)
+    if dialect_name == "sqlite":
+        op.execute("""
+                    INSERT OR IGNORE INTO dag_bundle (name) VALUES
+                      ('example_dags'),
+                      ('dags-folder');
+                    """)
+
+    conn = op.get_bind()
+    with op.batch_alter_table("dag", schema=None) as batch_op:
+        conn.execute(
+            text(
+                """
+                UPDATE dag
+                SET bundle_name =
+                    CASE
+                        WHEN fileloc LIKE '%/airflow/example_dags/%' THEN 
'example_dags'
+                        ELSE 'dags-folder'
+                    END
+                WHERE bundle_name IS NULL
+                """
+            )
+        )
+        # drop the foreign key temporarily and recreate it once both columns 
are changed
+        batch_op.drop_constraint(batch_op.f("dag_bundle_name_fkey"), 
type_="foreignkey")
+        batch_op.alter_column("bundle_name", nullable=False, 
existing_type=sa.String(length=250))
+
+    with op.batch_alter_table("dag_bundle", schema=None) as batch_op:
+        batch_op.alter_column("name", nullable=False, 
existing_type=sa.String(length=250))
+
+    with op.batch_alter_table("dag", schema=None) as batch_op:
+        batch_op.create_foreign_key(
+            batch_op.f("dag_bundle_name_fkey"), "dag_bundle", ["bundle_name"], 
["name"]
+        )
+
+
+def downgrade():
+    """NO downgrade because the primary key cannot be null."""

Review Comment:
   Let's rollback to making bundle_name nullable here. However, no need to 
nullify the data



-- 
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: commits-unsubscr...@airflow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to