Skip to content

Commit

Permalink
write migration
Browse files Browse the repository at this point in the history
  • Loading branch information
hjanott committed Nov 19, 2024
1 parent 7b5f725 commit 3f0a7ec
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 2 deletions.
2 changes: 1 addition & 1 deletion global/data/example-data.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"_migration_index": 62,
"_migration_index": 63,
"gender":{
"1":{
"id": 1,
Expand Down
2 changes: 1 addition & 1 deletion global/data/initial-data.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"_migration_index": 62,
"_migration_index": 63,
"gender":{
"1":{
"id": 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from collections import defaultdict

from datastore.migrations import BaseModelMigration
from datastore.writer.core import BaseRequestEvent, RequestUpdateEvent

from openslides_backend.shared.patterns import fqid_from_collection_and_id


class Migration(BaseModelMigration):
"""
This migration adds the user_id "-1" to all existing action_workers.
This is the number usually used for calls using the internal route.
"""

target_migration_index = 63

def migrate_models(self) -> list[BaseRequestEvent] | None:
present_users_per_meeting: dict[int, list[int]] = defaultdict(list)
meetings_per_present_user: dict[int, list[int]] = defaultdict(list)
meetings = self.reader.get_all(
"meeting", ["group_ids", "present_user_ids", "meeting_user_ids"]
)
users = self.reader.get_all(
"user", ["is_present_in_meeting_ids", "meeting_user_ids"]
)

def helper() -> None:
if not (
(meeting_user_ids := user.get("meeting_user_ids", []))
and any(
meeting_user_id in meeting_user_ids
for meeting_user_id in meeting.get("meeting_user_ids", [])
)
):
meetings_per_present_user[user_id].append(meeting_id)
present_users_per_meeting[meeting_id].append(user_id)

for meeting_id, meeting in meetings.items():
for user_id in meeting.get("present_user_ids", []):
user = users.get(user_id, dict())
helper()

for user_id, user in users.items():
for meeting_id in user.get("is_present_in_meeting_ids", []):
helper()

return [
RequestUpdateEvent(
fqid_from_collection_and_id(what_collection, what_id),
{
field: [
id_
for id_ in lookup.get(what_id, dict()).get(field, [])
if id_ not in which_ids
]
},
)
for lookup, cross_lookup, what_collection, field in [
(meetings, present_users_per_meeting, "meeting", "present_user_ids"),
(users, meetings_per_present_user, "user", "is_present_in_meeting_ids"),
]
for what_id, which_ids in cross_lookup.items()
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from typing import Any


def create_data() -> dict[str, dict[str, Any]]:
return {
"meeting/11": {
"id": 1,
"name": "meeting name",
"present_user_ids": [1, 2],
"meeting_user_ids": [3],
},
"meeting/111": {
"id": 1,
"name": "meeting name",
"present_user_ids": [2],
"meeting_user_ids": [4],
},
"user/1": {
"id": 1,
"username": "wrong_user",
"is_present_in_meeting_ids": [11],
},
"user/2": {
"id": 2,
"username": "correct_user",
"is_present_in_meeting_ids": [11, 111],
"meeting_user_ids": [3, 4],
},
"meeting_user/3": {"id": 3, "user_id": 2, "meeting_id": 11},
"meeting_user/4": {"id": 4, "user_id": 2, "meeting_id": 111},
}


def test_migration_both_ways(write, finalize, assert_model):
data = create_data()
for fqid, fields in data.items():
write({"type": "create", "fqid": fqid, "fields": fields})

finalize("0062_unset_presence_of_removed_users")

data["meeting/11"]["present_user_ids"] = [2]
data["user/1"]["is_present_in_meeting_ids"] = []

for fqid, fields in data.items():
assert_model(fqid, fields)


def test_migration_one_way(write, finalize, assert_model):
data = create_data()
data["meeting/11"]["present_user_ids"] = [2]

for fqid, fields in data.items():
write({"type": "create", "fqid": fqid, "fields": fields})

finalize("0062_unset_presence_of_removed_users")

data["user/1"]["is_present_in_meeting_ids"] = []

for fqid, fields in data.items():
assert_model(fqid, fields)


def test_migration_other_way(write, finalize, assert_model):
data = create_data()
data["user/1"]["is_present_in_meeting_ids"] = []

for fqid, fields in data.items():
write({"type": "create", "fqid": fqid, "fields": fields})

finalize("0062_unset_presence_of_removed_users")

data["meeting/11"]["present_user_ids"] = [2]

for fqid, fields in data.items():
assert_model(fqid, fields)

0 comments on commit 3f0a7ec

Please sign in to comment.