Skip to content

Commit 4a7cb7d

Browse files
authored
Merge pull request #149 from aiven/kmichel-lenient-rebuild
Rebuild tables without primary key or with invalid dates #149
2 parents d1e57bf + bf3757f commit 4a7cb7d

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

myhoard/restore_coordinator.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,15 @@ def rebuild_tables(self) -> None:
450450
self._ensure_mysql_server_is_started(with_binlog=False, with_gtids=False)
451451
# Rebuilding very large tables can be slow
452452
with self._mysql_cursor(timeout=3600.0) as cursor:
453+
cursor.execute("SELECT @@SESSION.sql_mode")
454+
unwanted_sql_modes = {"STRICT_ALL_TABLES", " STRICT_TRANS_TABLES", "NO_ZERO_DATE", "NO_ZERO_IN_DATE"}
455+
sql_mode = cursor.fetchone()["@@SESSION.sql_mode"]
456+
lenient_sql_mode = ",".join([mode for mode in sql_mode.split(",") if mode not in unwanted_sql_modes])
457+
if lenient_sql_mode != sql_mode:
458+
self.log.info("Switching SQL mode from %r to %r during tables rebuild", sql_mode, lenient_sql_mode)
459+
cursor.execute("SET SESSION sql_mode = %s", (lenient_sql_mode,))
460+
self.log.info("Disabling requirement for primary keys during tables rebuild")
461+
cursor.execute("SET SESSION sql_require_primary_key = false")
453462
cursor.execute(
454463
"SELECT TABLE_SCHEMA,TABLE_NAME,TABLE_ROWS,AVG_ROW_LENGTH"
455464
" FROM INFORMATION_SCHEMA.TABLES WHERE ENGINE='InnoDB'"

test/test_restore_coordinator.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ def _restore_coordinator_sequence(
4949
cursor.execute("CREATE TABLE t0 (id INTEGER PRIMARY KEY, data TEXT)")
5050
cursor.execute("CREATE TABLE t1 (id INTEGER PRIMARY KEY, data TEXT)")
5151
cursor.execute("CREATE TABLE t2 (id INTEGER PRIMARY KEY, data TEXT)")
52+
cursor.execute("SET SESSION sql_require_primary_key = false")
53+
cursor.execute("SET SESSION sql_mode = ''")
54+
cursor.execute("CREATE TABLE bad0 (id INTEGER, data TEXT)")
55+
cursor.execute("CREATE TABLE bad1 (id INTEGER, data DATETIME default '0000-00-00 00:00:00')")
5256
cursor.execute("COMMIT")
5357

5458
private_key_pem, public_key_pem = generate_rsa_key_pair()
@@ -321,6 +325,8 @@ def stream_is_closed(stream):
321325
assert rc.state_manager.update_log.count({"last_rebuilt_table": "`db1`.`t0`"}) == 1
322326
assert rc.state_manager.update_log.count({"last_rebuilt_table": "`db1`.`t1`"}) == 2
323327
assert rc.state_manager.update_log.count({"last_rebuilt_table": "`db1`.`t2`"}) == 1
328+
assert rc.state_manager.update_log.count({"last_rebuilt_table": "`db1`.`bad0`"}) == 1
329+
assert rc.state_manager.update_log.count({"last_rebuilt_table": "`db1`.`bad1`"}) == 1
324330
else:
325331
assert rc.state["restore_errors"] == 0
326332
assert rc.state["remote_read_errors"] == 0

0 commit comments

Comments
 (0)