Skip to content

Fix db migrate failure to Airflow 2.9.2 under PyMySQL driver - #70235

Open
Aaryan123456679 wants to merge 3 commits into
apache:mainfrom
Aaryan123456679:fix/mysql-pymysql-migration-43690
Open

Fix db migrate failure to Airflow 2.9.2 under PyMySQL driver#70235
Aaryan123456679 wants to merge 3 commits into
apache:mainfrom
Aaryan123456679:fix/mysql-pymysql-migration-43690

Conversation

@Aaryan123456679

Copy link
Copy Markdown
Contributor

Migrating the metadata database to Airflow 2.9.2 (revision 686269002441) raised pymysql.err.ProgrammingError on MySQL when using the PyMySQL driver, because the migration used MySQL's PREPARE/EXECUTE/DEALLOCATE PREPARE dynamic SQL to conditionally drop unique constraints on the connection and dag_run tables. This syntax is not supported by PyMySQL (see PyMySQL#202), so any deployment using PyMySQL as its MySQL driver could not migrate past this revision.

Replaced the dynamic SQL with a plain lookup of existing constraints from information_schema.TABLE_CONSTRAINTS, followed by conditional ALTER TABLE ... DROP INDEX statements — this works with any MySQL driver.

Verified against a real MySQL backend forced to use mysql+pymysql://: downgrading to bff083ad727d and re-running airflow db migrate now completes without error, and the expected unique constraints (connection_conn_id_uq, dag_run_dag_id_*_key) are present afterward.

closes: #43690


Was generative AI tooling used to co-author this PR?
  • Yes — Claude Code (Sonnet 5)

Generated-by: Claude Code (Sonnet 5) following the guidelines

@boring-cyborg boring-cyborg Bot added the area:db-migrations PRs with DB migration label Jul 22, 2026
@Aaryan123456679

Copy link
Copy Markdown
Contributor Author

Added a newsfragment for the release notes.

Upgrading through revision 686269002441 raised
pymysql.err.ProgrammingError because the migration used MySQL's
PREPARE/EXECUTE/DEALLOCATE PREPARE dynamic SQL to conditionally drop
unique constraints, which PyMySQL's driver does not support
(see PyMySQL/PyMySQL#202). Replaced with a
plain lookup of existing constraints from
information_schema.TABLE_CONSTRAINTS followed by conditional
ALTER TABLE ... DROP INDEX statements, which works with any MySQL
driver.

Verified against a real MySQL/PyMySQL backend: downgrading to
bff083ad727d and re-running db migrate now completes without error.

closes: apache#43690
The PyMySQL online-mode fix in this migration queries
information_schema.TABLE_CONSTRAINTS via conn.execute() to decide which
legacy unique constraints to drop. In Alembic's offline (--sql) mode
there is no live connection, so conn.execute() returns None and
iterating over it raises TypeError, breaking the CI "Test offline
migration" step. Guard the query-then-conditionally-drop logic behind
context.is_offline_mode(), falling back to the original dynamic
prepare/execute/deallocate SQL text for the offline branch, since that
text is only ever emitted as script output there rather than executed
directly.
@Aaryan123456679
Aaryan123456679 force-pushed the fix/mysql-pymysql-migration-43690 branch from 977c6d0 to 3caee08 Compare July 22, 2026 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:db-migrations PRs with DB migration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error migrating database from any lower Airflow version 2.x to 2.9.2

1 participant