From 07ea9fcaa10fc1a8c43ef5f627360d4adb12115a Mon Sep 17 00:00:00 2001 From: Ash Berlin-Taylor Date: Mon, 24 Jan 2022 20:37:01 +0000 Subject: [PATCH] (Re)fix Dangling rows moving with MySQL+Replication (#21061) Splitting this CREATE TABLE AS SELECT query into two queries (CREATE TABLE LIKE .. followed by INSERT INTO) because the former doesn't play nicely with MySQL when replication is enabled --- airflow/utils/db.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/airflow/utils/db.py b/airflow/utils/db.py index 9e8e326d65d71..defc729ec2192 100644 --- a/airflow/utils/db.py +++ b/airflow/utils/db.py @@ -860,8 +860,15 @@ def _move_dangling_data_to_new_table( cte_sql = stmt.ctes[cte] session.execute(f"WITH {cte_sql} SELECT source.* INTO {target_table_name} FROM source") + elif dialect_name == "mysql": + # MySQL with replication needs this split in to two queries, so just do it for all MySQL + # ERROR 1786 (HY000): Statement violates GTID consistency: CREATE TABLE ... SELECT. + session.execute(f"CREATE TABLE {target_table_name} LIKE {source_table.name}") + session.execute( + f"INSERT INTO {target_table_name} {source_query.selectable.compile(bind=session.get_bind())}" + ) else: - # Postgres, MySQL and SQLite all support the same "create as select" + # Postgres and SQLite both support the same "CREATE TABLE a AS SELECT ..." syntax session.execute( f"CREATE TABLE {target_table_name} AS {source_query.selectable.compile(bind=session.get_bind())}" )