Skip to content

Commit

Permalink
(Re)fix Dangling rows moving with MySQL+Replication (#21061)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ashb authored Jan 24, 2022
1 parent 1169e3a commit 07ea9fc
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion airflow/utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())}"
)
Expand Down

0 comments on commit 07ea9fc

Please sign in to comment.