Skip initial migration if seeding on initialize.py

This commit is contained in:
Kevin Meinhardt 2024-11-15 15:24:58 +01:00
Родитель 03fae8133e
Коммит e7014fe61f
2 изменённых файлов: 10 добавлений и 12 удалений

Просмотреть файл

@ -33,6 +33,7 @@ class Command(BaseDataCommand):
""" """
Create the database. Create the database.
""" """
logging.info(f'options: {options}')
# We need to support skipping loading/seeding when desired. # We need to support skipping loading/seeding when desired.
# Like in CI environments where you don't want to load data every time. # Like in CI environments where you don't want to load data every time.
if settings.DATA_BACKUP_SKIP: if settings.DATA_BACKUP_SKIP:
@ -41,10 +42,12 @@ class Command(BaseDataCommand):
) )
return return
clean = options.get('clean') # If DB empty or we are explicitly cleaning, then bail with data_seed.
load = options.get('load') if options.get('clean') or not self.local_admin_exists():
logging.info(f'options: {options}') call_command('data_seed')
return
load = options.get('load')
# We always migrate the DB. # We always migrate the DB.
logging.info('Migrating...') logging.info('Migrating...')
call_command('migrate', '--noinput') call_command('migrate', '--noinput')
@ -52,9 +55,6 @@ class Command(BaseDataCommand):
# If we specify a specifi backup, simply load that. # If we specify a specifi backup, simply load that.
if load: if load:
call_command('data_load', '--name', load) call_command('data_load', '--name', load)
# If DB empty or we are explicitly cleaning, then reseed.
elif clean or not self.local_admin_exists():
call_command('data_seed')
# We should reindex even if no data is loaded/modified # We should reindex even if no data is loaded/modified
# because we might have a fresh instance of elasticsearch # because we might have a fresh instance of elasticsearch
else: else:

Просмотреть файл

@ -464,16 +464,15 @@ class TestInitializeDataCommand(BaseTestDataCommand):
def test_handle_with_clean_and_load_arguments(self): def test_handle_with_clean_and_load_arguments(self):
""" """
Test running the 'initialize' command with both '--clean' and '--load' Test running the 'initialize' command with both '--clean' and '--load'
arguments. Expected: Command should prioritize '--load' and perform arguments. Expected: Command should prioritize '--clean' and perform
migration, loading. migration and seeding.
""" """
name = 'test' name = 'test'
call_command('initialize', clean=True, load=name) call_command('initialize', clean=True, load=name)
self._assert_commands_called_in_order( self._assert_commands_called_in_order(
self.mocks['mock_call_command'], self.mocks['mock_call_command'],
[ [
self.mock_commands.migrate, self.mock_commands.data_seed,
self.mock_commands.data_load(name),
], ],
) )
@ -487,7 +486,6 @@ class TestInitializeDataCommand(BaseTestDataCommand):
self._assert_commands_called_in_order( self._assert_commands_called_in_order(
self.mocks['mock_call_command'], self.mocks['mock_call_command'],
[ [
self.mock_commands.migrate,
self.mock_commands.data_seed, self.mock_commands.data_seed,
], ],
) )
@ -518,7 +516,6 @@ class TestInitializeDataCommand(BaseTestDataCommand):
self._assert_commands_called_in_order( self._assert_commands_called_in_order(
self.mocks['mock_call_command'], self.mocks['mock_call_command'],
[ [
self.mock_commands.migrate,
self.mock_commands.data_seed, self.mock_commands.data_seed,
], ],
) )
@ -529,6 +526,7 @@ class TestInitializeDataCommand(BaseTestDataCommand):
Expected: The command exits with an error and does not proceed to seeding Expected: The command exits with an error and does not proceed to seeding
or loading data. or loading data.
""" """
self.with_local_admin()
self.mocks['mock_call_command'].side_effect = Exception('test') self.mocks['mock_call_command'].side_effect = Exception('test')
with pytest.raises(Exception) as context: with pytest.raises(Exception) as context:
call_command('initialize') call_command('initialize')