InitializeDBCommand._create_schema()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
nop 2
1
# -*- coding: utf-8 -*-
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
import argparse
3
4
import plaster_pastedeploy
5
import transaction
6
from pyramid.paster import (
7
    get_appsettings,
8
    setup_logging,
9
    )
10
11
from tracim import CFG
12
from tracim.fixtures import FixturesLoader
13
from tracim.fixtures.users_and_groups import Base as BaseFixture
14
from sqlalchemy.exc import IntegrityError
15
from tracim.command import AppContextCommand
0 ignored issues
show
introduced by
Imports from package tracim are not grouped
Loading history...
16
from tracim.models.meta import DeclarativeBase
17
from tracim.models import (
18
    get_engine,
19
    get_session_factory,
20
    get_tm_session,
21
    )
22
23
24
class InitializeDBCommand(AppContextCommand):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
25
    auto_setup_context = False
26
27
    def get_description(self) -> str:
28
        return "Initialize DB"
29
30
    def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
31
        parser = super().get_parser(prog_name)
32
        return parser
33
34
    def take_action(self, parsed_args: argparse.Namespace) -> None:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable argparse does not seem to be defined.
Loading history...
35
        super(InitializeDBCommand, self).take_action(parsed_args)
36
        config_uri = parsed_args.config_file
37
        setup_logging(config_uri)
38
        settings = get_appsettings(config_uri)
39
        self._create_schema(settings)
40
        self._populate_database(settings)
41
42
    @classmethod
43
    def _create_schema(
44
            cls,
45
            settings: plaster_pastedeploy.ConfigDict
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable plaster_pastedeploy does not seem to be defined.
Loading history...
46
    ) -> None:
47
        print("- Create Schemas of databases -")
48
        engine = get_engine(settings)
49
        DeclarativeBase.metadata.create_all(engine)
50
51
    @classmethod
52
    def _populate_database(
53
            cls,
54
            settings: plaster_pastedeploy.ConfigDict
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable plaster_pastedeploy does not seem to be defined.
Loading history...
55
    ) -> None:
56
        engine = get_engine(settings)
57
        session_factory = get_session_factory(engine)
58
        app_config = CFG(settings)
59
        print("- Populate database with default data -")
60
        with transaction.manager:
61
            dbsession = get_tm_session(session_factory, transaction.manager)
62
            try:
63
                fixtures_loader = FixturesLoader(dbsession, app_config)
64
                fixtures_loader.loads([BaseFixture])
65
                transaction.commit()
66
                print("Database initialized.")
67
            except IntegrityError:
68
                print('Warning, there was a problem when adding default data'
69
                      ', it may have already been added:')
70
                import traceback
71
                print(traceback.format_exc())
72
                transaction.abort()
73
                print('Database initialization failed')
74