|
@@ -11,6 +11,7 @@ from pyramid.paster import (
|
11
|
11
|
from tracim import CFG
|
12
|
12
|
from tracim.fixtures import FixturesLoader
|
13
|
13
|
from tracim.fixtures.users_and_groups import Base as BaseFixture
|
|
14
|
+from tracim.fixtures.content import Content as ContentFixture
|
14
|
15
|
from sqlalchemy.exc import IntegrityError
|
15
|
16
|
from tracim.command import AppContextCommand
|
16
|
17
|
from tracim.models.meta import DeclarativeBase
|
|
@@ -25,10 +26,18 @@ class InitializeDBCommand(AppContextCommand):
|
25
|
26
|
auto_setup_context = False
|
26
|
27
|
|
27
|
28
|
def get_description(self) -> str:
|
28
|
|
- return "Initialize DB"
|
|
29
|
+ return "Initialize database"
|
29
|
30
|
|
30
|
31
|
def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
|
31
|
32
|
parser = super().get_parser(prog_name)
|
|
33
|
+ parser.add_argument(
|
|
34
|
+ "--test-data",
|
|
35
|
+ help='Add some default data to database to make test',
|
|
36
|
+ dest='test_data',
|
|
37
|
+ required=False,
|
|
38
|
+ action='store_true',
|
|
39
|
+ default=False,
|
|
40
|
+ )
|
32
|
41
|
return parser
|
33
|
42
|
|
34
|
43
|
def take_action(self, parsed_args: argparse.Namespace) -> None:
|
|
@@ -37,7 +46,7 @@ class InitializeDBCommand(AppContextCommand):
|
37
|
46
|
setup_logging(config_uri)
|
38
|
47
|
settings = get_appsettings(config_uri)
|
39
|
48
|
self._create_schema(settings)
|
40
|
|
- self._populate_database(settings)
|
|
49
|
+ self._populate_database(settings, add_test_data=parsed_args.test_data)
|
41
|
50
|
|
42
|
51
|
@classmethod
|
43
|
52
|
def _create_schema(
|
|
@@ -51,7 +60,8 @@ class InitializeDBCommand(AppContextCommand):
|
51
|
60
|
@classmethod
|
52
|
61
|
def _populate_database(
|
53
|
62
|
cls,
|
54
|
|
- settings: plaster_pastedeploy.ConfigDict
|
|
63
|
+ settings: plaster_pastedeploy.ConfigDict,
|
|
64
|
+ add_test_data: bool
|
55
|
65
|
) -> None:
|
56
|
66
|
engine = get_engine(settings)
|
57
|
67
|
session_factory = get_session_factory(engine)
|
|
@@ -60,8 +70,14 @@ class InitializeDBCommand(AppContextCommand):
|
60
|
70
|
with transaction.manager:
|
61
|
71
|
dbsession = get_tm_session(session_factory, transaction.manager)
|
62
|
72
|
try:
|
|
73
|
+ fixtures = [BaseFixture]
|
63
|
74
|
fixtures_loader = FixturesLoader(dbsession, app_config)
|
64
|
|
- fixtures_loader.loads([BaseFixture])
|
|
75
|
+ fixtures_loader.loads(fixtures)
|
|
76
|
+ transaction.commit()
|
|
77
|
+ if add_test_data:
|
|
78
|
+ app_config.configure_filedepot()
|
|
79
|
+ fixtures = [ContentFixture]
|
|
80
|
+ fixtures_loader.loads(fixtures)
|
65
|
81
|
transaction.commit()
|
66
|
82
|
print("Database initialized.")
|
67
|
83
|
except IntegrityError:
|
|
@@ -71,3 +87,36 @@ class InitializeDBCommand(AppContextCommand):
|
71
|
87
|
print(traceback.format_exc())
|
72
|
88
|
transaction.abort()
|
73
|
89
|
print('Database initialization failed')
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+class DeleteDBCommand(AppContextCommand):
|
|
93
|
+ auto_setup_context = False
|
|
94
|
+
|
|
95
|
+ def get_description(self) -> str:
|
|
96
|
+ return "Delete database"
|
|
97
|
+
|
|
98
|
+ def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
|
|
99
|
+ parser = super().get_parser(prog_name)
|
|
100
|
+ parser.add_argument(
|
|
101
|
+ "--force",
|
|
102
|
+ help='force delete of database',
|
|
103
|
+ dest='force',
|
|
104
|
+ required=False,
|
|
105
|
+ action='store_true',
|
|
106
|
+ default=False,
|
|
107
|
+ )
|
|
108
|
+ return parser
|
|
109
|
+
|
|
110
|
+ def take_action(self, parsed_args: argparse.Namespace) -> None:
|
|
111
|
+ super(DeleteDBCommand, self).take_action(parsed_args)
|
|
112
|
+ config_uri = parsed_args.config_file
|
|
113
|
+ setup_logging(config_uri)
|
|
114
|
+ settings = get_appsettings(config_uri)
|
|
115
|
+ engine = get_engine(settings)
|
|
116
|
+ if parsed_args.force:
|
|
117
|
+ print('Database deletion begin')
|
|
118
|
+ DeclarativeBase.metadata.drop_all(engine)
|
|
119
|
+ print('Database deletion done')
|
|
120
|
+ else:
|
|
121
|
+ print('Warning, You should use --force if you really want to'
|
|
122
|
+ ' delete database.')
|