1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
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 |
|
|
|
|
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): |
|
|
|
|
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: |
|
|
|
|
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 |
|
|
|
|
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 |
|
|
|
|
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
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.