Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ORM vs migration files inconsistencies #44221

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ def downgrade():
if conn.dialect.name == "mssql":
with op.batch_alter_table("log") as batch_op:
batch_op.drop_index("idx_log_event")
batch_op.alter_column("event", type_=sa.String(30), nullable=False)
batch_op.alter_column("event", type_=sa.String(30))
batch_op.create_index("idx_log_event", ["event"])
else:
with op.batch_alter_table("log") as batch_op:
batch_op.alter_column("event", type_=sa.String(30), nullable=False)
batch_op.alter_column("event", type_=sa.String(30))
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ def upgrade():
with op.batch_alter_table("task_instance_note", schema=None) as batch_op:
batch_op.drop_constraint("task_instance_note_user_fkey", type_="foreignkey")

if op.get_bind().dialect.name == "mysql":
with op.batch_alter_table("dag_run_note", schema=None) as batch_op:
batch_op.drop_index("dag_run_note_user_fkey")

with op.batch_alter_table("task_instance_note", schema=None) as batch_op:
batch_op.drop_index("task_instance_note_user_fkey")


def downgrade():
"""Unapply Drop ab_user.id foreign key."""
Expand All @@ -53,3 +60,10 @@ def downgrade():

with op.batch_alter_table("dag_run_note", schema=None) as batch_op:
batch_op.create_foreign_key("dag_run_note_user_fkey", "ab_user", ["user_id"], ["id"])

if op.get_bind().dialect.name == "mysql":
with op.batch_alter_table("task_instance_note", schema=None) as batch_op:
batch_op.create_index("task_instance_note_user_fkey", ["user_id"], unique=False)

with op.batch_alter_table("dag_run_note", schema=None) as batch_op:
batch_op.create_index("dag_run_note_user_fkey", ["user_id"], unique=False)
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,24 @@

def upgrade():
with op.batch_alter_table("dag_run", schema=None) as batch_op:
batch_op.alter_column("execution_date", new_column_name="logical_date", existing_type=sa.TIMESTAMP)
batch_op.alter_column(
"execution_date",
new_column_name="logical_date",
existing_type=sa.TIMESTAMP,
existing_nullable=False,
)
with op.batch_alter_table("dag_run", schema=None) as batch_op:
batch_op.drop_constraint("dag_run_dag_id_execution_date_key", type_="unique")


