|
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): |
|
|
|
|
|
|
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
|
|
|
|
It is generally discouraged to redefine built-ins as this makes code very hard to read.