Skip to content

Commit

Permalink
Config: fix bug in downgrade past version 6 (#5528)
Browse files Browse the repository at this point in the history
Downgrading the configuration beyond version 6 would result in a `KeyError`
because the `_v6_backend` key was popped at the wrong level.
  • Loading branch information
kjappelbaum authored May 19, 2022
1 parent 3e92862 commit f5f8ef7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion aiida/manage/configuration/migrations/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def upgrade(self, config: ConfigType) -> None:
def downgrade(self, config: ConfigType) -> None:
for profile_name, profile in config.get('profiles', {}).items():
if '_v6_backend' in profile.get('storage', {}):
profile.setdefault('storage', {})['backend'] = profile.pop('_v6_backend')
profile.setdefault('storage', {})['backend'] = profile['storage'].pop('_v6_backend')
else:
CONFIG_LOGGER.warning(f'profile {profile_name!r} had no expected "storage._v6_backend" key')

Expand Down
19 changes: 19 additions & 0 deletions tests/manage/configuration/migrations/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@ def test_migrate_individual(load_config_sample, initial, target, monkeypatch):
assert config_migrated == config_target


def test_merge_storage_backends_downgrade_profile(empty_config, profile_factory, caplog):
"""Test the downgrade of schema version 7.
Test specifically the case that the ``storage._v6_backend`` key does not exist.
"""
config = empty_config
profile_a = profile_factory('profile_a', test_profile=False)
profile_b = profile_factory('profile_b', test_profile=False)

profile_a._attributes[profile_a.KEY_STORAGE]['_v6_backend'] = 'django' # pylint: disable=protected-access

config.add_profile(profile_a)
config.add_profile(profile_b)

config_migrated = downgrade_config(config.dictionary, 6)
assert list(config_migrated['profiles'].keys()) == ['profile_a', 'profile_b']
assert f'profile {profile_b.name!r} had no expected "storage._v6_backend" key' in caplog.records[0].message


def test_add_test_profile_key_downgrade_profile(empty_config, profile_factory, caplog):
"""Test the downgrade of schema version 8.
Expand Down

0 comments on commit f5f8ef7

Please sign in to comment.