11# Copyright (c) 2020 Aiven, Helsinki, Finland. https://aiven.io/
2+ import datetime
3+
24from aiven_mysql_migrate import config
35from aiven_mysql_migrate .dump_tools import MySQLMigrationToolBase , get_dump_tool
46from aiven_mysql_migrate .enums import MySQLMigrateTool , MySQLMigrateMethod
810 SSLNotSupportedException , TooManyDatabasesException , UnsupportedBinLogFormatException , UnsupportedMySQLEngineException ,
911 UnsupportedMySQLVersionException , WrongMigrationConfigurationException
1012)
13+ from aiven_mysql_migrate .migration_error import MysqlMigrationError
1114from aiven_mysql_migrate .utils import MySQLConnectionInfo , PrivilegeCheckUser , select_global_var
1215from looseversion import LooseVersion
1316from pathlib import Path
@@ -41,6 +44,7 @@ def __init__(
4144 privilege_check_user : Optional [str ] = None ,
4245 output_meta_file : Optional [Path ] = None ,
4346 dump_tool : MySQLMigrateTool = MySQLMigrateTool .mysqldump ,
47+ output_error_file : Optional [Path ] = None ,
4448 ):
4549 self .dump_tool_name = dump_tool
4650 self .dump_tool : Optional [MySQLMigrationToolBase ] = None
@@ -61,6 +65,7 @@ def __init__(
6165 if privilege_check_user :
6266 self .privilege_check_user = PrivilegeCheckUser .parse (privilege_check_user )
6367 self .output_meta_file = output_meta_file
68+ self .output_error_file = output_error_file
6469
6570 def setup_signal_handlers (self ):
6671 signal .signal (signal .SIGINT , self ._stop_migration )
@@ -380,6 +385,24 @@ def _wait_for_replication(self, *, seconds_behind_master: int = 0, check_interva
380385
381386 def start (self , * ,
382387 migration_method : MySQLMigrateMethod , seconds_behind_master : int , stop_replication : bool = False ) -> None :
388+ try :
389+ self .start_migration (migration_method = migration_method ,
390+ seconds_behind_master = seconds_behind_master ,
391+ stop_replication = stop_replication )
392+ except Exception as e :
393+ if self .output_error_file is not None :
394+ with open (self .output_error_file , "w" , encoding = 'utf-8' ) as f :
395+ error = MysqlMigrationError (error_type = e .__class__ .__module__ + "." + e .__class__ .__name__ ,
396+ error_msg = str (e ),
397+ error_date = datetime .datetime .now (datetime .timezone .utc ))
398+ f .write (json .dumps (error .__dict__ , default = str ))
399+ raise
400+
401+ def start_migration (self ,
402+ * ,
403+ migration_method : MySQLMigrateMethod ,
404+ seconds_behind_master : int ,
405+ stop_replication : bool = False ) -> None :
383406 LOGGER .info ("Start migration of the following databases:" )
384407 for db in self .databases :
385408 LOGGER .info ("\t %s" , db )
0 commit comments