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 |
|
|
|
|
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): |
|
|
|
|
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
|
|
|
|