| Total Complexity | 5 |
| Total Lines | 41 |
| Duplicated Lines | 0 % |
| Coverage | 0% |
| 1 | from plugin.core.database import Database |
||
| 11 | class SchemaMigration(Migration): |
||
| 12 | def run(self): |
||
| 13 | log.debug('migrations_path: %r', migrations_path) |
||
| 14 | |||
| 15 | router = self._build_router() |
||
| 16 | |||
| 17 | # Validate current schema |
||
| 18 | if not router.validate(): |
||
| 19 | log.error('Detected corrupt/invalid database schema, resetting database...') |
||
| 20 | return self.reset() |
||
| 21 | else: |
||
| 22 | log.info('Database schema is valid') |
||
| 23 | |||
| 24 | # Run schema migrations |
||
| 25 | router.run() |
||
| 26 | return True |
||
| 27 | |||
| 28 | @classmethod |
||
| 29 | def reset(cls): |
||
| 30 | if not Database.reset('main', db, 'invalid-schema'): |
||
| 31 | # Unable to reset database |
||
| 32 | return False |
||
| 33 | |||
| 34 | # Run migrations |
||
| 35 | router = cls._build_router() |
||
| 36 | router.run() |
||
| 37 | |||
| 38 | # Log message to channel menu |
||
| 39 | from plugin.managers import MessageManager |
||
| 40 | from plugin.models import Message |
||
| 41 | |||
| 42 | MessageManager.get.from_message(logging.WARNING, |
||
|
|
|||
| 43 | "Plugin database has been automatically reset due to schema corruption, see http://bit.ly/TFPx90101 for more details", |
||
| 44 | code=Message.Code.DatabaseSchemaCorruptionReset |
||
| 45 | ) |
||
| 46 | |||
| 47 | return True |
||
| 48 | |||
| 49 | @staticmethod |
||
| 50 | def _build_router(): |
||
| 51 | return Router(migrations_path, DATABASE=db) |
||
| 52 |