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

Update migrations list command to show migrations that no longer exist in the codebase #6299

Merged
merged 4 commits into from
Sep 13, 2024
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
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()
onkar marked this conversation as resolved.
Show resolved Hide resolved
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
Loading