Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

👌 IMPROVE: Configuration migrations #5319

Merged
merged 14 commits into from
Jan 21, 2022
16 changes: 16 additions & 0 deletions aiida/cmdline/commands/cmd_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
# For further information please visit http://www.aiida.net #
###########################################################################
"""`verdi config` command."""
import json
from pathlib import Path
import textwrap

import click

from aiida.cmdline.commands.cmd_verdi import verdi
from aiida.cmdline.params import arguments
from aiida.cmdline.utils import echo
from aiida.manage.configuration import MIGRATIONS, downgrade_config, get_config_path
from aiida.manage.configuration.settings import DEFAULT_CONFIG_INDENT_SIZE


@verdi.group('config')
Expand Down Expand Up @@ -190,3 +194,15 @@ def verdi_config_caching(disabled):
echo.echo(identifier)
elif disabled:
echo.echo(identifier)


@verdi_config.command('downgrade')
@click.argument('version', type=click.Choice({str(m.down_revision) for m in MIGRATIONS}))
def verdi_config_downgrade(version):
"""Print a configuration, downgraded to a specific version."""
path = Path(get_config_path())
echo.echo_report(f'Downgrading configuration to v{version}: {path}')
config = json.loads(path.read_text(encoding='utf8'))
downgrade_config(config, int(version))
path.write_text(json.dumps(config, indent=DEFAULT_CONFIG_INDENT_SIZE), encoding='utf8')
echo.echo_success('Downgraded')
3 changes: 3 additions & 0 deletions aiida/manage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
'ConfigValidationError',
'DEFAULT_DBINFO',
'DeliveryFailed',
'MIGRATIONS',
'OLDEST_COMPATIBLE_CONFIG_VERSION',
'Option',
'Postgres',
Expand All @@ -50,6 +51,7 @@
'config_needs_migrating',
'config_schema',
'disable_caching',
'downgrade_config',
'enable_caching',
'get_current_version',
'get_manager',
Expand All @@ -58,6 +60,7 @@
'get_use_cache',
'parse_option',
'reset_manager',
'upgrade_config',
'write_database_integrity_violation',
)

Expand Down
3 changes: 3 additions & 0 deletions aiida/manage/configuration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@
'CURRENT_CONFIG_VERSION',
'Config',
'ConfigValidationError',
'MIGRATIONS',
'OLDEST_COMPATIBLE_CONFIG_VERSION',
'Option',
'Profile',
'check_and_migrate_config',
'config_needs_migrating',
'config_schema',
'downgrade_config',
'get_current_version',
'get_option',
'get_option_names',
'parse_option',
'upgrade_config',
)

# yapf: enable
Expand Down
2 changes: 1 addition & 1 deletion aiida/manage/configuration/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def from_file(cls, filepath):
migrated = False

# If the configuration file needs to be migrated first create a specific backup so it can easily be reverted
if config_needs_migrating(config):
if config_needs_migrating(config, filepath):
migrated = True
echo.echo_warning(f'current configuration file `{filepath}` is outdated and will be migrated')
filepath_backup = cls._backup(filepath)
Expand Down
4 changes: 3 additions & 1 deletion aiida/manage/configuration/migrations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
# pylint: disable=wildcard-import

from .migrations import *
from .utils import *

__all__ = (
'CURRENT_CONFIG_VERSION',
'MIGRATIONS',
'OLDEST_COMPATIBLE_CONFIG_VERSION',
'check_and_migrate_config',
'config_needs_migrating',
'downgrade_config',
'get_current_version',
'upgrade_config',
)

# yapf: enable
Loading