Skip to content

Commit ad1a431

Browse files
authored
CLI: Validate storage in verdi storage version (aiidateam#6551)
The `verdi storage version`, in addition to printing the version of the code's and storage's schema, now also validates the storage. If the storage is corrupt or cannot be reached, the command returns the exit code 3. If the storage and code schema versions are incompatible, exit code 4 is returned. This way this command serves as an alternative to running `verdi storage migrate` as a way to check whether a profile needs to be migrated. The `verdi storage migrate` command needs to perform checks such as whether the daemon is running and so is always going to be slower.
1 parent 7402c17 commit ad1a431

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

src/aiida/cmdline/commands/cmd_storage.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
###########################################################################
99
"""`verdi storage` commands."""
1010

11+
import sys
12+
1113
import click
1214
from click_spinner import spinner
1315

@@ -24,14 +26,37 @@ def verdi_storage():
2426

2527
@verdi_storage.command('version')
2628
def storage_version():
27-
"""Print the current version of the storage schema."""
29+
"""Print the current version of the storage schema.
30+
31+
The command returns the following exit codes:
32+
33+
* 0: If the storage schema is equal and compatible to the schema version of the code
34+
* 3: If the storage cannot be reached or is corrupt
35+
* 4: If the storage schema is compatible with the code schema version and probably needs to be migrated.
36+
"""
2837
from aiida import get_profile
38+
from aiida.common.exceptions import CorruptStorage, IncompatibleStorageSchema, UnreachableStorage
2939

30-
profile = get_profile()
31-
head_version = profile.storage_cls.version_head()
32-
profile_version = profile.storage_cls.version_profile(profile)
33-
echo.echo(f'Latest storage schema version: {head_version!r}')
34-
echo.echo(f'Storage schema version of {profile.name!r}: {profile_version!r}')
40+
try:
41+
profile = get_profile()
42+
head_version = profile.storage_cls.version_head()
43+
profile_version = profile.storage_cls.version_profile(profile)
44+
echo.echo(f'Latest storage schema version: {head_version!r}')
45+
echo.echo(f'Storage schema version of {profile.name!r}: {profile_version!r}')
46+
except Exception as exception:
47+
echo.echo_critical(f'Failed to determine the storage version: {exception}')
48+
49+
try:
50+
profile.storage_cls(profile)
51+
except (CorruptStorage, UnreachableStorage) as exception:
52+
echo.echo_error(f'The storage cannot be reached or is corrupt: {exception}')
53+
sys.exit(3)
54+
except IncompatibleStorageSchema:
55+
echo.echo_error(
56+
f'The storage schema version {profile_version} is incompatible with the code version {head_version}.'
57+
'Run `verdi storage migrate` to migrate the storage.'
58+
)
59+
sys.exit(4)
3560

3661

3762
@verdi_storage.command('migrate')

tests/cmdline/commands/test_storage.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,26 @@ def tests_storage_version(run_cli_command):
2323
assert version in result.output
2424

2525

26+
@pytest.mark.parametrize(
27+
'exception_cls, exit_code',
28+
(
29+
(exceptions.CorruptStorage, 3),
30+
(exceptions.UnreachableStorage, 3),
31+
(exceptions.IncompatibleStorageSchema, 4),
32+
),
33+
)
34+
def tests_storage_version_non_zero_exit_code(aiida_profile, run_cli_command, monkeypatch, exception_cls, exit_code):
35+
"""Test the ``verdi storage version`` command when it returns a non-zero exit code."""
36+
37+
def validate_storage(self):
38+
raise exception_cls()
39+
40+
with monkeypatch.context() as context:
41+
context.setattr(aiida_profile.storage_cls.migrator, 'validate_storage', validate_storage)
42+
result = run_cli_command(cmd_storage.storage_version, raises=True)
43+
assert result.exit_code == exit_code
44+
45+
2646
def tests_storage_info(aiida_localhost, run_cli_command):
2747
"""Test the ``verdi storage info`` command with the ``--detailed`` option."""
2848
from aiida import orm

0 commit comments

Comments
 (0)