-
Notifications
You must be signed in to change notification settings - Fork 537
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |