jedcunningham commented on code in PR #47592: URL: https://github.com/apache/airflow/pull/47592#discussion_r2145272243
########## airflow-core/src/airflow/migrations/versions/0071_3_1_0_make_bundle_name_not_nullable.py: ########## @@ -0,0 +1,94 @@ +# +# 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: 1b612ec87098 +Revises: 0242ac120002 +Create Date: 2025-05-20 21:13:32.525404 + +""" + +from __future__ import annotations + +import sqlalchemy as sa +from alembic import op +from sqlalchemy.sql import text + +# revision identifiers, used by Alembic. +revision = "1b612ec87098" +down_revision = "0242ac120002" +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: @ephraimbuddy don't you think we should make it nullable again so in a downgrade, we are still in a consistent state orm wise? Maybe it doesn't matter... ########## airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_assets.py: ########## @@ -437,6 +438,11 @@ def test_filter_assets_by_uri_pattern_works(self, test_client, params, expected_ def test_filter_assets_by_dag_ids_works(self, test_client, dag_ids, expected_num, session): session.query(DagModel).delete() session.commit() + bundle_name = "test_bundle" + orm_dag_bundle = DagBundleModel(name=bundle_name) Review Comment: You can probably simplify a lot of these tests using the [testing_dag_bundle fixture](https://github.com/apache/airflow/blob/5de7cda70478ff43c3b7fb995092900750fb7996/devel-common/src/tests_common/pytest_plugin.py#L2436). -- 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]
