Test Failed
Push — develop ( 720679...ba0508 )
by Dean
02:45
created

SchemaMigration._build_router()   A

Complexity

Conditions 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
crap 6
1 1
from plugin.core.database.manager import DatabaseManager
2 1
from plugin.models.core import db, migrations_path
3 1
from plugin.modules.migrations.core.base import Migration
4
5 1
import logging
6 1
7
log = logging.getLogger(__name__)
8 1
9
# Try import "peewee_migrate" router
10
try:
11 1
    from peewee_migrate.core import Router
12 1
except (ImportError, NameError):
13 1
    Router = None
14
15 1
16
class SchemaMigration(Migration):
17
    def run(self):
18 1
        log.debug('migrations_path: %r', migrations_path)
19 1
20 1
        # Build migration router
21
        router = self._build_router()
22 1
23
        if not router:
24
            return False
25 1
26 1
        # Validate current schema
27
        if not router.validate():
28 1
            log.error('Detected corrupt/invalid database schema, resetting database...')
29
            return self.reset()
30 1
        else:
31
            log.info('Database schema is valid')
32
33
        # Run schema migrations
34
        router.run()
35 1
        return True
36 1
37
    @classmethod
38
    def reset(cls):
39 1
        if not DatabaseManager.reset('main', db, 'invalid-schema'):
40 1
            # Unable to reset database
41
            return False
42 1
43
        # Build migration router
44
        router = cls._build_router()
45
46
        if not router:
47 1
            return False
48
49 1
        # Run migrations
50
        router.run()
51 1
52
        # Log message to channel menu
53
        from plugin.managers.message import MessageManager
54
        from plugin.models import Message
55
56
        MessageManager.get.from_message(logging.WARNING,
0 ignored issues
show
Bug introduced by
It seems like a value for argument message is missing in the unbound method call.
Loading history...
57
            "Plugin database has been automatically reset due to schema corruption, see http://bit.ly/TFPx90101 for more details",
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (130/120).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
58
            code=Message.Code.DatabaseSchemaCorruptionReset
59
        )
60
61
        return True
62
63
    @staticmethod
64
    def _build_router():
65
        if not Router:
66
            return None
67
68
        return Router(migrations_path, DATABASE=db)
69