mirror of
https://github.com/ansible-collections/community.mysql.git
synced 2025-04-22 10:21:29 -07:00
mysql_db: add chdir argument (#396)
This commit is contained in:
parent
2a3f8f6506
commit
8e79690a02
3 changed files with 61 additions and 0 deletions
2
changelogs/fragments/0-mysql_db_add_chdir_argument.yml
Normal file
2
changelogs/fragments/0-mysql_db_add_chdir_argument.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
minor_changes:
|
||||||
|
- mysql_db - add the ``chdir`` argument to avoid failings when a dump file contains relative paths (https://github.com/ansible-collections/community.mysql/issues/395).
|
|
@ -150,6 +150,12 @@ options:
|
||||||
type: bool
|
type: bool
|
||||||
default: no
|
default: no
|
||||||
version_added: '0.1.0'
|
version_added: '0.1.0'
|
||||||
|
chdir:
|
||||||
|
description:
|
||||||
|
- Changes the current working directory.
|
||||||
|
- Can be useful, for example, when I(state=import) and a dump file contains relative paths.
|
||||||
|
type: path
|
||||||
|
version_added: '3.4.0'
|
||||||
|
|
||||||
seealso:
|
seealso:
|
||||||
- module: community.mysql.mysql_info
|
- module: community.mysql.mysql_info
|
||||||
|
@ -562,6 +568,7 @@ def main():
|
||||||
restrict_config_file=dict(type='bool', default=False),
|
restrict_config_file=dict(type='bool', default=False),
|
||||||
check_implicit_admin=dict(type='bool', default=False),
|
check_implicit_admin=dict(type='bool', default=False),
|
||||||
config_overrides_defaults=dict(type='bool', default=False),
|
config_overrides_defaults=dict(type='bool', default=False),
|
||||||
|
chdir=dict(type='path'),
|
||||||
)
|
)
|
||||||
|
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
|
@ -610,6 +617,13 @@ def main():
|
||||||
restrict_config_file = module.params["restrict_config_file"]
|
restrict_config_file = module.params["restrict_config_file"]
|
||||||
check_implicit_admin = module.params['check_implicit_admin']
|
check_implicit_admin = module.params['check_implicit_admin']
|
||||||
config_overrides_defaults = module.params['config_overrides_defaults']
|
config_overrides_defaults = module.params['config_overrides_defaults']
|
||||||
|
chdir = module.params['chdir']
|
||||||
|
|
||||||
|
if chdir:
|
||||||
|
try:
|
||||||
|
os.chdir(chdir)
|
||||||
|
except Exception as e:
|
||||||
|
module.fail_json("Cannot change the current directory to %s: %s" % (chdir, e))
|
||||||
|
|
||||||
if len(db) > 1 and state == 'import':
|
if len(db) > 1 and state == 'import':
|
||||||
module.fail_json(msg="Multiple databases are not supported with state=import")
|
module.fail_json(msg="Multiple databases are not supported with state=import")
|
||||||
|
|
|
@ -416,6 +416,51 @@
|
||||||
that:
|
that:
|
||||||
- result is changed
|
- result is changed
|
||||||
|
|
||||||
|
########################
|
||||||
|
# Test import with chdir
|
||||||
|
|
||||||
|
- name: Create dir
|
||||||
|
file:
|
||||||
|
path: ~/subdir
|
||||||
|
state: directory
|
||||||
|
|
||||||
|
- name: Create test dump
|
||||||
|
shell: 'echo "SOURCE ./subdir_test.sql" > ~/original_test.sql'
|
||||||
|
|
||||||
|
- name: Create test source
|
||||||
|
shell: 'echo "SELECT 1" > ~/subdir/subdir_test.sql'
|
||||||
|
|
||||||
|
- name: Try to restore without chdir argument, must fail
|
||||||
|
mysql_db:
|
||||||
|
login_user: '{{ mysql_user }}'
|
||||||
|
login_password: '{{ mysql_password }}'
|
||||||
|
login_host: 127.0.0.1
|
||||||
|
login_port: '{{ mysql_primary_port }}'
|
||||||
|
name: '{{ db_name }}'
|
||||||
|
state: import
|
||||||
|
target: '~/original_test.sql'
|
||||||
|
ignore_errors: yes
|
||||||
|
register: result
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- result is failed
|
||||||
|
- result.msg is search('Failed to open file')
|
||||||
|
|
||||||
|
- name: Restore with chdir argument, must pass
|
||||||
|
mysql_db:
|
||||||
|
login_user: '{{ mysql_user }}'
|
||||||
|
login_password: '{{ mysql_password }}'
|
||||||
|
login_host: 127.0.0.1
|
||||||
|
login_port: '{{ mysql_primary_port }}'
|
||||||
|
name: '{{ db_name }}'
|
||||||
|
state: import
|
||||||
|
target: '~/original_test.sql'
|
||||||
|
chdir: ~/subdir
|
||||||
|
register: result
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- result is succeeded
|
||||||
|
|
||||||
##########
|
##########
|
||||||
# Clean up
|
# Clean up
|
||||||
##########
|
##########
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue