Browse Source

pre-populate_database with tracimcli db init

Guénaël Muller 7 years ago
parent
commit
fefdd8fd80
1 changed files with 29 additions and 14 deletions
  1. 29 14
      tracim/command/initializedb.py

+ 29 - 14
tracim/command/initializedb.py View File

1
 # -*- coding: utf-8 -*-
1
 # -*- coding: utf-8 -*-
2
-import os
3
-import sys
4
 import transaction
2
 import transaction
5
-
6
 from pyramid.paster import (
3
 from pyramid.paster import (
7
     get_appsettings,
4
     get_appsettings,
8
     setup_logging,
5
     setup_logging,
9
     )
6
     )
10
-from pyramid.scripts.common import parse_vars
11
 
7
 
8
+from tracim import CFG
9
+from tracim.fixtures import FixturesLoader
10
+from tracim.fixtures.users_and_groups import Base as BaseFixture
11
+from sqlalchemy.exc import IntegrityError
12
 from tracim.command import AppContextCommand
12
 from tracim.command import AppContextCommand
13
 from tracim.models.meta import DeclarativeBase
13
 from tracim.models.meta import DeclarativeBase
14
 from tracim.models import (
14
 from tracim.models import (
24
     def get_description(self):
24
     def get_description(self):
25
         return "Initialize DB"
25
         return "Initialize DB"
26
 
26
 
27
-    def get_epilog(self):
28
-        return "################"
29
-
30
     def get_parser(self, prog_name):
27
     def get_parser(self, prog_name):
31
         parser = super().get_parser(prog_name)
28
         parser = super().get_parser(prog_name)
32
         return parser
29
         return parser
34
     def take_action(self, parsed_args):
31
     def take_action(self, parsed_args):
35
         super(InitializeDBCommand, self).take_action(parsed_args)
32
         super(InitializeDBCommand, self).take_action(parsed_args)
36
         config_uri = parsed_args.config_file
33
         config_uri = parsed_args.config_file
37
-
38
         setup_logging(config_uri)
34
         setup_logging(config_uri)
39
         settings = get_appsettings(config_uri)
35
         settings = get_appsettings(config_uri)
36
+        self._create_schema(settings)
37
+        self._populate_database(settings)
38
+
39
+    @classmethod
40
+    def _create_schema(cls, settings):
41
+        print("- Create Schemas of databases -")
40
         engine = get_engine(settings)
42
         engine = get_engine(settings)
41
         DeclarativeBase.metadata.create_all(engine)
43
         DeclarativeBase.metadata.create_all(engine)
42
-        session_factory = get_session_factory(engine)
43
 
44
 
45
+    @classmethod
46
+    def _populate_database(cls, settings):
47
+        engine = get_engine(settings)
48
+        session_factory = get_session_factory(engine)
49
+        app_config = CFG(settings)
50
+        print("- Populate database with default data -")
44
         with transaction.manager:
51
         with transaction.manager:
45
-            pass
46
-            # dbsession = get_tm_session(session_factory, transaction.manager)
47
-            # model = MyModel(name='one', value=1)
48
-            # dbsession.add(model)
49
-            # Add global manager data, just for test
52
+            dbsession = get_tm_session(session_factory, transaction.manager)
53
+            try:
54
+                fixtures_loader = FixturesLoader(dbsession, app_config)
55
+                fixtures_loader.loads([BaseFixture])
56
+                transaction.commit()
57
+                print("Database initialized.")
58
+            except IntegrityError:
59
+                print('Warning, there was a problem when adding default data'
60
+                      ', it may have already been added:')
61
+                import traceback
62
+                print(traceback.format_exc())
63
+                transaction.abort()
64
+                print('Database initialization failed')