Passed
Push — master ( 98c4ba...ef77bd )
by Konrad
08:25
created

import_database_dump()   C

Complexity

Conditions 10

Size

Total Lines 53
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 53
rs 5.9999
c 0
b 0
f 0
cc 10
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like db_sync_tool.database.process.import_database_dump() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
#!/usr/bin/env python3
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.database.utility -> db_sync_tool.utility.helper -> db_sync_tool.remote.utility).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.remote.client -> db_sync_tool.remote.utility).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.utility.log -> db_sync_tool.utility.system).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.database.utility -> db_sync_tool.utility.helper -> db_sync_tool.utility.output -> db_sync_tool.utility.log -> db_sync_tool.utility.system -> db_sync_tool.utility.parser -> db_sync_tool.recipes.symfony -> db_sync_tool.utility.mode -> db_sync_tool.remote.system -> db_sync_tool.remote.client -> db_sync_tool.remote.utility).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.database.utility -> db_sync_tool.utility.output -> db_sync_tool.utility.log -> db_sync_tool.utility.system -> db_sync_tool.utility.parser -> db_sync_tool.remote.utility).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.remote.client -> db_sync_tool.utility.output -> db_sync_tool.utility.log -> db_sync_tool.utility.system -> db_sync_tool.utility.parser).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.remote.client -> db_sync_tool.utility.output -> db_sync_tool.utility.mode -> db_sync_tool.remote.system).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.database.utility -> db_sync_tool.utility.mode -> db_sync_tool.remote.system -> db_sync_tool.utility.helper -> db_sync_tool.remote.utility).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.remote.system -> db_sync_tool.utility.helper -> db_sync_tool.utility.mode).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.database.utility -> db_sync_tool.utility.helper -> db_sync_tool.utility.output -> db_sync_tool.utility.log -> db_sync_tool.utility.system -> db_sync_tool.utility.parser -> db_sync_tool.recipes.typo3 -> db_sync_tool.utility.mode -> db_sync_tool.remote.system -> db_sync_tool.remote.client -> db_sync_tool.remote.utility).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.recipes.symfony -> db_sync_tool.utility.system -> db_sync_tool.utility.parser).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.database.utility -> db_sync_tool.utility.helper -> db_sync_tool.utility.output -> db_sync_tool.utility.log -> db_sync_tool.utility.system -> db_sync_tool.utility.parser -> db_sync_tool.recipes.drupal -> db_sync_tool.utility.mode -> db_sync_tool.remote.system -> db_sync_tool.remote.client -> db_sync_tool.remote.utility).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.database.utility -> db_sync_tool.utility.output -> db_sync_tool.utility.log -> db_sync_tool.utility.system -> db_sync_tool.utility.parser -> db_sync_tool.remote.client -> db_sync_tool.remote.utility).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.database.utility -> db_sync_tool.utility.helper -> db_sync_tool.utility.mode -> db_sync_tool.remote.system -> db_sync_tool.remote.client -> db_sync_tool.remote.utility).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.remote.utility -> db_sync_tool.utility.output -> db_sync_tool.utility.log -> db_sync_tool.utility.system -> db_sync_tool.utility.helper).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.database.utility -> db_sync_tool.utility.helper -> db_sync_tool.utility.system -> db_sync_tool.utility.parser -> db_sync_tool.remote.client -> db_sync_tool.remote.utility).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.utility.helper -> db_sync_tool.utility.system).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.database.utility -> db_sync_tool.utility.helper -> db_sync_tool.utility.output -> db_sync_tool.utility.log -> db_sync_tool.utility.system -> db_sync_tool.utility.parser -> db_sync_tool.recipes.wordpress -> db_sync_tool.utility.mode -> db_sync_tool.remote.system -> db_sync_tool.remote.client -> db_sync_tool.remote.utility).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.database.utility -> db_sync_tool.utility.helper -> db_sync_tool.utility.output -> db_sync_tool.utility.log -> db_sync_tool.utility.system -> db_sync_tool.utility.parser -> db_sync_tool.remote.client -> db_sync_tool.remote.utility).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.recipes.wordpress -> db_sync_tool.utility.system -> db_sync_tool.utility.parser).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.utility.helper -> db_sync_tool.utility.output -> db_sync_tool.utility.log -> db_sync_tool.utility.system).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.utility.log -> db_sync_tool.utility.system -> db_sync_tool.utility.output).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.database.utility -> db_sync_tool.utility.helper -> db_sync_tool.utility.system -> db_sync_tool.utility.output -> db_sync_tool.utility.mode -> db_sync_tool.remote.system -> db_sync_tool.remote.client -> db_sync_tool.remote.utility).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.database.utility -> db_sync_tool.utility.helper -> db_sync_tool.utility.output -> db_sync_tool.utility.mode -> db_sync_tool.remote.system -> db_sync_tool.remote.client -> db_sync_tool.remote.utility).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.remote.client -> db_sync_tool.remote.utility -> db_sync_tool.utility.output -> db_sync_tool.utility.mode -> db_sync_tool.remote.system).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.database.utility -> db_sync_tool.utility.mode -> db_sync_tool.remote.system -> db_sync_tool.remote.client -> db_sync_tool.remote.utility).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.database.utility -> db_sync_tool.utility.mode -> db_sync_tool.utility.helper -> db_sync_tool.utility.output -> db_sync_tool.utility.log -> db_sync_tool.utility.system -> db_sync_tool.utility.parser -> db_sync_tool.remote.client -> db_sync_tool.remote.utility).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.remote.client -> db_sync_tool.remote.utility -> db_sync_tool.utility.output -> db_sync_tool.utility.log -> db_sync_tool.utility.system -> db_sync_tool.utility.parser).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.utility.helper -> db_sync_tool.utility.mode).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (db_sync_tool.database.utility -> db_sync_tool.utility.mode -> db_sync_tool.utility.helper -> db_sync_tool.remote.utility).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

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 as database_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
        database_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) + database_utility.database_dump_file_name
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (108/100).

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

