Test Failed
Push — develop ( a80574...5ed66c )
by Dean
02:36
created

DatabaseManager.reset()   B

Complexity

Conditions 3

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 8.8571
cc 3
1
from plugin.core.backup import BackupManager
2
from plugin.core.database.connection import db_connect, db_connection
3
from plugin.core.environment import Environment
0 ignored issues
show
Bug introduced by
The name environment does not seem to exist in module plugin.core.
Loading history...
Configuration introduced by
Unable to import 'plugin.core.environment' (invalid syntax (<string>, line 105))

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
4
5
from threading import RLock
6
import logging
7
import os
8
9
log = logging.getLogger(__name__)
10
11
12
class DatabaseManager(object):
13
    _cache = {
14
        'peewee': {},
15
        'raw': {}
16
    }
17
    _lock = RLock()
18
19
    @classmethod
20
    def main(cls):
21
        return cls._get(Environment.path.plugin_database, 'peewee', name='plugin database')
22
23
    @classmethod
24
    def cache(cls, name):
25
        return cls._get(os.path.join(Environment.path.plugin_caches, '%s.db' % name), 'raw', name='%s cache' % name)
26
27
    @classmethod
28
    def reset(cls, group, database, tag=None):
29
        # Backup database
30
        if not BackupManager.database.backup(group, database, tag):
31
            return False
32
33
        log.info('[%s] Resetting database objects...', group)
34
35
        # Get `database` connection
36
        conn = db_connection(database)
37
38
        # Drop all objects (index, table, trigger)
39
        conn.cursor().execute(
40
            "PRAGMA writable_schema = 1; "
41
            "DELETE FROM sqlite_master WHERE type IN ('table', 'index', 'trigger'); "
42
            "PRAGMA writable_schema = 0;"
43
        )
44
45
        # Recover space
46
        conn.cursor().execute('VACUUM;')
47
48
        # Check database integrity
49
        integrity, = conn.cursor().execute('PRAGMA INTEGRITY_CHECK;').fetchall()[0]
50
51
        if integrity != 'ok':
52
            log.error('[%s] Database integrity check error: %r', group, integrity)
53
            return False
54
55
        log.info('[%s] Database reset', group)
56
        return True
57
58
    @classmethod
59
    def _get(cls, path, type, **kwargs):
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in type.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
60
        path = os.path.abspath(path)
61
        cache = cls._cache[type]
62
63
        with cls._lock:
64
            if path not in cache:
65
                cache[path] = db_connect(path, type, **kwargs)
66
67
            # Return cached connection
68
            return cache[path]
69