Passed
Push — master ( cea4ff...5c2229 )
by Konrad
01:16
created

import_database_dump_file()   A

Complexity

Conditions 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 13
rs 10
c 0
b 0
f 0
cc 2
nop 2
1
#!/usr/bin/env python3
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
# -*- coding: future_fstrings -*-
3
4
from db_sync_tool.utility import parser, mode, system, helper, output
5
from db_sync_tool.database import utility
6
7
8
def create_origin_database_dump():
9
    """
10
    Creating the origin database dump file
11
    :return:
12
    """
13
    if not mode.is_import():
14
        parser.get_database_configuration(mode.Client.ORIGIN)
15
        utility.generate_database_dump_filename()
16
        helper.check_and_create_dump_dir(mode.Client.ORIGIN, helper.get_dump_dir(mode.Client.ORIGIN))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (101/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
17
18
        _dump_file_path = helper.get_dump_dir(mode.Client.ORIGIN) + utility.database_dump_file_name
19
20
        output.message(
21
            output.Subject.ORIGIN,
22
            f'Creating database dump {output.CliFormat.BLACK}{_dump_file_path}{output.CliFormat.ENDC}',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (103/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
23
            True
24
        )
25
        mode.run_command(
26
            helper.get_command('origin', 'mysqldump') + ' --no-tablespaces ' +
27
            utility. generate_mysql_credentials('origin') + ' ' +
28
            system.config['origin']['db']['name'] + ' ' +
29
            utility.generate_ignore_database_tables() +
30
            ' > ' + _dump_file_path,
31
            mode.Client.ORIGIN
32
        )
33
34
        utility.check_database_dump(mode.Client.ORIGIN, _dump_file_path)
35
        prepare_origin_database_dump()
36
37
38
def import_database_dump():
39
    """
40
    Importing the selected database dump file
41
    :return:
42
    """
43
    if not system.config['is_same_client'] and not mode.is_import():
44
        prepare_target_database_dump()
45
46
    if not system.config['keep_dump'] and not system.config['is_same_client']:
47
        output.message(
48
            output.Subject.TARGET,
49
            'Importing database dump',
50
            True
51
        )
52
53
        if not mode.is_import():
54
           _dump_path = helper.get_dump_dir(mode.Client.TARGET) + utility.database_dump_file_name
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 11 were found.
Loading history...
55
        else:
56
           _dump_path = system.config['import']
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 11 were found.
Loading history...
57
58
        utility.check_database_dump(mode.Client.TARGET, _dump_path)
59
60
        import_database_dump_file(mode.Client.TARGET, _dump_path)
61
62
    if 'after_dump' in system.config['target']:
63
        _after_dump = system.config['target']['after_dump']
64
        output.message(
65
            output.Subject.TARGET,
66
            f'Importing after_dump file {output.CliFormat.BLACK}{_after_dump}{output.CliFormat.ENDC}',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (102/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
67
            True
68
        )
69
70
        import_database_dump_file(mode.Client.TARGET, _after_dump)
71
72
73
def import_database_dump_file(client, filepath):
74
    """
75
    Import a database dump file
76
    :param client: String
77
    :param filepath: String
78
    :return:
79
    """
80
    if helper.check_file_exists(client, filepath):
81
        mode.run_command(
82
            helper.get_command(client, 'mysql') + ' ' +
83
            utility.generate_mysql_credentials(client) + ' ' +
84
            system.config[client]['db']['name'] + ' < ' + filepath,
85
            client
86
        )
87
88
89
def prepare_origin_database_dump():
90
    """
91
    Preparing the origin database dump file by compressing them as .tar.gz
92
    :return:
93
    """
94
    output.message(
95
        output.Subject.ORIGIN,
96
        'Compressing database dump',
97
        True
98
    )
99
    mode.run_command(
100
        helper.get_command(mode.Client.ORIGIN, 'tar') + ' cfvz ' + helper.get_dump_dir(
101
            mode.Client.ORIGIN) + utility.database_dump_file_name + '.tar.gz -C ' + helper.get_dump_dir(
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (104/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
102
            mode.Client.ORIGIN) + ' ' + utility.database_dump_file_name + ' > /dev/null',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 4 spaces).
Loading history...
103
        mode.Client.ORIGIN
104
    )
105
106
107
def prepare_target_database_dump():
108
    """
109
    Preparing the target database dump by the unpacked .tar.gz file
110
    :return:
111
    """
112
    output.message(output.Subject.TARGET, 'Extracting database dump', True)
113
    mode.run_command(
114
        helper.get_command('target', 'tar') + ' xzf ' + helper.get_dump_dir(
115
            mode.Client.TARGET) + utility.database_dump_file_name + '.tar.gz -C ' + helper.get_dump_dir(
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (104/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
116
            mode.Client.TARGET) + ' > /dev/null',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 4 spaces).
Loading history...
117
        mode.Client.TARGET
118
    )
119