-
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.
Use db_backup to manage database dump/load
- Loading branch information
Showing
9 changed files
with
322 additions
and
227 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
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
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
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,50 @@ | ||
import logging | ||
import os | ||
import shutil | ||
|
||
from django.conf import settings | ||
from django.core.management import call_command | ||
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' | ||
|
||
call_command = call_command | ||
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 and not force: | ||
raise CommandError( | ||
f'path {path} already exists.' 'Use --force to overwrite.' | ||
) | ||
|
||
self.clean_dir(name) | ||
os.makedirs(path, exist_ok=True) |
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
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
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 |
---|---|---|
@@ -1,48 +1,42 @@ | ||
import logging | ||
import os | ||
import shutil | ||
|
||
from django.conf import settings | ||
from django.core.management import call_command | ||
from django.core.management.base import BaseCommand | ||
|
||
from ..base import BaseDataCommand | ||
|
||
|
||
class Command(BaseCommand): | ||
class Command(BaseDataCommand): | ||
help = 'Seed the _init data dir with fresh data from the database' | ||
|
||
def handle(self, *args, **options): | ||
init_name = settings.DATA_BACKUP_INIT | ||
init_path = os.path.abspath(os.path.join(settings.DATA_BACKUP_DIR, init_name)) | ||
logging.info(f'Clearing {init_path}') | ||
shutil.rmtree(init_path, ignore_errors=True) | ||
|
||
logging.info('Resetting database...') | ||
call_command('flush', '--noinput') | ||
call_command('migrate', '--noinput') | ||
num_addons = 10 | ||
num_themes = 5 | ||
|
||
self.clean_dir(self.data_backup_init) | ||
|
||
self.logger.info('Resetting database...') | ||
self.call_command('flush', '--noinput') | ||
self.call_command('migrate', '--noinput') | ||
# reindex --wipe will force the ES mapping to be re-installed. Useful to | ||
# make sure the mapping is correct before adding a bunch of add-ons. | ||
call_command('reindex', '--wipe', '--force', '--noinput') | ||
self.call_command('reindex', '--wipe', '--force', '--noinput') | ||
|
||
logging.info('Loading initial data...') | ||
call_command('loaddata', 'initial.json') | ||
call_command('import_prod_versions') | ||
call_command( | ||
self.logger.info('Loading initial data...') | ||
self.call_command('loaddata', 'initial.json') | ||
self.call_command('import_prod_versions') | ||
self.call_command( | ||
'createsuperuser', | ||
'--no-input', | ||
'--username', | ||
settings.LOCAL_ADMIN_USERNAME, | ||
'--email', | ||
settings.LOCAL_ADMIN_EMAIL, | ||
) | ||
call_command('loaddata', 'zadmin/users') | ||
|
||
logging.info('Generating add-ons...') | ||
call_command('generate_addons', '--app', 'firefox', 10) | ||
call_command('generate_addons', '--app', 'android', 10) | ||
call_command('generate_themes', 5) | ||
# These add-ons are specifically useful for the addons-frontend | ||
# homepage. You may have to re-run this, in case the data there | ||
# changes. | ||
call_command('generate_default_addons_for_frontend') | ||
logging.info(f'Dumping data to {init_path}') | ||
call_command('dump_data', '--name', init_name) | ||
self.call_command('loaddata', 'zadmin/users') | ||
|
||
self.logger.info('Generating add-ons...') | ||
self.call_command('generate_addons', '--app', 'firefox', num_addons) | ||
self.call_command('generate_addons', '--app', 'android', num_addons) | ||
self.call_command('generate_themes', num_themes) | ||
|
||
self.call_command('generate_default_addons_for_frontend') | ||
|
||
self.call_command('dump_data', '--name', self.data_backup_init) |
Oops, something went wrong.