You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.8 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (C) 2018 MuK IT GmbH
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. ###################################################################################
  19. import os
  20. import json
  21. import uuid
  22. import logging
  23. import shutil
  24. import zipfile
  25. import tempfile
  26. from contextlib import closing
  27. from odoo import _, modules, api, sql_db, SUPERUSER_ID
  28. from odoo.tools import osutil, config, exec_pg_command
  29. from odoo.service import db
  30. from odoo.addons.muk_utils.tools import patch
  31. from odoo.addons.muk_fields_file.fields import file
  32. _logger = logging.getLogger(__name__)
  33. @patch.monkey_patch(db)
  34. @db.check_db_management_enabled
  35. def exp_duplicate_database(db_original_name, db_name):
  36. res = exp_duplicate_database.super(db_original_name, db_name)
  37. from_files = file.get_store_path(db_original_name)
  38. to_files = file.get_store_path(db_name)
  39. if os.path.exists(from_files) and not os.path.exists(to_files):
  40. shutil.copytree(from_files, to_files)
  41. return res
  42. @patch.monkey_patch(db)
  43. @db.check_db_management_enabled
  44. def exp_drop(db_name):
  45. res = exp_drop.super(db_name)
  46. files = file.get_store_path(db_name)
  47. if os.path.exists(files):
  48. shutil.rmtree(files)
  49. return res
  50. @patch.monkey_patch(db)
  51. @db.check_db_management_enabled
  52. def dump_db(db_name, stream, backup_format='zip'):
  53. if backup_format == 'zip':
  54. res = dump_db.super(db_name, False, backup_format)
  55. with osutil.tempdir() as dump_dir:
  56. with zipfile.ZipFile(res, 'r') as zip:
  57. zip.extractall(dump_dir)
  58. files = file.get_store_path(db_name)
  59. if os.path.exists(files):
  60. shutil.copytree(files, os.path.join(dump_dir, 'files'))
  61. if stream:
  62. osutil.zip_dir(dump_dir, stream, include_dir=False, fnct_sort=lambda file_name: file_name != 'dump.sql')
  63. else:
  64. t=tempfile.TemporaryFile()
  65. osutil.zip_dir(dump_dir, t, include_dir=False, fnct_sort=lambda file_name: file_name != 'dump.sql')
  66. t.seek(0)
  67. return t
  68. else:
  69. return dump_db.super(db_name, stream, backup_format)
  70. @patch.monkey_patch(db)
  71. @db.check_db_management_enabled
  72. def restore_db(db, dump_file, copy=False):
  73. res = restore_db.super(db, dump_file, copy)
  74. with osutil.tempdir() as dump_dir:
  75. if zipfile.is_zipfile(dump_file):
  76. with zipfile.ZipFile(dump_file, 'r') as zip:
  77. files = [m for m in zip.namelist() if m.startswith('files/')]
  78. if files:
  79. z.extractall(dump_dir, files)
  80. files_path = os.path.join(dump_dir, 'files')
  81. shutil.move(files_path, file.get_store_path(db_name))
  82. return res
  83. @patch.monkey_patch(db)
  84. @db.check_db_management_enabled
  85. def exp_rename(old_name, new_name):
  86. res = exp_rename.super(old_name, new_name)
  87. from_files = file.get_store_path(old_name)
  88. to_files = file.get_store_path(new_name)
  89. if os.path.exists(from_files) and not os.path.exists(to_files):
  90. shutil.copytree(from_files, to_files)
  91. return res