Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Update migration command #432

Merged
merged 2 commits into from
Jul 20, 2022
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
14 changes: 10 additions & 4 deletions api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,16 +451,22 @@ def perform_migration(self) -> None:
existing_submissions = Submission.objects.filter(completed_by=self.old_user)
self.affected_submissions.add(*existing_submissions)

# need to process transcriptions first because the submissions they're
# linked to are about to change
transcriptions = Transcription.objects.filter(
submission__in=existing_submissions, author=self.old_user
)
transcriptions.update(author=self.new_user)
Comment on lines +456 to +459
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the submission__in check necessary here? I think we want to just migrate all transcriptions, so we could just migrate all of them.

Or does that not work with the revert?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we keep track of the submissions we act on (for revert purposes) it makes more sense to only modify the transcriptions attached to the submissions we're tracking so that we don't overreach. I agree that in most cases it'll be everything attached to the user, but I can't guarantee that.

existing_submissions.update(
claimed_by=self.new_user, completed_by=self.new_user
)

def get_number_affected(self) -> int:
"""Get the number of records that have been modified."""
return self.affected_submissions.count()

def revert(self) -> None:
"""Undo the account migration."""
transcriptions = Transcription.objects.filter(
submission__in=self.affected_submissions.all(), author=self.new_user
)
transcriptions.update(author=self.old_user)
self.affected_submissions.update(
claimed_by=self.old_user, completed_by=self.old_user
)
35 changes: 31 additions & 4 deletions api/tests/slack/commands/test_account_migration.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from unittest.mock import patch

from api.models import AccountMigration, Submission
from api.models import AccountMigration, Submission, Transcription
from api.slack.commands.migrate_user import (
_create_blocks,
migrate_user_cmd,
process_migrate_user,
)
from blossom.strings import translation
from utils.test_helpers import create_submission, create_user
from utils.test_helpers import create_submission, create_transcription, create_user

i18n = translation()

Expand All @@ -18,20 +18,31 @@ def test_perform_migration() -> None:
user2 = create_user(id=200, username="Moddington")

submission1 = create_submission(claimed_by=user1, completed_by=user1)
create_submission(claimed_by=user2, completed_by=user2)
submission2 = create_submission(claimed_by=user2, completed_by=user2)

transcription1 = create_transcription(submission=submission1, user=user1)
transcription2 = create_transcription(submission=submission2, user=user2)

assert Submission.objects.filter(completed_by=user1).count() == 1
assert Submission.objects.filter(completed_by=user2).count() == 1
assert Transcription.objects.filter(author=user1).count() == 1
assert Transcription.objects.filter(author=user2).count() == 1

migration = AccountMigration.objects.create(old_user=user1, new_user=user2)
migration.perform_migration()
assert migration.affected_submissions.count() == 1
assert Submission.objects.filter(completed_by=user1).count() == 0
assert Submission.objects.filter(completed_by=user2).count() == 2
assert Transcription.objects.filter(author=user1).count() == 0
assert Transcription.objects.filter(author=user2).count() == 2

submission1.refresh_from_db()
transcription1.refresh_from_db()
transcription2.refresh_from_db()
assert submission1.claimed_by == user2
assert submission1.completed_by == user2
assert transcription1.author == user2
assert transcription2.author == user2


def test_revert() -> None:
Expand All @@ -40,7 +51,11 @@ def test_revert() -> None:
user2 = create_user(id=200, username="Moddington")

submission1 = create_submission(claimed_by=user1, completed_by=user1)
create_submission(claimed_by=user2, completed_by=user2)
submission2 = create_submission(claimed_by=user2, completed_by=user2)
transcription1 = create_transcription(submission=submission1, user=user1)
transcription2 = create_transcription(submission=submission2, user=user2)
assert Transcription.objects.filter(author=user1).count() == 1
assert Transcription.objects.filter(author=user2).count() == 1

assert Submission.objects.filter(completed_by=user1).count() == 1
assert Submission.objects.filter(completed_by=user2).count() == 1
Expand All @@ -50,15 +65,27 @@ def test_revert() -> None:

assert Submission.objects.filter(completed_by=user1).count() == 0
assert Submission.objects.filter(completed_by=user2).count() == 2
assert Transcription.objects.filter(author=user1).count() == 0
assert Transcription.objects.filter(author=user2).count() == 2
transcription1.refresh_from_db()
transcription2.refresh_from_db()
assert transcription1.author == user2
assert transcription2.author == user2

migration.revert()

assert Submission.objects.filter(completed_by=user1).count() == 1
assert Submission.objects.filter(completed_by=user2).count() == 1
assert Submission.objects.filter(completed_by=user1).count() == 1
assert Submission.objects.filter(completed_by=user2).count() == 1

submission1.refresh_from_db()
transcription1.refresh_from_db()
transcription2.refresh_from_db()
assert submission1.claimed_by == user1
assert submission1.completed_by == user1
assert transcription1.author == user1
assert transcription2.author == user2


def test_create_blocks() -> None:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "blossom"
version = "1.77.0"
version = "1.78.0"
description = "The site!"
authors = ["Grafeas Group Ltd. <devs@grafeas.org>"]

Expand Down