Skip to content

Commit e187edf

Browse files
committed
typing and other small fixes
1 parent f4eb8e9 commit e187edf

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

src/sentry/new_migrations/monkey/models.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
from django.db.migrations import DeleteModel
22
from django_zero_downtime_migrations.backends.postgres.schema import UnsafeOperationException
33

4-
from sentry.new_migrations.monkey.state import DeletionAction
4+
from sentry.db.postgres.schema import SafePostgresDatabaseSchemaEditor
5+
from sentry.new_migrations.monkey.state import DeletionAction, SentryProjectState
56

67

78
class SafeDeleteModel(DeleteModel):
8-
def __init__(self, *args, deletion_action: DeletionAction | None = None, **kwargs):
9-
if deletion_action is None:
10-
raise UnsafeOperationException("Deletion State is required")
9+
def __init__(self, *args, deletion_action: DeletionAction, **kwargs):
1110
super().__init__(*args, **kwargs)
1211
self.deletion_action = deletion_action
1312

14-
def state_forwards(self, app_label, state):
13+
def state_forwards(self, app_label: str, state: SentryProjectState) -> None: # type: ignore[override]
1514
if self.deletion_action == DeletionAction.MOVE_TO_PENDING:
1615
model = state.apps.get_model(app_label, self.name)
1716
fields_with_constraints = [
@@ -25,22 +24,34 @@ def state_forwards(self, app_label, state):
2524
)
2625
state.remove_model(app_label, self.name_lower, deletion_action=self.deletion_action)
2726

28-
def database_forwards(self, app_label, schema_editor, from_state, to_state):
27+
def database_forwards(
28+
self,
29+
app_label: str,
30+
schema_editor: SafePostgresDatabaseSchemaEditor, # type: ignore[override]
31+
from_state: SentryProjectState, # type: ignore[override]
32+
to_state: SentryProjectState, # type: ignore[override]
33+
) -> None:
2934
if self.deletion_action == DeletionAction.MOVE_TO_PENDING:
3035
return
3136

3237
model = from_state.get_pending_deletion_model(app_label, self.name)
3338
if self.allow_migrate_model(schema_editor.connection.alias, model):
3439
schema_editor.delete_model(model, is_safe=True)
3540

36-
def database_backwards(self, app_label, schema_editor, from_state, to_state):
41+
def database_backwards(
42+
self,
43+
app_label: str,
44+
schema_editor: SafePostgresDatabaseSchemaEditor, # type: ignore[override]
45+
from_state: SentryProjectState, # type: ignore[override]
46+
to_state: SentryProjectState, # type: ignore[override]
47+
) -> None:
3748
if self.deletion_action == DeletionAction.MOVE_TO_PENDING:
3849
return
3950
model = to_state.get_pending_deletion_model(app_label, self.name)
4051
if self.allow_migrate_model(schema_editor.connection.alias, model):
4152
schema_editor.create_model(model)
4253

43-
def describe(self):
54+
def describe(self) -> str:
4455
if self.deletion_action == DeletionAction.MOVE_TO_PENDING:
4556
return f"Moved model {self.name} to pending deletion state"
4657
else:

src/sentry/new_migrations/monkey/state.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
from __future__ import annotations
2+
13
from copy import deepcopy
24
from enum import Enum
35

46
from django.db.migrations.state import ProjectState
7+
from django.db.models import Model
58
from django_zero_downtime_migrations.backends.postgres.schema import UnsafeOperationException
69

710

@@ -13,9 +16,9 @@ class DeletionAction(Enum):
1316
class SentryProjectState(ProjectState):
1417
def __init__(self, *args, **kwargs):
1518
super().__init__(*args, **kwargs)
16-
self.pending_deletion_models = {}
19+
self.pending_deletion_models: dict[tuple[str, str], type[Model]] = {}
1720

18-
def get_pending_deletion_model(self, app_label, model_name):
21+
def get_pending_deletion_model(self, app_label: str, model_name: str) -> type[Model]:
1922
model_key = (app_label.lower(), model_name.lower())
2023
if model_key not in self.pending_deletion_models:
2124
raise UnsafeOperationException(
@@ -24,8 +27,10 @@ def get_pending_deletion_model(self, app_label, model_name):
2427
)
2528
return self.pending_deletion_models[model_key]
2629

27-
def remove_model(self, app_label, model_name, deletion_action: DeletionAction | None = None):
28-
model_key = (app_label, model_name)
30+
def remove_model(
31+
self, app_label: str, model_name: str, deletion_action: DeletionAction | None = None
32+
) -> None:
33+
model_key = (app_label.lower(), model_name.lower())
2934
if deletion_action == DeletionAction.DELETE:
3035
if model_key not in self.pending_deletion_models:
3136
raise UnsafeOperationException(
@@ -43,7 +48,7 @@ def remove_model(self, app_label, model_name, deletion_action: DeletionAction |
4348
self.pending_deletion_models[model_key] = self.apps.get_model(app_label, model_name)
4449
super().remove_model(app_label, model_name)
4550

46-
def clone(self):
51+
def clone(self) -> SentryProjectState:
4752
new_state = super().clone()
4853
new_state.pending_deletion_models = deepcopy(self.pending_deletion_models) # type: ignore[attr-defined]
49-
return new_state
54+
return new_state # type: ignore[return-value]

0 commit comments

Comments
 (0)