Passed
Push — master ( ffbe9f...5c3c8b )
by Konrad
01:19
created

generate_database_dump_filename()   A

Complexity

Conditions 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 14
rs 10
c 0
b 0
f 0
cc 2
nop 0
1
#!/usr/bin/env python3
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
# -*- coding: utf-8 -*-
3
4
5
import sys
6
import datetime
7
from db_sync_tool.utility import mode, system, helper, output
8
9
10
database_dump_file_name = None
0 ignored issues
show
Coding Style Naming introduced by
Constant name "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...
11
12
13
def run_database_command(client, command):
14
    """
15
    Run a database command using the "mysql -e" command
16
    :param client: String
17
    :param command: String database command
18
    :return:
19
    """
20
    return mode.run_command(
21
        helper.get_command(client, 'mysql') + ' ' + generate_mysql_credentials(client) + ' -e "' + command + '"',
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...
22
        client, True)
23
24
25
def generate_database_dump_filename():
26
    """
27
    Generate a database dump filename like "_[name]_[date].sql" or using the give filename
28
    :return:
29
    """
30
    global 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 "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...
31
32
    if system.option['dump_name'] == '':
33
        # _project-db_20-08-2020_12-37.sql
34
        _now = datetime.datetime.now()
35
        database_dump_file_name = '_' + system.config['origin']['db']['name'] + '_' + _now.strftime(
36
            "%d-%m-%Y_%H-%M") + '.sql'
37
    else:
38
        database_dump_file_name = system.option['dump_name'] + '.sql'
39
40
41
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...
42
    """
43
    Generate the ignore tables options for the mysqldump command by the given table list
44
    # ToDo: Too much conditional nesting
45
    :return: String
46
    """
47
    _ignore_tables = []
48
    if 'ignore_table' in system.config:
49
        for table in system.config['ignore_table']:
50
            if '*' in table:
51
                _wildcard_tables = get_database_tables_like('origin', table.replace('*', ''))
52
                if _wildcard_tables:
53
                    for wildcard_table in _wildcard_tables:
54
                        _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...
55
            else:
56
                _ignore_tables = generate_ignore_database_table(_ignore_tables, table)
57
        return ' '.join(_ignore_tables)
58
59
60
def generate_ignore_database_table(ignore_tables, table):
61
    """
62
    :param ignore_tables: Dictionary
63
    :param table: String
64
    :return:
65
    """
66
    ignore_tables.append('--ignore-table=' + system.config['origin']['db']['name'] + '.' + table)
67
    return ignore_tables
68
69
70
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...
71
    """
72
    Get database table names like the given name
73
    :param client: String
74
    :param name: String
75
    :return: Dictionary
76
    """
77
    _dbname = system.config[client]['db']['name']
78
    _tables = run_database_command(client, f'SHOW TABLES FROM {_dbname} LIKE \'%{name}%\';').strip()
79
    if _tables != '':
80
        return _tables.split('\n', 1)[1:]
81
82
83
def generate_mysql_credentials(client):
84
    """
85
    Generate the needed database credential information for the mysql command
86
    :param client: String
87
    :return:
88
    """
89
    _credentials = '-u\'' + system.config[client]['db']['user'] + '\' -p\'' + system.config[client]['db'][
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...
90
        'password'] + '\''
91
    if 'host' in system.config[client]['db']:
92
        _credentials += ' -h\'' + system.config[client]['db']['host'] + '\''
93
    if 'port' in system.config[client]['db']:
94
        _credentials += ' -P\'' + str(system.config[client]['db']['port']) + '\''
95
    return _credentials
96
97
98
def check_database_dump(client, filepath):
99
    """
100
    Checking the last line of the dump file if it contains "-- Dump completed on"
101
    :param client: String
102
    :param filepath: String
103
    :return:
104
    """
105
    if system.option['check_dump']:
106
        _line = mode.run_command(
107
            helper.get_command(client, 'tail') + ' -n 1 ' + filepath,
108
            client,
109
            True
110
        )
111
112
        if "-- Dump completed on" not in _line:
113
            sys.exit(output.message(output.Subject.ERROR, 'Dump file is corrupted', False))
114