|
@@ -0,0 +1,132 @@
|
|
1
|
+# -*- coding: utf-8 -*-
|
|
2
|
+import argparse
|
|
3
|
+
|
|
4
|
+import plaster_pastedeploy
|
|
5
|
+import transaction
|
|
6
|
+from depot.manager import DepotManager
|
|
7
|
+from pyramid.paster import (
|
|
8
|
+ get_appsettings,
|
|
9
|
+ setup_logging,
|
|
10
|
+ )
|
|
11
|
+
|
|
12
|
+from tracim import CFG
|
|
13
|
+from tracim.fixtures import FixturesLoader
|
|
14
|
+from tracim.fixtures.users_and_groups import Base as BaseFixture
|
|
15
|
+from tracim.fixtures.content import Content as ContentFixture
|
|
16
|
+from sqlalchemy.exc import IntegrityError
|
|
17
|
+from tracim.command import AppContextCommand
|
|
18
|
+from tracim.models.meta import DeclarativeBase
|
|
19
|
+from tracim.models import (
|
|
20
|
+ get_engine,
|
|
21
|
+ get_session_factory,
|
|
22
|
+ get_tm_session,
|
|
23
|
+ )
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+class InitializeDBCommand(AppContextCommand):
|
|
27
|
+ auto_setup_context = False
|
|
28
|
+
|
|
29
|
+ def get_description(self) -> str:
|
|
30
|
+ return "Initialize database"
|
|
31
|
+
|
|
32
|
+ def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
|
|
33
|
+ parser = super().get_parser(prog_name)
|
|
34
|
+ parser.add_argument(
|
|
35
|
+ "--test-data",
|
|
36
|
+ help='Add some default data to database to make test',
|
|
37
|
+ dest='test_data',
|
|
38
|
+ required=False,
|
|
39
|
+ action='store_true',
|
|
40
|
+ default=False,
|
|
41
|
+ )
|
|
42
|
+ return parser
|
|
43
|
+
|
|
44
|
+ def take_action(self, parsed_args: argparse.Namespace) -> None:
|
|
45
|
+ super(InitializeDBCommand, self).take_action(parsed_args)
|
|
46
|
+ config_uri = parsed_args.config_file
|
|
47
|
+ setup_logging(config_uri)
|
|
48
|
+ settings = get_appsettings(config_uri)
|
|
49
|
+ self._create_schema(settings)
|
|
50
|
+ self._populate_database(settings, add_test_data=parsed_args.test_data)
|
|
51
|
+
|
|
52
|
+ @classmethod
|
|
53
|
+ def _create_schema(
|
|
54
|
+ cls,
|
|
55
|
+ settings: plaster_pastedeploy.ConfigDict
|
|
56
|
+ ) -> None:
|
|
57
|
+ print("- Create Schemas of databases -")
|
|
58
|
+ engine = get_engine(settings)
|
|
59
|
+ DeclarativeBase.metadata.create_all(engine)
|
|
60
|
+
|
|
61
|
+ @classmethod
|
|
62
|
+ def _populate_database(
|
|
63
|
+ cls,
|
|
64
|
+ settings: plaster_pastedeploy.ConfigDict,
|
|
65
|
+ add_test_data: bool
|
|
66
|
+ ) -> None:
|
|
67
|
+ engine = get_engine(settings)
|
|
68
|
+ session_factory = get_session_factory(engine)
|
|
69
|
+ app_config = CFG(settings)
|
|
70
|
+ print("- Populate database with default data -")
|
|
71
|
+ with transaction.manager:
|
|
72
|
+ dbsession = get_tm_session(session_factory, transaction.manager)
|
|
73
|
+ try:
|
|
74
|
+ fixtures = [BaseFixture]
|
|
75
|
+ fixtures_loader = FixturesLoader(dbsession, app_config)
|
|
76
|
+ fixtures_loader.loads(fixtures)
|
|
77
|
+ transaction.commit()
|
|
78
|
+ if add_test_data:
|
|
79
|
+ app_config.configure_filedepot()
|
|
80
|
+ fixtures = [ContentFixture]
|
|
81
|
+ fixtures_loader.loads(fixtures)
|
|
82
|
+ transaction.commit()
|
|
83
|
+ print("Database initialized.")
|
|
84
|
+ except IntegrityError:
|
|
85
|
+ print('Warning, there was a problem when adding default data'
|
|
86
|
+ ', it may have already been added:')
|
|
87
|
+ import traceback
|
|
88
|
+ print(traceback.format_exc())
|
|
89
|
+ transaction.abort()
|
|
90
|
+ print('Database initialization failed')
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+class DeleteDBCommand(AppContextCommand):
|
|
94
|
+ auto_setup_context = False
|
|
95
|
+
|
|
96
|
+ def get_description(self) -> str:
|
|
97
|
+ return "Delete database"
|
|
98
|
+
|
|
99
|
+ def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
|
|
100
|
+ parser = super().get_parser(prog_name)
|
|
101
|
+ parser.add_argument(
|
|
102
|
+ "--force",
|
|
103
|
+ help='force delete of database',
|
|
104
|
+ dest='force',
|
|
105
|
+ required=False,
|
|
106
|
+ action='store_true',
|
|
107
|
+ default=False,
|
|
108
|
+ )
|
|
109
|
+ return parser
|
|
110
|
+
|
|
111
|
+ def take_action(self, parsed_args: argparse.Namespace) -> None:
|
|
112
|
+ super(DeleteDBCommand, self).take_action(parsed_args)
|
|
113
|
+ config_uri = parsed_args.config_file
|
|
114
|
+ setup_logging(config_uri)
|
|
115
|
+ settings = get_appsettings(config_uri)
|
|
116
|
+ engine = get_engine(settings)
|
|
117
|
+ app_config = CFG(settings)
|
|
118
|
+ app_config.configure_filedepot()
|
|
119
|
+
|
|
120
|
+ if parsed_args.force:
|
|
121
|
+ print('Database deletion begin.')
|
|
122
|
+ DeclarativeBase.metadata.drop_all(engine)
|
|
123
|
+ print('Database deletion done.')
|
|
124
|
+ print('Cleaning depot begin.')
|
|
125
|
+ depot = DepotManager.get()
|
|
126
|
+ depot_files = depot.list()
|
|
127
|
+ for file_ in depot_files:
|
|
128
|
+ depot.delete(file_)
|
|
129
|
+ print('Cleaning depot done.')
|
|
130
|
+ else:
|
|
131
|
+ print('Warning, You should use --force if you really want to'
|
|
132
|
+ ' delete database.')
|