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

verdi status: distinguish database schema version incompatible #4319

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions aiida/cmdline/commands/cmd_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
# For further information please visit http://www.aiida.net #
###########################################################################
"""`verdi status` command."""
import enum
import sys

import enum
import click

from aiida.cmdline.commands.cmd_verdi import verdi
from aiida.cmdline.utils import echo
from aiida.common.log import override_log_level
from aiida.common.exceptions import IncompatibleDatabaseSchema
from ..utils.echo import ExitCode


Expand Down Expand Up @@ -51,7 +52,7 @@ class ServiceStatus(enum.IntEnum):
@click.option('--no-rmq', is_flag=True, help='Do not check RabbitMQ status')
def verdi_status(no_rmq):
"""Print status of AiiDA services."""
# pylint: disable=broad-except,too-many-statements
# pylint: disable=broad-except,too-many-statements,too-many-branches
from aiida.cmdline.utils.daemon import get_daemon_status, delete_stale_pid_file
from aiida.common.utils import Capturing
from aiida.manage.external.rmq import get_rmq_url
Expand Down Expand Up @@ -93,8 +94,13 @@ def verdi_status(no_rmq):
with override_log_level(): # temporarily suppress noisy logging
backend = manager.get_backend()
backend.cursor()
except Exception:
print_status(ServiceStatus.DOWN, 'postgres', 'Unable to connect as {}@{}:{}'.format(*database_data))
except IncompatibleDatabaseSchema:
message = 'Database schema version is incompatible with the code: run `verdi database migrate`.'
print_status(ServiceStatus.DOWN, 'postgres', message)
exit_code = ExitCode.CRITICAL
except Exception as exc:
message = 'Unable to connect as {}@{}:{}'.format(*database_data)
print_status(ServiceStatus.DOWN, 'postgres', message, exception=exc)
exit_code = ExitCode.CRITICAL
else:
print_status(ServiceStatus.UP, 'postgres', 'Connected as {}@{}:{}'.format(*database_data))
Expand Down Expand Up @@ -123,7 +129,6 @@ def verdi_status(no_rmq):
print_status(ServiceStatus.UP, 'daemon', daemon_status)
else:
print_status(ServiceStatus.WARNING, 'daemon', daemon_status)
exit_code = ExitCode.SUCCESS # A daemon that is not running is not a failure

except Exception as exc:
print_status(ServiceStatus.ERROR, 'daemon', 'Error getting daemon status', exception=exc)
Expand Down
15 changes: 15 additions & 0 deletions tests/cmdline/commands/test_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,18 @@ def test_status_no_rmq(run_cli_command):

for string in ['config', 'profile', 'postgres', 'daemon']:
assert string in result.output


def test_database_incompatible(run_cli_command, monkeypatch):
"""Test `verdi status` when database schema version is incompatible with that of the code."""
from aiida.manage.manager import get_manager

def get_backend():
from aiida.common.exceptions import IncompatibleDatabaseSchema
raise IncompatibleDatabaseSchema()

monkeypatch.setattr(get_manager(), 'get_backend', get_backend)

result = run_cli_command(cmd_status.verdi_status, raises=True)
assert 'Database schema version is incompatible with the code: run `verdi database migrate`.' in result.output
assert result.exit_code is ExitCode.CRITICAL