Passed
Push — develop ( b3bfc7...6fb855 )
by Dean
02:36
created

SchemaMigration.run()   A

Complexity

Conditions 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2
Metric Value
cc 2
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 2
rs 9.4285
1 1
from plugin.core.database import Database
2 1
from plugin.models.core import db, migrations_path
3 1
from plugin.modules.migrations.core.base import Migration
4
5 1
from peewee_migrate.core import Router
6 1
import logging
7
8 1
log = logging.getLogger(__name__)
9
10
11 1
class SchemaMigration(Migration):
12 1
    def run(self):
13 1
        log.debug('migrations_path: %r', migrations_path)
14
15 1
        router = self._build_router()
16
17
        # Validate current schema
18 1
        if not router.validate():
19 1
            log.error('Detected corrupt/invalid database schema, resetting database...')
20 1
            return self.reset()
21
        else:
22 1
            log.info('Database schema is valid')
23
24
        # Run schema migrations
25 1
        router.run()
26 1
        return True
27
28 1
    @classmethod
29
    def reset(cls):
30 1
        if not Database.reset('main', db, 'invalid-schema'):
31
            # Unable to reset database
32
            return False
33
34
        # Run migrations
35 1
        router = cls._build_router()
36 1
        router.run()
37
38
        # Log message to channel menu
39 1
        from plugin.managers.message import MessageManager
40 1
        from plugin.models import Message
41
42 1
        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...
43
            "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...
44
            code=Message.Code.DatabaseSchemaCorruptionReset
45
        )
46
47 1
        return True
48
49 1
    @staticmethod
50
    def _build_router():
51
        return Router(migrations_path, DATABASE=db)
52