From 2b755d8ade9717ea4c777a70da363b8fdec99c66 Mon Sep 17 00:00:00 2001 From: shivansh02 Date: Sun, 3 Mar 2024 23:15:07 +0530 Subject: [PATCH] custom show command for mysql and schema description --- borgmatic/config/schema.yaml | 6 +++--- borgmatic/hooks/mysql.py | 3 ++- tests/unit/hooks/test_mysql.py | 21 +++++++++++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/borgmatic/config/schema.yaml b/borgmatic/config/schema.yaml index 51278bf..c8288cf 100644 --- a/borgmatic/config/schema.yaml +++ b/borgmatic/config/schema.yaml @@ -1092,15 +1092,15 @@ properties: description: | Command to use instead of "mysqldump". This can be used to run a specific mysql_dump version (e.g., one inside - a running docker container). Defaults to "mysqldump". + a running container). Defaults to "mysqldump". example: docker exec mysql_container mysqldump mysql_command: type: string description: | Command to run instead of "mysql". This can be used to run a specific mysql - version (e.g., one inside a running docker - container). Defaults to "mysql". + version (e.g., one inside a running container). + Defaults to "mysql". example: docker exec mysql_container mysql format: type: string diff --git a/borgmatic/hooks/mysql.py b/borgmatic/hooks/mysql.py index 086e798..2086efb 100644 --- a/borgmatic/hooks/mysql.py +++ b/borgmatic/hooks/mysql.py @@ -35,8 +35,9 @@ def database_names_to_dump(database, extra_environment, log_prefix, dry_run): if dry_run: return () + mysql_show_command = database.get('mysql_command') or 'mysql' show_command = ( - ('mysql',) + (mysql_show_command,) + (tuple(database['list_options'].split(' ')) if 'list_options' in database else ()) + (('--host', database['hostname']) if 'hostname' in database else ()) + (('--port', str(database['port'])) if 'port' in database else ()) diff --git a/tests/unit/hooks/test_mysql.py b/tests/unit/hooks/test_mysql.py index 46d1777..771fc2f 100644 --- a/tests/unit/hooks/test_mysql.py +++ b/tests/unit/hooks/test_mysql.py @@ -142,6 +142,27 @@ def test_database_names_to_dump_runs_mysql_with_list_options(): assert module.database_names_to_dump(database, None, 'test.yaml', '') == ('foo', 'bar') +def test_database_names_to_dump_runs_non_default_mysql_with_list_options(): + database = { + 'name': 'all', + 'list_options': '--defaults-extra-file=my.cnf', + 'mysql_command': 'custom_mysql', + } + flexmock(module).should_receive('execute_command_and_capture_output').with_args( + extra_environment=None, + full_command=( + 'custom_mysql', # Custom MySQL command + '--defaults-extra-file=my.cnf', + '--skip-column-names', + '--batch', + '--execute', + 'show schemas', + ) + ).and_return(('foo\nbar')).once() + + assert module.database_names_to_dump(database, None, 'test.yaml', '') == ('foo', 'bar') + + def test_execute_dump_command_runs_mysqldump(): process = flexmock() flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')