-
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 + management commands to implement data dump/load/seed (#…
…22693) * Validate data dump * 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 * Remove dead settings * Move base class to __init__.py * Remove uneeded data_load * Lint * TMP: updates to comments * Fix broken test * Create/migrate DB before seeding.
- Loading branch information
Showing
12 changed files
with
564 additions
and
65 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
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,48 @@ | ||
from datetime import datetime | ||
|
||
from django.core.management import call_command | ||
|
||
from .. import BaseDataCommand | ||
|
||
|
||
class Command(BaseDataCommand): | ||
help = 'Dump data with a specified name' | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
'--name', | ||
type=str, | ||
default=datetime.now().strftime('%Y%m%d%H%M%S'), | ||
help='Name of the data dump', | ||
) | ||
parser.add_argument( | ||
'--force', action='store_true', help='Force overwrite of existing dump' | ||
) | ||
|
||
def handle(self, *args, **options): | ||
name = options.get('name') | ||
force = options.get('force') | ||
|
||
dump_path = self.backup_dir_path(name) | ||
db_path = self.backup_db_path(name) | ||
storage_path = self.backup_storage_path(name) | ||
|
||
try: | ||
self.make_dir(dump_path, force=force) | ||
|
||
call_command( | ||
'dbbackup', | ||
output_path=db_path, | ||
interactive=False, | ||
compress=True, | ||
) | ||
|
||
call_command( | ||
'mediabackup', | ||
output_path=storage_path, | ||
interactive=False, | ||
compress=True, | ||
) | ||
except Exception as e: | ||
self.clean_dir(dump_path) | ||
raise e |
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 os | ||
|
||
from django.core.management import call_command | ||
from django.core.management.base import CommandError | ||
|
||
from .. import BaseDataCommand | ||
|
||
|
||
class Command(BaseDataCommand): | ||
help = 'Load data from a specified name' | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
'--name', | ||
type=str, | ||
required=True, | ||
help='Name of the data dump', | ||
) | ||
|
||
def handle(self, *args, **options): | ||
name = options.get('name') | ||
db_path = self.backup_db_path(name) | ||
storage_path = self.backup_storage_path(name) | ||
|
||
if not os.path.exists(db_path): | ||
print('DB backup not found: {db_path}') | ||
raise CommandError(f'DB backup not found: {db_path}') | ||
|
||
call_command( | ||
'dbrestore', | ||
input_path=db_path, | ||
interactive=False, | ||
uncompress=True, | ||
) | ||
|
||
if not os.path.exists(storage_path): | ||
raise CommandError(f'Storage backup not found: {storage_path}') | ||
|
||
call_command( | ||
'mediarestore', | ||
input_path=storage_path, | ||
interactive=False, | ||
uncompress=True, | ||
replace=True, | ||
) | ||
|
||
# reindex --wipe will force the ES mapping to be re-installed. | ||
# After loading data from a backup, we should always reindex | ||
# to make sure the mapping is correct. | ||
call_command('reindex', '--wipe', '--force', '--noinput') |
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,45 @@ | ||
from django.conf import settings | ||
from django.core.management import call_command | ||
|
||
from .. import BaseDataCommand | ||
|
||
|
||
class Command(BaseDataCommand): | ||
help = ( | ||
'Reset and seed the database with initial data, ' | ||
'generated add-ons, and data from AMO production.' | ||
) | ||
|
||
def handle(self, *args, **options): | ||
num_addons = 10 | ||
num_themes = 5 | ||
|
||
self.clean_dir(self.data_backup_init) | ||
|
||
self.logger.info('Resetting database...') | ||
call_command('flush', '--noinput') | ||
# reindex --wipe will force the ES mapping to be re-installed. | ||
call_command('reindex', '--wipe', '--force', '--noinput') | ||
call_command('migrate', '--noinput') | ||
|
||
self.logger.info('Loading initial data...') | ||
call_command('loaddata', 'initial.json') | ||
call_command('import_prod_versions') | ||
call_command( | ||
'createsuperuser', | ||
'--no-input', | ||
'--username', | ||
settings.LOCAL_ADMIN_USERNAME, | ||
'--email', | ||
settings.LOCAL_ADMIN_EMAIL, | ||
) | ||
call_command('loaddata', 'zadmin/users') | ||
|
||
self.logger.info('Generating add-ons...') | ||
call_command('generate_addons', '--app', 'firefox', num_addons) | ||
call_command('generate_addons', '--app', 'android', num_addons) | ||
call_command('generate_themes', num_themes) | ||
|
||
call_command('generate_default_addons_for_frontend') | ||
|
||
call_command('data_dump', '--name', self.data_backup_init) |
Oops, something went wrong.