Skip to content

Commit

Permalink
Update migrations list command to show migrations that no longer exis…
Browse files Browse the repository at this point in the history
…t in the codebase (#6299)

Fixes #2159

Running `snuba migrations list` will now show migrations that are in
clickhouse but no longer in the codebase (e.g. because snuba was
downgraded).

# Testing
Deleted migration in codebase to check that it shows up in the list.
<img width="668" alt="image"
src="https://github.com/user-attachments/assets/90f855b9-94af-416d-9452-0335c2abd8d6">
  • Loading branch information
davidtsuk authored Sep 13, 2024
1 parent d789447 commit 68b4ebd
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 15 deletions.
2 changes: 1 addition & 1 deletion snuba/admin/clickhouse/migration_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def __init__(
).get_migrations()

migration_statuses = {}
for migration_id, status, _ in migrations:
for migration_id, status, _, _ in migrations:
migration_statuses[migration_id] = {
"migration_id": migration_id,
"status": status,
Expand Down
2 changes: 1 addition & 1 deletion snuba/admin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def migrations_groups_list(group: str) -> Response:
"status": status.value,
"blocking": blocking,
}
for migration_id, status, blocking in runner_group_migrations
for migration_id, status, blocking, _ in runner_group_migrations
]
),
200,
Expand Down
10 changes: 7 additions & 3 deletions snuba/cli/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ def list() -> None:
setup_logging()
check_clickhouse_connections(CLUSTERS)
runner = Runner()
for group, group_migrations in runner.show_all():
for group, group_migrations in runner.show_all(include_nonexistent=True):
readiness_state = get_group_readiness_state(group)
click.echo(f"{group.value} (readiness_state: {readiness_state.value})")
for migration_id, status, blocking in group_migrations:
for migration_id, status, blocking, existing in group_migrations:
symbol = {
Status.COMPLETED: "X",
Status.NOT_STARTED: " ",
Expand All @@ -53,7 +53,11 @@ def list() -> None:
if status != Status.COMPLETED and blocking:
blocking_text = " (blocking)"

click.echo(f"[{symbol}] {migration_id}{in_progress_text}{blocking_text}")
existing_text = "" if existing else " (this migration no longer exists)"

click.echo(
f"[{symbol}] {migration_id}{in_progress_text}{blocking_text}{existing_text}"
)

click.echo()

Expand Down
27 changes: 24 additions & 3 deletions snuba/migrations/runner.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import defaultdict
from datetime import datetime
from functools import partial
from typing import List, Mapping, MutableMapping, NamedTuple, Optional, Sequence, Tuple
Expand Down Expand Up @@ -64,6 +65,7 @@ class MigrationDetails(NamedTuple):
migration_id: str
status: Status
blocking: bool
exists: bool


class Runner:
Expand Down Expand Up @@ -133,7 +135,7 @@ def force_overwrite_status(
)

def show_all(
self, groups: Optional[Sequence[str]] = None
self, groups: Optional[Sequence[str]] = None, include_nonexistent: bool = False
) -> List[Tuple[MigrationGroup, List[MigrationDetails]]]:
"""
Returns the list of migrations and their statuses for each group.
Expand All @@ -148,6 +150,9 @@ def show_all(
migration_groups = get_active_migration_groups()

migration_status = self._get_migration_status(migration_groups)
clickhouse_group_migrations = defaultdict(set)
for group, migration_id in migration_status.keys():
clickhouse_group_migrations[group].add(migration_id)

def get_status(migration_key: MigrationKey) -> Status:
return migration_status.get(migration_key, Status.NOT_STARTED)
Expand All @@ -156,15 +161,31 @@ def get_status(migration_key: MigrationKey) -> Status:
group_migrations: List[MigrationDetails] = []
group_loader = get_group_loader(group)

for migration_id in group_loader.get_migrations():
migration_ids = group_loader.get_migrations()
for migration_id in migration_ids:
migration_key = MigrationKey(group, migration_id)
migration = group_loader.load_migration(migration_id)
group_migrations.append(
MigrationDetails(
migration_id, get_status(migration_key), migration.blocking
migration_id,
get_status(migration_key),
migration.blocking,
True,
)
)

if include_nonexistent:
non_existing_migrations = clickhouse_group_migrations.get(
group, set()
).difference(set(migration_ids))
for migration_id in non_existing_migrations:
migration_key = MigrationKey(group, migration_id)
group_migrations.append(
MigrationDetails(
migration_id, get_status(migration_key), False, False
)
)

migrations.append((group, group_migrations))

return migrations
Expand Down
17 changes: 10 additions & 7 deletions tests/admin/clickhouse_migrations/test_migration_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ def group_loader() -> GroupLoader:


RUN_MIGRATIONS: Sequence[MigrationDetails] = [
MigrationDetails("0001", Status.COMPLETED, True),
MigrationDetails("0002", Status.NOT_STARTED, True),
MigrationDetails("0003", Status.NOT_STARTED, True),
MigrationDetails("0001", Status.COMPLETED, True, True),
MigrationDetails("0002", Status.NOT_STARTED, True, True),
MigrationDetails("0003", Status.NOT_STARTED, True, True),
]


Expand Down Expand Up @@ -62,9 +62,9 @@ def test_status_checker_run(


REVERSE_MIGRATIONS: Sequence[MigrationDetails] = [
MigrationDetails("0001", Status.COMPLETED, True),
MigrationDetails("0002", Status.IN_PROGRESS, True),
MigrationDetails("0003", Status.NOT_STARTED, True),
MigrationDetails("0001", Status.COMPLETED, True, True),
MigrationDetails("0002", Status.IN_PROGRESS, True, True),
MigrationDetails("0003", Status.NOT_STARTED, True, True),
]


Expand Down Expand Up @@ -155,7 +155,10 @@ def test_run_migration_checks_and_policies(
mock_policy = Mock()
checker = mock_checker()
mock_runner.show_all.return_value = [
(MigrationGroup("events"), [MigrationDetails("0001", Status.COMPLETED, True)])
(
MigrationGroup("events"),
[MigrationDetails("0001", Status.COMPLETED, True, True)],
)
]

mock_policy.can_run.return_value = policy_result[0]
Expand Down
20 changes: 20 additions & 0 deletions tests/migrations/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@ def test_show_all_for_groups() -> None:
assert all([migration.status == Status.COMPLETED for migration in migrations])


@pytest.mark.clickhouse_db
def test_show_all_nonexistent_migration() -> None:
runner = Runner()
assert all(
[
migration.status == Status.NOT_STARTED
for (_, group_migrations) in runner.show_all()
for migration in group_migrations
]
)
runner.run_all(force=True)
assert all(
[
migration.status == Status.COMPLETED
for (_, group_migrations) in runner.show_all()
for migration in group_migrations
]
)


@pytest.mark.clickhouse_db
def test_run_migration() -> None:
runner = Runner()
Expand Down

0 comments on commit 68b4ebd

Please sign in to comment.