def downgrade():
with op.batch_alter_table("dag_run", schema=None) as batch_op:
batch_op.alter_column("logical_date", new_column_name="execution_date", existing_type=sa.TIMESTAMP)
batch_op.alter_column(
"logical_date",
new_column_name="execution_date",
existing_type=sa.TIMESTAMP,
existing_nullable=False,
)
with op.batch_alter_table("dag_run", schema=None) as batch_op:
batch_op.create_unique_constraint(
"dag_run_dag_id_execution_date_key",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ def upgrade():
op.create_table(
"backfill",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("dag_id", sa.String(length=250), nullable=True),
sa.Column("dag_id", sa.String(length=250), nullable=False),
sa.Column("from_date", airflow.utils.sqlalchemy.UtcDateTime(timezone=True), nullable=False),
sa.Column("to_date", airflow.utils.sqlalchemy.UtcDateTime(timezone=True), nullable=False),
sa.Column("dag_run_conf", sqlalchemy_jsonfield.jsonfield.JSONField(), nullable=True),
sa.Column("dag_run_conf", sqlalchemy_jsonfield.jsonfield.JSONField(), nullable=False),
sa.Column("is_paused", sa.Boolean(), nullable=True),
sa.Column("max_active_runs", sa.Integer(), nullable=False),
sa.Column("created_at", airflow.utils.sqlalchemy.UtcDateTime(timezone=True), nullable=False),
Expand All @@ -65,6 +65,10 @@ def upgrade():
sa.Column("sort_ordinal", sa.Integer(), nullable=False),
sa.PrimaryKeyConstraint("id", name=op.f("backfill_dag_run_pkey")),
sa.UniqueConstraint("backfill_id", "dag_run_id", name="ix_bdr_backfill_id_dag_run_id"),
sa.ForeignKeyConstraint(
["backfill_id"], ["backfill.id"], name="bdr_backfill_fkey", ondelete="cascade"
),
sa.ForeignKeyConstraint(["dag_run_id"], ["dag_run.id"], name="bdr_dag_run_fkey", ondelete="set null"),
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ def upgrade():
# Add 'name' column. Set it to nullable for now.
with op.batch_alter_table("dataset", schema=None) as batch_op:
batch_op.add_column(sa.Column("name", _STRING_COLUMN_TYPE))
batch_op.add_column(
sa.Column("group", _STRING_COLUMN_TYPE, default=str, server_default="", nullable=False)
)
batch_op.add_column(sa.Column("group", _STRING_COLUMN_TYPE, default="", nullable=False))
# Fill name from uri column.
with Session(bind=op.get_bind()) as session:
session.execute(sa.text("update dataset set name=uri"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
def upgrade():
"""Apply Add exception_reason and logical_date to BackfillDagRun."""
with op.batch_alter_table("backfill", schema=None) as batch_op:
batch_op.add_column(sa.Column("reprocess_behavior", sa.String(length=250), nullable=True))
batch_op.add_column(sa.Column("reprocess_behavior", sa.String(length=250), nullable=False))
with op.batch_alter_table("backfill_dag_run", schema=None) as batch_op:
batch_op.add_column(sa.Column("exception_reason", sa.String(length=250), nullable=True))
batch_op.add_column(sa.Column("logical_date", UtcDateTime(timezone=True), nullable=False))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,32 @@ def _get_type_id_column(dialect_name: str) -> sa.types.TypeEngine:
return sa.String(36)


def create_foreign_keys():
for fk in ti_fk_constraints:
if fk["table"] in ["task_instance_history", "task_map"]:
continue
with op.batch_alter_table(fk["table"]) as batch_op:
batch_op.create_foreign_key(
constraint_name=fk["fk"],
referent_table=ti_table,
local_cols=ti_fk_cols,
remote_cols=ti_fk_cols,
ondelete="CASCADE",
)
for fk in ti_fk_constraints:
if fk["table"] not in ["task_instance_history", "task_map"]:
continue
with op.batch_alter_table(fk["table"]) as batch_op:
batch_op.create_foreign_key(
constraint_name=fk["fk"],
referent_table=ti_table,
local_cols=ti_fk_cols,
remote_cols=ti_fk_cols,
ondelete="CASCADE",
onupdate="CASCADE",
)


def upgrade():
"""Add UUID primary key to task instance table."""
conn = op.get_bind()
Expand Down Expand Up @@ -232,15 +258,7 @@ def upgrade():
batch_op.create_primary_key("task_instance_pkey", ["id"])

# Create foreign key constraints
for fk in ti_fk_constraints:
with op.batch_alter_table(fk["table"]) as batch_op:
batch_op.create_foreign_key(
constraint_name=fk["fk"],
referent_table=ti_table,
local_cols=ti_fk_cols,
remote_cols=ti_fk_cols,
ondelete="CASCADE",
)
create_foreign_keys()


def downgrade():
Expand Down Expand Up @@ -270,12 +288,4 @@ def downgrade():
batch_op.create_primary_key("task_instance_pkey", ti_fk_cols)

# Re-add foreign key constraints
for fk in ti_fk_constraints:
with op.batch_alter_table(fk["table"]) as batch_op:
batch_op.create_foreign_key(
constraint_name=fk["fk"],
referent_table=ti_table,
local_cols=ti_fk_cols,
remote_cols=ti_fk_cols,
ondelete="CASCADE",
)
create_foreign_keys()
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ def upgrade():
op.execute(
"""
ALTER TABLE xcom
ALTER COLUMN value TYPE JSONB
ALTER COLUMN value TYPE JSON
USING CASE
WHEN value IS NOT NULL THEN CAST(CONVERT_FROM(value, 'UTF8') AS JSONB)
WHEN value IS NOT NULL THEN CAST(CONVERT_FROM(value, 'UTF8') AS JSON)
ELSE NULL
END
"""
Expand Down Expand Up @@ -136,6 +136,7 @@ def upgrade():
# Drop the old `value_old` column
with op.batch_alter_table("xcom", schema=None) as batch_op:
batch_op.drop_column("value_old")
op.drop_table("_xcom_archive")


def downgrade():
Expand Down
1 change: 0 additions & 1 deletion airflow/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def import_all_models():
import airflow.models.serialized_dag
import airflow.models.taskinstancehistory
import airflow.models.tasklog
import airflow.providers.fab.auth_manager.models


def __getattr__(name):
Expand Down
2 changes: 1 addition & 1 deletion airflow/models/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class AssetAliasModel(Base):
),
"mysql",
),
default=str,
default="",
nullable=False,
)

Expand Down
2 changes: 1 addition & 1 deletion docs/apache-airflow/img/airflow_erd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7748eec981f977cc97b852d1fe982aebe24ec2d090ae8493a65cea101f9d42a5
b12f6811bb7a340362e4b8774b3bb81db28a7d0258564b3431bd537368554cc3
4 changes: 3 additions & 1 deletion scripts/in_container/run_generate_migration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@

cd "${AIRFLOW_SOURCES}" || exit 1
cd "airflow" || exit 1
airflow db reset
airflow db reset -y
airflow db downgrade -n 2.10.3 -y
airflow db migrate -r heads
alembic revision --autogenerate -m "${@}"
2 changes: 2 additions & 0 deletions tests_common/test_utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ def initial_db_init():
from airflow.www.extensions.init_auth_manager import get_auth_manager

db.resetdb()
db.downgrade(to_revision="5f2621c13b39")
db.upgradedb(to_revision="head")
db.bootstrap_dagbag()
# minimal app to add roles
flask_app = Flask(__name__)
Expand Down
Loading