Passed
Push — master ( 8d66d0...ffbe9f )
by Konrad
02:02
created

get_database_tables_like()   A

Complexity

Conditions 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
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: utf-8 -*-
3
4
import sys
5
import datetime
6
from db_sync_tool.utility import parser, mode, system, helper, output
7
8
#
9
# GLOBALS
10
#
11
12
origin_database_dump_file_name = None
0 ignored issues
show
Coding Style Naming introduced by
Constant name "origin_database_dump_file_name" doesn't conform to UPPER_CASE naming style ('([^\\W\\da-z][^\\Wa-z]*|__.*__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
13
14
#
15
# FUNCTIONS
16
#
17
18
def create_origin_database_dump():
19
    """
20
    Creating the origin database dump file
21
    :return:
22
    """
23
24
    if not mode.is_import():
25
        parser.get_database_configuration(mode.Client.ORIGIN)
26
        generate_database_dump_filename()
27
        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...
28
29
        _dump_file_path = helper.get_dump_dir(mode.Client.ORIGIN) + origin_database_dump_file_name
30
31
        output.message(
32
            output.Subject.ORIGIN,
33
            'Creating database dump',
34
            True
35
        )
36
        mode.run_command(
37
            helper.get_command('origin', 'mysqldump') + ' --no-tablespaces ' + generate_mysql_credentials('origin') + ' ' +
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (123/100).

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

Loading history...
38
            system.config['db']['origin'][
39
                'dbname'] + ' ' + generate_ignore_database_tables() + ' > ' + _dump_file_path,
40
            mode.Client.ORIGIN
41
        )
42
43
        check_database_dump(mode.Client.ORIGIN, _dump_file_path)
44
        prepare_origin_database_dump()
45
46
47
def run_database_command(client, command):
48
    """
49
    Run a database command using the "mysql -e" command
50
    :param command: String database command
51
    :return:
52
    """
53
    return mode.run_command(helper.get_command(client, 'mysql') + ' ' + generate_mysql_credentials(client) + ' -e "' + command + '"', client, True)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (147/100).

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

Loading history...
54
55
56
def generate_database_dump_filename():
57
    """
58
    Generate a database dump filename like "_[dbname]_[date].sql" or using the give filename
59
    :return:
60
    """
61
    global origin_database_dump_file_name
0 ignored issues
show
Coding Style introduced by
Usage of the global statement should be avoided.

Usage of global can make code hard to read and test, its usage is generally not recommended unless you are dealing with legacy code.

Loading history...
Coding Style Naming introduced by
Constant name "origin_database_dump_file_name" doesn't conform to UPPER_CASE naming style ('([^\\W\\da-z][^\\Wa-z]*|__.*__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
62
63
    if system.option['dump_name'] == '':
64
        # _project-db_20-08-2020_12-37.sql
65
        _now = datetime.datetime.now()
66
        origin_database_dump_file_name = '_' + system.config['db']['origin']['dbname'] + '_' + _now.strftime("%d-%m-%Y_%H-%M") + '.sql'
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (135/100).

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

Loading history...
67
    else:
68
        origin_database_dump_file_name = system.option['dump_name'] + '.sql'
69
70
71
def generate_ignore_database_tables():
0 ignored issues
show
Unused Code introduced by
Either all return statements in a function should return an expression, or none of them should.
Loading history...
72
    """
73
    Generate the ignore tables options for the mysqldump command by the given table list
74
    :return: String
75
    """
76
    _ignore_tables = []
77
    if 'ignore_table' in system.config['host']:
78
        for table in system.config['host']['ignore_table']:
79
            if '*' in table:
80
                _wildcard_tables = get_database_tables_like('origin', table.replace('*', ''))
81
                if _wildcard_tables:
82
                    for wildcard_table in _wildcard_tables:
83
                        _ignore_tables = generate_ignore_database_table(_ignore_tables, wildcard_table)
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...
84
            else:
85
                _ignore_tables = generate_ignore_database_table(_ignore_tables, table)
86
        return ' '.join(_ignore_tables)
87
88
89
def generate_ignore_database_table(ignore_tables, table):
90
    """
91
    :param ignore_tables: Dictionary
92
    :param table: String
93
    :return:
94
    """
95
    ignore_tables.append('--ignore-table=' + system.config['db']['origin']['dbname'] + '.' + table)
96
    return ignore_tables
97
98
99
def get_database_tables_like(client, name):
0 ignored issues
show
Unused Code introduced by
Either all return statements in a function should return an expression, or none of them should.
Loading history...
100
    """
101
    Get database table names like the given name
102
    :param client: String
103
    :param name: String
104
    :return: Dictionary
105
    """
106
    _dbname = system.config['db'][client]['dbname']
107
    _tables = run_database_command(client, f'SHOW TABLES FROM {_dbname} LIKE \'%{name}%\';').strip()
108
    if _tables != '':
109
        return _tables.split('\n', 1)[1:]
110
111
112
def generate_mysql_credentials(client):
113
    """
114
    Generate the needed database credential information for the mysql command
115
    :param client: String
116
    :return:
117
    """
118
    _credentials = '-u\'' + system.config['db'][client]['user'] + '\' -p\'' + system.config['db'][client][
0 ignored issues
show
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...
119
        'password'] + '\''
120
    if 'host' in system.config['db'][client]:
121
        _credentials += ' -h\'' + system.config['db'][client]['host'] + '\''
122
    if 'port' in system.config['db'][client]:
123
        _credentials += ' -P\'' + str(system.config['db'][client]['port']) + '\''
124
    return _credentials
125
126
127
#
128
# IMPORT DATABASE DUMP
129
#
130
def import_database_dump():
131
    """
132
    Importing the selected database dump file
133
    :return:
134
    """
135
    if (not system.option['is_same_client'] and not mode.is_import()):
136
        prepare_target_database_dump()
137
138
    if not system.option['keep_dump'] and not system.option['is_same_client']:
139
        output.message(
140
            output.Subject.TARGET,
141
            'Importing database dump',
142
            True
143
        )
144
145
        if not mode.is_import():
146
           _dump_path = helper.get_dump_dir(mode.Client.TARGET) + origin_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...
147
        else:
148
           _dump_path = system.option['import']
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 11 were found.
Loading history...
149
150
        check_database_dump(mode.Client.TARGET, _dump_path)
151
        mode.run_command(
152
            helper.get_command('target', 'mysql') + ' ' + generate_mysql_credentials('target') + ' ' +
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...
153
            system.config['db']['target'][
154
                'dbname'] + ' < ' + _dump_path,
155
            mode.Client.TARGET
156
        )
157
158
159
def prepare_origin_database_dump():
160
    """
161
    Preparing the origin database dump file by compressing them as .tar.gz
162
    :return:
163
    """
164
    output.message(
165
        output.Subject.ORIGIN,
166
        'Compressing database dump',
167
        True
168
    )
169
    mode.run_command(
170
        helper.get_command(mode.Client.ORIGIN, 'tar') + ' cfvz ' + helper.get_dump_dir(
171
            mode.Client.ORIGIN) + origin_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 (103/100).

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

Loading history...
172
            mode.Client.ORIGIN) + ' ' + origin_database_dump_file_name + ' > /dev/null',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 4 spaces).
Loading history...
173
        mode.Client.ORIGIN
174
    )
175
176
177
def prepare_target_database_dump():
178
    """
179
    Preparing the target database dump by the unpacked .tar.gz file
180
    :return:
181
    """
182
    output.message(output.Subject.TARGET, 'Extracting database dump', True)
183
    mode.run_command(
184
        helper.get_command('target', 'tar') + ' xzf ' + helper.get_dump_dir(
185
            mode.Client.TARGET) + origin_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 (103/100).

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

Loading history...
186
            mode.Client.TARGET) + ' > /dev/null',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 4 spaces).
Loading history...
187
        mode.Client.TARGET
188
    )
189
190
191
def check_database_dump(client, filepath):
192
    """
193
    Checking the last line of the dump file if it contains "-- Dump completed on"
194
    :param client: String
195
    :param filepath: String
196
    :return:
197
    """
198
199
    if system.option['check_dump']:
200
        _line = mode.run_command(
201
            helper.get_command(client, 'tail') + ' -n 1 ' + filepath,
202
                client,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (remove 4 spaces).
Loading history...
203
                True
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (remove 4 spaces).
Loading history...
204
            )
205
206
        if "-- Dump completed on" not in _line:
207
            sys.exit(output.message(output.Subject.ERROR, 'Dump file is corrupted', False))
208