Passed
Push — develop ( 6fb855...7cfc9a )
by Dean
02:39
created

Database._backup_path()   A

Complexity

Conditions 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2
Metric Value
cc 2
dl 0
loc 20
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
1 1
from plugin.core.backup import BackupManager
2 1
from plugin.core.environment import Environment
3 1
from plugin.core.helpers.database import db_connect, db_connection
4
5 1
from threading import RLock
6 1
import logging
7 1
import os
8
9 1
log = logging.getLogger(__name__)
10
11
12 1
class Database(object):
13 1
    _cache = {
14
        'peewee': {},
15
        'raw': {}
16
    }
17 1
    _lock = RLock()
18
19 1
    @classmethod
20
    def main(cls):
21 1
        return cls._get(Environment.path.plugin_database, 'peewee')
22
23 1
    @classmethod
24
    def cache(cls, name):
25 1
        return cls._get(os.path.join(Environment.path.plugin_caches, '%s.db' % name), 'raw')
26
27 1
    @classmethod
28 1
    def reset(cls, group, database, tag=None):
29
        # Backup database
30 1
        if not BackupManager.database.backup(group, database, tag):
31
            return False
32
33 1
        log.info('[%s] Resetting database objects...', group)
34
35
        # Get `database` connection
36 1
        conn = db_connection(database)
37
38
        # Drop all objects (index, table, trigger)
39 1
        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 1
        conn.cursor().execute('VACUUM;')
47
48
        # Check database integrity
49 1
        integrity, = conn.cursor().execute('PRAGMA INTEGRITY_CHECK;').fetchall()[0]
50
51 1
        if integrity != 'ok':
52
            log.error('[%s] Database integrity check error: %r', group, integrity)
53
            return False
54
55 1
        log.info('[%s] Database reset', group)
56 1
        return True
57
58 1
    @classmethod
59
    def _get(cls, path, type):
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 1
        path = os.path.abspath(path)
61 1
        cache = cls._cache[type]
62
63 1
        with cls._lock:
64 1
            if path not in cache:
65 1
                cache[path] = db_connect(path, type)
66
67
            # Return cached connection
68
            return cache[path]
69