Skip to content

Commit 9cac232

Browse files
committed
cleaning
1 parent 873f28d commit 9cac232

2 files changed

Lines changed: 7 additions & 25 deletions

File tree

osf/management/commands/migrate_notifications.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242

4343
TIMEOUT_SECONDS = 3600 # 60 minutes timeout
44-
BATCH_SIZE = 1000
44+
BATCH_SIZE = 1000 # batch size, can be changed
4545

4646

4747
@contextmanager
@@ -83,10 +83,10 @@ def migrate_legacy_notification_subscriptions(
8383
def timeout_handler(signum, frame):
8484
raise TimeoutError("Batch processing timed out")
8585

86-
# Precompute notification type IDs
86+
# Notification type IDs
8787
notiftype_map = dict(NotificationType.objects.values_list('name', 'id'))
8888

89-
# Cache existing keys only once
89+
# Cache existing keys
9090
existing_keys = set(
9191
(
9292
user_id,
@@ -115,7 +115,7 @@ def timeout_handler(signum, frame):
115115
.order_by("id")[:batch_size]
116116
)
117117
if not batch:
118-
break # done
118+
break
119119
subscriptions_to_create = []
120120

121121
signal.signal(signal.SIGALRM, timeout_handler)
@@ -180,7 +180,7 @@ def timeout_handler(signum, frame):
180180
)
181181
created += len(subscriptions_to_create)
182182

183-
# Logging with ETA
183+
# Logging ETA
184184
batch_time = time.time() - batch_start_time
185185
processed = last_id + len(batch)
186186
rate = processed / (time.time() - start_time_total)

osf_tests/management_commands/test_migrate_notifications.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22
from django.contrib.contenttypes.models import ContentType
3-
from django.db import IntegrityError, transaction
3+
from django.db import transaction
44

55
from osf.models import Node, RegistrationProvider
66
from osf_tests.factories import (
@@ -49,7 +49,6 @@ def provider2(self):
4949
def node(self):
5050
return ProjectFactory()
5151

52-
5352
ALL_PROVIDER_EVENTS = [
5453
'new_pending_withdraw_requests',
5554
'contributor_added_preprint',
@@ -96,9 +95,7 @@ def test_migrate_provider_subscription(self, users, provider, provider2):
9695
self.create_legacy_sub(event_name='new_pending_submissions', users=users, provider=provider)
9796
self.create_legacy_sub(event_name='new_pending_submissions', users=users, provider=provider2)
9897
self.create_legacy_sub(event_name='new_pending_submissions', users=users, provider=RegistrationProvider.get_default())
99-
10098
migrate_legacy_notification_subscriptions()
101-
10299
subs = NotificationSubscription.objects.filter(
103100
notification_type__name=NotificationType.Type.PROVIDER_NEW_PENDING_SUBMISSIONS
104101
)
@@ -109,15 +106,11 @@ def test_migrate_provider_subscription(self, users, provider, provider2):
109106

110107
def test_migrate_node_subscription(self, users, user, node):
111108
self.create_legacy_sub('file_updated', users, user=user, node=node)
112-
113109
migrate_legacy_notification_subscriptions()
114-
115110
nt = NotificationType.objects.get(name=NotificationType.Type.NODE_FILE_UPDATED)
116111
assert nt.object_content_type == ContentType.objects.get_for_model(Node)
117-
118112
subs = NotificationSubscription.objects.filter(notification_type=nt)
119113
assert subs.count() == 1
120-
121114
for sub in subs:
122115
assert sub.subscribed_object == node
123116

@@ -140,8 +133,6 @@ def test_idempotent_migration(self, users, user, node, provider):
140133

141134
def test_migrate_all_subscription_types(self, users, user, provider, provider2, node):
142135
providers = [provider, provider2]
143-
# Create legacy subscriptions for all events
144-
145136
for event_name in self.ALL_EVENT_NAMES:
146137
if event_name in self.ALL_PROVIDER_EVENTS:
147138
self.create_legacy_sub(event_name=event_name, users=users, user=user, node=node, provider=provider)
@@ -168,7 +159,7 @@ def test_migrate_all_subscription_types(self, users, user, provider, provider2,
168159
notification_type__name=nt_name
169160
)
170161
assert nt_objs.exists()
171-
# Optional: Verify subscriptions belong to correct objects
162+
# Verify subscriptions belong to correct objects
172163
for provider in providers:
173164
nt_name = NotificationType.Type.PROVIDER_NEW_PENDING_SUBMISSIONS
174165
content_type = ContentType.objects.get_for_model(provider.__class__)
@@ -194,16 +185,11 @@ def failing_migration():
194185

195186
with pytest.raises(RuntimeError):
196187
failing_migration()
197-
# nothing persisted
198188
assert NotificationSubscription.objects.filter(user=user).count() == 0
199189

200190
def test_migrate_skips_invalid_data(self, users, user, node, provider):
201-
# Create a legacy subscription with an invalid event name
202191
self.create_legacy_sub(event_name='wrong_data', users=users, user=user, node=node, provider=provider)
203-
204192
migrate_legacy_notification_subscriptions()
205-
206-
# Invalid subs should be ignored, so no new subscriptions are created for this user
207193
assert NotificationSubscription.objects.filter(user=user).count() == 0
208194

209195
def test_migrate_batch_with_valid_and_invalid(self, users, user, node, provider):
@@ -223,11 +209,7 @@ def test_migrate_batch_with_valid_and_invalid(self, users, user, node, provider)
223209
node=node,
224210
provider=provider,
225211
)
226-
227212
migrate_legacy_notification_subscriptions()
228-
229-
# Valid one should be migrated
230213
assert NotificationSubscription.objects.filter(user=user).count() == 1
231-
# Check that the migrated subscription corresponds to the valid type
232214
migrated = NotificationSubscription.objects.filter(user=user).first()
233215
assert migrated.notification_type.name == NotificationType.Type.PROVIDER_REVIEWS_RESUBMISSION_CONFIRMATION.value

0 commit comments

Comments
 (0)