From 579894632f3e2a836ebf077bd4b934ff1a1d62c4 Mon Sep 17 00:00:00 2001 From: Kevin Meinhardt Date: Mon, 23 Sep 2024 13:41:30 +0200 Subject: [PATCH] https://github.com/mozilla/addons-server/pull/22693 Use db_backup to manage database dump/load TMP: fix docs Align file names and command names Reindex on data_load Fix broken class assignment and add better error handling TMP: Why is this test failing ..now Remove dead settings --- src/olympia/amo/management/base.py | 48 ++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/olympia/amo/management/base.py diff --git a/src/olympia/amo/management/base.py b/src/olympia/amo/management/base.py new file mode 100644 index 00000000000..de8f0da6f42 --- /dev/null +++ b/src/olympia/amo/management/base.py @@ -0,0 +1,48 @@ +import logging +import os +import shutil + +from django.conf import settings +from django.core.management.base import BaseCommand, CommandError + + +class BaseDataCommand(BaseCommand): + # Settings for django-dbbackup + data_backup_dirname = os.path.abspath(os.path.join(settings.ROOT, 'backups')) + data_backup_init = '_init' + data_backup_db_filename = 'db.sql' + data_backup_storage_filename = 'storage.tar' + + logger = logging + + def backup_dir_path(self, name): + return os.path.abspath(os.path.join(self.data_backup_dirname, name)) + + def backup_db_path(self, name): + return os.path.abspath( + os.path.join(self.backup_dir_path(name), self.data_backup_db_filename) + ) + + def backup_storage_path(self, name): + return os.path.abspath( + os.path.join(self.backup_dir_path(name), self.data_backup_storage_filename) + ) + + def clean_dir(self, name: str) -> None: + path = self.backup_dir_path(name) + logging.info(f'Clearing {path}') + shutil.rmtree(path, ignore_errors=True) + + def make_dir(self, name: str, force: bool = False) -> None: + path = self.backup_dir_path(name) + path_exists = os.path.exists(path) + + if path_exists: + if force: + self.clean_dir(name) + else: + raise CommandError( + f'path {path} already exists.' 'Use --force to overwrite.' + ) + + os.makedirs(path, exist_ok=True)