diff --git a/global/data/example-data.json b/global/data/example-data.json index f651420f5..2ebd6682a 100644 --- a/global/data/example-data.json +++ b/global/data/example-data.json @@ -1,5 +1,5 @@ { - "_migration_index": 56, + "_migration_index": 57, "organization": { "1": { "id": 1, diff --git a/global/data/initial-data.json b/global/data/initial-data.json index 3da49a1e0..3251be624 100644 --- a/global/data/initial-data.json +++ b/global/data/initial-data.json @@ -1,5 +1,5 @@ { - "_migration_index": 56, + "_migration_index": 57, "organization": { "1": { "id": 1, diff --git a/openslides_backend/migrations/migrations/0056_fix_meeting_mediafile_relations.py b/openslides_backend/migrations/migrations/0056_fix_meeting_mediafile_relations.py new file mode 100644 index 000000000..82c48408a --- /dev/null +++ b/openslides_backend/migrations/migrations/0056_fix_meeting_mediafile_relations.py @@ -0,0 +1,42 @@ +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 fixes some fields that were not rewritten in migration 54 + """ + + target_migration_index = 57 + group_fields = [ + "access_group_ids", + "inherited_access_group_ids", + ] + + def migrate_models(self) -> list[BaseRequestEvent] | None: + groups_to_access_groups: dict[int, dict[str, list[int]]] = { + group["id"]: { + "meeting_mediafile_" + field: [] for field in self.group_fields + } + for group in self.reader.get_all("group", ["id"]).values() + } + for mmediafile in self.reader.get_all( + "meeting_mediafile", ["id", *self.group_fields] + ).values(): + for field in self.group_fields: + for group_id in mmediafile.get(field, []): + groups_to_access_groups[group_id][ + "meeting_mediafile_" + field + ].append(mmediafile["id"]) + return [ + RequestUpdateEvent( + fqid_from_collection_and_id("group", group_id), + { + **{"mediafile_" + field: None for field in self.group_fields}, + **access_groups, + }, + ) + for group_id, access_groups in groups_to_access_groups.items() + ] diff --git a/tests/system/migrations/test_0056_fix_meeting_mediafile_relations.py b/tests/system/migrations/test_0056_fix_meeting_mediafile_relations.py new file mode 100644 index 000000000..82ea0e78e --- /dev/null +++ b/tests/system/migrations/test_0056_fix_meeting_mediafile_relations.py @@ -0,0 +1,173 @@ +def test_migration(write, finalize, assert_model): + write( + { + "type": "create", + "fqid": "meeting/1", + "fields": {"id": 1, "meeting_mediafile_ids": [1, 2], "group_ids": [1]}, + }, + { + "type": "create", + "fqid": "meeting/2", + "fields": { + "id": 2, + "meeting_mediafile_ids": [3, 4, 6], + "group_ids": [2, 3], + }, + }, + { + "type": "create", + "fqid": "mediafile/1", + "fields": {"id": 1, "meeting_mediafile_ids": [1, 3]}, + }, + { + "type": "create", + "fqid": "mediafile/2", + "fields": {"id": 2, "meeting_mediafile_ids": [2]}, + }, + { + "type": "create", + "fqid": "mediafile/5", + "fields": {"id": 5, "child_ids": [6], "meeting_mediafile_ids": [4]}, + }, + { + "type": "create", + "fqid": "mediafile/6", + "fields": {"id": 6, "parent_id": 5, "meeting_mediafile_ids": [6]}, + }, + { + "type": "create", + "fqid": "meeting_mediafile/1", + "fields": { + "id": 1, + "mediafile_id": 1, + "meeting_id": 1, + "access_group_ids": [1], + "inherited_access_group_ids": [1], + "is_public": False, + }, + }, + { + "type": "create", + "fqid": "meeting_mediafile/2", + "fields": { + "id": 2, + "mediafile_id": 2, + "meeting_id": 1, + "access_group_ids": [1], + "inherited_access_group_ids": [1], + "is_public": False, + }, + }, + { + "type": "create", + "fqid": "meeting_mediafile/3", + "fields": { + "id": 3, + "mediafile_id": 1, + "meeting_id": 2, + "access_group_ids": [2, 3], + "inherited_access_group_ids": [2, 3], + "is_public": False, + }, + }, + { + "type": "create", + "fqid": "meeting_mediafile/4", + "fields": { + "id": 4, + "mediafile_id": 5, + "meeting_id": 2, + "access_group_ids": [3], + "inherited_access_group_ids": [3], + "is_public": False, + }, + }, + { + "type": "create", + "fqid": "meeting_mediafile/5", + "fields": { + "id": 5, + "mediafile_id": 2, + "meeting_id": 2, + "access_group_ids": [4], + "inherited_access_group_ids": [], + "is_public": False, + }, + }, + { + "type": "create", + "fqid": "meeting_mediafile/6", + "fields": { + "id": 6, + "mediafile_id": 6, + "meeting_id": 2, + "access_group_ids": [2], + "inherited_access_group_ids": [], + "is_public": False, + }, + }, + { + "type": "create", + "fqid": "group/1", + "fields": { + "id": 1, + "meeting_id": 1, + "mediafile_access_group_ids": [1], + "mediafile_inherited_access_group_ids": [1], + "meeting_mediafile_access_group_ids": [2], + "meeting_mediafile_inherited_access_group_ids": [2], + }, + }, + { + "type": "create", + "fqid": "group/2", + "fields": { + "id": 2, + "meeting_id": 2, + "meeting_mediafile_access_group_ids": [3, 6], + "meeting_mediafile_inherited_access_group_ids": [3], + }, + }, + { + "type": "create", + "fqid": "group/3", + "fields": { + "id": 3, + "meeting_id": 2, + "mediafile_access_group_ids": [3, 4], + "mediafile_inherited_access_group_ids": [3, 4], + }, + }, + { + "type": "create", + "fqid": "group/4", + "fields": { + "id": 4, + "meeting_id": 2, + "mediafile_id": 2, + "mediafile_access_group_ids": [5], + "mediafile_inherited_access_group_ids": [5], + }, + }, + ) + write( + {"type": "delete", "fqid": "meeting_mediafile/5", "fields": {}}, + {"type": "delete", "fqid": "group/4", "fields": {}}, + ) + + finalize("0056_fix_meeting_mediafile_relations") + + for id_, (meeting_id, access_groups, inherited_access_groups) in { + 1: (1, [1, 2], [1, 2]), + 2: (2, [3, 6], [3]), + 3: (2, [3, 4], [3, 4]), + }.items(): + assert_model( + f"group/{id_}", + { + "id": id_, + "meeting_id": meeting_id, + "meeting_mediafile_access_group_ids": access_groups, + "meeting_mediafile_inherited_access_group_ids": inherited_access_groups, + }, + )