db.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  2. import argparse
  3. import io
  4. import urllib.parse
  5. import sys
  6. import zipfile
  7. from functools import partial
  8. from pathlib import Path
  9. import requests
  10. from . import Command
  11. from .server import report_configuration
  12. from ..service.db import dump_db, exp_drop, exp_db_exist, exp_duplicate_database, exp_rename, restore_db
  13. from ..tools import config
  14. eprint = partial(print, file=sys.stderr, flush=True)
  15. class Db(Command):
  16. """ Create, drop, dump, load databases """
  17. name = 'db'
  18. def run(self, cmdargs):
  19. """Command-line version of the database manager.
  20. Doesn't provide a `create` command as that's not useful. Commands are
  21. all filestore-aware.
  22. """
  23. parser = argparse.ArgumentParser(
  24. prog=f'{Path(sys.argv[0]).name} {self.name}',
  25. description=self.__doc__.strip()
  26. )
  27. parser.add_argument('-c', '--config')
  28. parser.add_argument('-D', '--data-dir')
  29. parser.add_argument('--addons-path')
  30. parser.add_argument('-r', '--db_user')
  31. parser.add_argument('-w', '--db_password')
  32. parser.add_argument('--pg_path')
  33. parser.add_argument('--db_host')
  34. parser.add_argument('--db_port')
  35. parser.add_argument('--db_sslmode')
  36. parser.set_defaults(func=lambda _: exit(parser.format_help()))
  37. subs = parser.add_subparsers()
  38. load = subs.add_parser(
  39. "load", help="Load a dump file.",
  40. description="Loads a dump file into odoo, dump file can be a URL. "
  41. "If `database` is provided, uses that as the database name. "
  42. "Otherwise uses the dump file name without extension.")
  43. load.set_defaults(func=self.load)
  44. load.add_argument(
  45. '-f', '--force', action='store_const', default=False, const=True,
  46. help="delete `database` database before loading if it exists"
  47. )
  48. load.add_argument(
  49. '-n', '--neutralize', action='store_const', default=False, const=True,
  50. help="neutralize the database after restore"
  51. )
  52. load.add_argument(
  53. 'database', nargs='?',
  54. help="database to create, defaults to dump file's name "
  55. "(without extension)"
  56. )
  57. load.add_argument('dump_file', help="zip or pg_dump file to load")
  58. dump = subs.add_parser(
  59. "dump", help="Create a dump with filestore.",
  60. description="Creates a dump file. The dump is always in zip format "
  61. "(with filestore), to get a no-filestore format use "
  62. "pg_dump directly.")
  63. dump.set_defaults(func=self.dump)
  64. dump.add_argument('database', help="database to dump")
  65. dump.add_argument(
  66. 'dump_path', nargs='?', default='-',
  67. help="if provided, database is dumped to specified path, otherwise "
  68. "or if `-`, dumped to stdout",
  69. )
  70. duplicate = subs.add_parser("duplicate", help="Duplicate a database including filestore.")
  71. duplicate.set_defaults(func=self.duplicate)
  72. duplicate.add_argument(
  73. '-f', '--force', action='store_const', default=False, const=True,
  74. help="delete `target` database before copying if it exists"
  75. )
  76. duplicate.add_argument(
  77. '-n', '--neutralize', action='store_const', default=False, const=True,
  78. help="neutralize the target database after duplicate"
  79. )
  80. duplicate.add_argument("source")
  81. duplicate.add_argument("target", help="database to copy `source` to, must not exist unless `-f` is specified in which case it will be dropped first")
  82. rename = subs.add_parser("rename", help="Rename a database including filestore.")
  83. rename.set_defaults(func=self.rename)
  84. rename.add_argument(
  85. '-f', '--force', action='store_const', default=False, const=True,
  86. help="delete `target` database before renaming if it exists"
  87. )
  88. rename.add_argument('source')
  89. rename.add_argument("target", help="database to rename `source` to, must not exist unless `-f` is specified, in which case it will be dropped first")
  90. drop = subs.add_parser("drop", help="Delete a database including filestore")
  91. drop.set_defaults(func=self.drop)
  92. drop.add_argument("database", help="database to delete")
  93. args = parser.parse_args(cmdargs)
  94. config.parse_config([
  95. val
  96. for k, v in vars(args).items()
  97. if v is not None
  98. if k in ['config', 'data_dir', 'addons_path'] or k.startswith(('db_', 'pg_'))
  99. for val in [
  100. '--data-dir' if k == 'data_dir'\
  101. else '--addons-path' if k == 'addons_path'\
  102. else f'--{k}',
  103. v,
  104. ]
  105. ])
  106. # force db management active to bypass check when only a
  107. # `check_db_management_enabled` version is available.
  108. config['list_db'] = True
  109. report_configuration()
  110. args.func(args)
  111. def load(self, args):
  112. db_name = args.database or Path(args.dump_file).stem
  113. self._check_target(db_name, delete_if_exists=args.force)
  114. url = urllib.parse.urlparse(args.dump_file)
  115. if url.scheme:
  116. eprint(f"Fetching {args.dump_file}...", end='')
  117. r = requests.get(args.dump_file, timeout=10)
  118. if not r.ok:
  119. exit(f" unable to fetch {args.dump_file}: {r.reason}")
  120. eprint(" done")
  121. dump_file = io.BytesIO(r.content)
  122. else:
  123. eprint(f"Restoring {args.dump_file}...")
  124. dump_file = args.dump_file
  125. if not zipfile.is_zipfile(dump_file):
  126. exit("Not a zipped dump file, use `pg_restore` to restore raw dumps,"
  127. " and `psql` to execute sql dumps or scripts.")
  128. restore_db(db=db_name, dump_file=dump_file, copy=True, neutralize_database=args.neutralize)
  129. def dump(self, args):
  130. if args.dump_path == '-':
  131. dump_db(args.database, sys.stdout.buffer)
  132. else:
  133. with open(args.dump_path, 'wb') as f:
  134. dump_db(args.database, f)
  135. def duplicate(self, args):
  136. self._check_target(args.target, delete_if_exists=args.force)
  137. exp_duplicate_database(args.source, args.target, neutralize_database=args.neutralize)
  138. def rename(self, args):
  139. self._check_target(args.target, delete_if_exists=args.force)
  140. exp_rename(args.source, args.target)
  141. def drop(self, args):
  142. if not exp_drop(args.database):
  143. exit(f"Database {args.database} does not exist.")
  144. def _check_target(self, target, *, delete_if_exists):
  145. if exp_db_exist(target):
  146. if delete_if_exists:
  147. exp_drop(target)
  148. else:
  149. exit(f"Target database {target} exists, aborting.\n\n"
  150. f"\tuse `--force` to delete the existing database anyway.")