Skip to content

Commit

Permalink
✨(cli) add migration check command
Browse files Browse the repository at this point in the history
The Warren CLI Alembic wrapper lacked a command to verify if the database has
any unapplied migrations.
This update introduces a migration check command, which will exit with a status
of 1 if there are unapplied migrations in the target database.
  • Loading branch information
wilbrdt committed May 21, 2024
1 parent 9510050 commit 1fe91eb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to
### Added

- Send LTI course id to the frontend
- Add a warren migration check CLI command

### Changed

Expand Down
10 changes: 10 additions & 0 deletions src/api/core/warren/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from uuid import UUID

import click
from alembic.util import CommandError
from pydantic import BaseModel

from warren import __version__ as warren_version
Expand Down Expand Up @@ -42,6 +43,15 @@ def migration():
"""Database migration commands (alembic wrapper)."""


@migration.command()
def check():
"""Check database migration."""
try:
alembic_migrations.check()
except CommandError:
raise click.ClickException("Target database is not up to date.") from None


@migration.command()
@click.option("--verbose", "-v", is_flag=True, default=False)
def current(verbose: bool):
Expand Down
5 changes: 5 additions & 0 deletions src/api/core/warren/migrations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
ALEMBIC_CFG.set_main_option("sqlalchemy.url", settings.DATABASE_URL)


def check():
"""Check if database has unapplied migrations."""
command.check(ALEMBIC_CFG)


def current(verbose: bool = False):
"""Get information about the current migration state."""
command.current(ALEMBIC_CFG, verbose=verbose)
Expand Down
19 changes: 18 additions & 1 deletion src/api/core/warren/tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""Test Warren commands functions."""

# ruff: noqa: S106
from unittest.mock import AsyncMock, MagicMock
from unittest.mock import AsyncMock, MagicMock, Mock

import pytest
from alembic import command as alembic_command
from alembic.util import CommandError
from click import BadParameter
from click.testing import CliRunner
from pydantic import BaseModel
Expand All @@ -23,6 +24,22 @@
from warren.xi.schema import Experience


def test_migration_check_command(monkeypatch):
"""Test warren check command."""
monkeypatch.setattr(alembic_command, "check", MagicMock())

runner = CliRunner()
runner.invoke(cli, ["migration", "check"])
alembic_command.check.assert_called_with(migrations.ALEMBIC_CFG)

monkeypatch.setattr(alembic_command, "check", Mock(side_effect=CommandError))

runner = CliRunner()
result = runner.invoke(cli, ["migration", "check"])
alembic_command.check.assert_called_with(migrations.ALEMBIC_CFG)
assert result.exit_code == 1


def test_migration_current_command(monkeypatch):
"""Test warren current command."""
monkeypatch.setattr(alembic_command, "current", MagicMock())
Expand Down

0 comments on commit 1fe91eb

Please sign in to comment.