Loading history...
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
            database_utility. generate_mysql_credentials('origin') + ' ' +
28
            system.config['origin']['db']['name'] + ' ' +
29
            database_utility.generate_ignore_database_tables() +
30
            ' > ' + _dump_file_path,
31
            mode.Client.ORIGIN
32
        )
33
34
        database_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 system.config['clear_database']:
47
        output.message(
48
            output.Subject.TARGET,
49
            'Clearing database before import',
50
            True
51
        )
52
        clear_database(mode.Client.TARGET)
53
54
    if not system.config['keep_dump'] and not system.config['is_same_client']:
55
        output.message(
56
            output.Subject.TARGET,
57
            'Importing database dump',
58
            True
59
        )
60
61
        if not mode.is_import():
62
           _dump_path = helper.get_dump_dir(mode.Client.TARGET) + database_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...
Coding Style introduced by
This line is too long as per the coding-style (106/100).

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

Loading history...
63
        else:
64
           _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...
65
66
        if not system.config['yes']:
67
            _host_name = helper.get_ssh_host_name(mode.Client.TARGET, True) if mode.is_remote(mode.Client.TARGET) else 'local'
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (126/100).

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

Loading history...
68
69
            helper.confirm(
70
                output.message(
71
                    output.Subject.TARGET,
72
                    f'Are you sure, you want to import the dump file into {_host_name} database?',
73
                    False
74
                ),
75
                True
76
            )
77
78
        database_utility.check_database_dump(mode.Client.TARGET, _dump_path)
79
80
        import_database_dump_file(mode.Client.TARGET, _dump_path)
81
82
    if 'after_dump' in system.config['target']:
83
        _after_dump = system.config['target']['after_dump']
84
        output.message(
85
            output.Subject.TARGET,
86
            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...
87
            True
88
        )
89
90
        import_database_dump_file(mode.Client.TARGET, _after_dump)
91
92
93
def import_database_dump_file(client, filepath):
94
    """
95
    Import a database dump file
96
    :param client: String
97
    :param filepath: String
98
    :return:
99
    """
100
    if helper.check_file_exists(client, filepath):
101
        mode.run_command(
102
            helper.get_command(client, 'mysql') + ' ' +
103
            database_utility.generate_mysql_credentials(client) + ' ' +
104
            system.config[client]['db']['name'] + ' < ' + filepath,
105
            client
106
        )
107
108
109
def prepare_origin_database_dump():
110
    """
111
    Preparing the origin database dump file by compressing them as .tar.gz
112
    :return:
113
    """
114
    output.message(
115
        output.Subject.ORIGIN,
116
        'Compressing database dump',
117
        True
118
    )
119
    mode.run_command(
120
        helper.get_command(mode.Client.ORIGIN, 'tar') + ' cfvz ' + helper.get_dump_dir(
121
            mode.Client.ORIGIN) + database_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 (113/100).

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

Loading history...
122
            mode.Client.ORIGIN) + ' ' + database_utility.database_dump_file_name + ' > /dev/null',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 4 spaces).
Loading history...
123
        mode.Client.ORIGIN
124
    )
125
126
127
def prepare_target_database_dump():
128
    """
129
    Preparing the target database dump by the unpacked .tar.gz file
130
    :return:
131
    """
132
    output.message(output.Subject.TARGET, 'Extracting database dump', True)
133
    mode.run_command(
134
        helper.get_command('target', 'tar') + ' xzf ' + helper.get_dump_dir(
135
            mode.Client.TARGET) + database_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 (113/100).

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

Loading history...
136
            mode.Client.TARGET) + ' > /dev/null',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 4 spaces).
Loading history...
137
        mode.Client.TARGET
138
    )
139
140
141
def clear_database(client):
142
    """
0 ignored issues
show
Bug introduced by
A suspicious escape sequence \` was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
143
    Clearing the database by dropping all tables
144
    https://www.techawaken.com/drop-tables-mysql-database/
145
146
    { mysql -hHOSTNAME -uUSERNAME -pPASSWORD -Nse 'show tables' DB_NAME; } | ( while read table; do if [ -z ${i+x} ];
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (117/100).

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

Loading history...
147
    then echo 'SET FOREIGN_KEY_CHECKS = 0;'; fi; i=1; echo "drop table \`$table\`;"; done;
148
    echo 'SET FOREIGN_KEY_CHECKS = 1;' ) | awk '{print}' ORS=' ' | mysql -hHOSTNAME -uUSERNAME -pPASSWORD DB_NAME;
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (114/100).

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

Loading history...
149
150
    :param client: String
151
    :return:
152
    """
153
    mode.run_command(
154
        '{ ' + helper.get_command(client, 'mysql') + ' ' +
155
        database_utility.generate_mysql_credentials(client) +
156
        ' -Nse \'show tables\' ' +
157
        system.config[client]['db']['name'] + '; }' +
158
        ' | ( while read table; do if [ -z ${i+x} ]; then echo \'SET FOREIGN_KEY_CHECKS = 0;\'; fi; i=1; ' +
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (108/100).

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

Loading history...
159
        'echo "drop table \\`$table\\`;"; done; echo \'SET FOREIGN_KEY_CHECKS = 1;\' ) | awk \'{print}\' ORS=' ' | ' +
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (118/100).

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

Loading history...
160
        helper.get_command(client, 'mysql') + ' ' +
161
        database_utility.generate_mysql_credentials(client) + ' ' +
162
        system.config[client]['db']['name'],
163
        client
164
    )
165