database.py 4.7KB

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