From b3ca6fd388a46be636ac4c0e0809c5286c4de9bf Mon Sep 17 00:00:00 2001 From: vkrasnovyd Date: Wed, 2 Dec 2026 18:41:19 +0100 Subject: [PATCH 1/4] Add indexes for 1-n --- .../mig_0103_update_indexes/indexes.py | 353 +++++++++++++++++- .../mig_0103_update_indexes/migration.py | 186 ++++++++- 2 files changed, 518 insertions(+), 21 deletions(-) diff --git a/openslides_backend/migrations/migrations/mig_0103_update_indexes/indexes.py b/openslides_backend/migrations/migrations/mig_0103_update_indexes/indexes.py index 245a4234a5..562c5b7aa6 100644 --- a/openslides_backend/migrations/migrations/mig_0103_update_indexes/indexes.py +++ b/openslides_backend/migrations/migrations/mig_0103_update_indexes/indexes.py @@ -29,19 +29,37 @@ "contype": "u", "fields": "content_object_id_topic_id", }, + "idx_agenda_item_t_parent_id": {"contype": None, "field": "parent_id"}, + "idx_agenda_item_t_meeting_id": {"contype": None, "field": "meeting_id"}, }, "assignment_t": { "unique_assignment_sequential_number_meeting_id": { "contype": "u", "fields": "sequential_number, meeting_id", - } + }, + "idx_assignment_t_meeting_id": {"contype": None, "field": "meeting_id"}, + }, + "assignment_candidate_t": { + "idx_assignment_candidate_t_assignment_id": { + "contype": None, + "field": "assignment_id", + }, + "idx_assignment_candidate_t_meeting_user_id": { + "contype": None, + "field": "meeting_user_id", + }, + "idx_assignment_candidate_t_meeting_id": { + "contype": None, + "field": "meeting_id", + }, }, "chat_group_t": { "unique_chat_group_meeting_id_name": { "contype": "u", "fields": "meeting_id, name", "strict": False, - } + }, + "idx_chat_group_t_meeting_id": {"contype": None, "field": "meeting_id"}, }, "nm_chat_group_read_group_ids_group_t": { "pk_nm_chat_group_read_group_ids_group_t": { @@ -71,12 +89,25 @@ "field": "group_id", }, }, + "chat_message_t": { + "idx_chat_message_t_meeting_user_id": { + "contype": None, + "field": "meeting_user_id", + }, + "idx_chat_message_t_chat_group_id": {"contype": None, "field": "chat_group_id"}, + "idx_chat_message_t_meeting_id": {"contype": None, "field": "meeting_id"}, + }, "committee_t": { "unique_committee_external_id": {"contype": "u", "fields": "external_id"}, "unique_committee_default_meeting_id": { "contype": "u", "fields": "default_meeting_id", }, + "idx_committee_t_parent_id": {"contype": None, "field": "parent_id"}, + "idx_committee_t_organization_id": { + "contype": None, + "field": "organization_id", + }, }, "nm_committee_manager_ids_user_t": { "pk_nm_committee_manager_ids_user_t": { @@ -120,13 +151,33 @@ "field": "forward_to_committee_id", }, }, - "gender_t": {"unique_gender_name": {"contype": "u", "fields": "name"}}, + "gender_t": { + "unique_gender_name": {"contype": "u", "fields": "name"}, + "idx_gender_t_organization_id": {"contype": None, "field": "organization_id"}, + }, "group_t": { "unique_group_meeting_id_external_id": { "contype": "u", "fields": "meeting_id, external_id", "strict": False, - } + }, + "idx_group_t_used_as_motion_poll_default_id": { + "contype": None, + "field": "used_as_motion_poll_default_id", + }, + "idx_group_t_used_as_assignment_poll_default_id": { + "contype": None, + "field": "used_as_assignment_poll_default_id", + }, + "idx_group_t_used_as_topic_poll_default_id": { + "contype": None, + "field": "used_as_topic_poll_default_id", + }, + "idx_group_t_used_as_poll_default_id": { + "contype": None, + "field": "used_as_poll_default_id", + }, + "idx_group_t_meeting_id": {"contype": None, "field": "meeting_id"}, }, "nm_group_meeting_user_ids_meeting_user_t": { "pk_nm_group_meeting_user_ids_meeting_user_t": { @@ -203,6 +254,13 @@ "idx_nm_group_poll_ids_poll_t_group_id": {"contype": None, "field": "group_id"}, "idx_nm_group_poll_ids_poll_t_poll_id": {"contype": None, "field": "poll_id"}, }, + "history_entry_t": { + "idx_history_entry_t_position_id": {"contype": None, "field": "position_id"}, + "idx_history_entry_t_meeting_id": {"contype": None, "field": "meeting_id"}, + }, + "history_position_t": { + "idx_history_position_t_user_id": {"contype": None, "field": "user_id"} + }, "list_of_speakers_t": { "unique_list_of_speakers_sequential_number_meeting_id": { "contype": "u", @@ -228,6 +286,7 @@ "contype": "u", "fields": "content_object_id_meeting_mediafile_id", }, + "idx_list_of_speakers_t_meeting_id": {"contype": None, "field": "meeting_id"}, }, "mediafile_t": { "unique_mediafile_title_parent_id_owner_id": { @@ -236,9 +295,26 @@ "strict": True, }, "unique_mediafile_token": {"contype": "u", "fields": "token"}, + "idx_mediafile_t_published_to_meetings_in_organization_id": { + "contype": None, + "field": "published_to_meetings_in_organization_id", + }, + "idx_mediafile_t_parent_id": {"contype": None, "field": "parent_id"}, }, "meeting_t": { "unique_meeting_external_id": {"contype": "u", "fields": "external_id"}, + "idx_meeting_t_is_active_in_organization_id": { + "contype": None, + "field": "is_active_in_organization_id", + }, + "idx_meeting_t_is_archived_in_organization_id": { + "contype": None, + "field": "is_archived_in_organization_id", + }, + "idx_meeting_t_template_for_organization_id": { + "contype": None, + "field": "template_for_organization_id", + }, "unique_meeting_motions_default_workflow_id": { "contype": "u", "fields": "motions_default_workflow_id", @@ -302,6 +378,7 @@ "contype": "u", "fields": "font_projector_h2_id", }, + "idx_meeting_t_committee_id": {"contype": None, "field": "committee_id"}, "unique_meeting_reference_projector_id": { "contype": "u", "fields": "reference_projector_id", @@ -343,7 +420,12 @@ "contype": "u", "fields": "mediafile_id, meeting_id", "strict": False, - } + }, + "idx_meeting_mediafile_t_mediafile_id": { + "contype": None, + "field": "mediafile_id", + }, + "idx_meeting_mediafile_t_meeting_id": {"contype": None, "field": "meeting_id"}, }, "gm_meeting_mediafile_attachment_ids_t": { "unique_meeting_mediafile_id_attachment_id": { @@ -376,7 +458,13 @@ "contype": "u", "fields": "meeting_id, user_id", "strict": False, - } + }, + "idx_meeting_user_t_user_id": {"contype": None, "field": "user_id"}, + "idx_meeting_user_t_meeting_id": {"contype": None, "field": "meeting_id"}, + "idx_meeting_user_t_vote_delegated_to_id": { + "contype": None, + "field": "vote_delegated_to_id", + }, }, "nm_meeting_user_structure_level_ids_structure_level_t": { "pk_nm_meeting_user_structure_level_ids_structure_level_t": { @@ -402,6 +490,21 @@ "contype": "u", "fields": "sequential_number, meeting_id", }, + "idx_motion_t_lead_motion_id": {"contype": None, "field": "lead_motion_id"}, + "idx_motion_t_sort_parent_id": {"contype": None, "field": "sort_parent_id"}, + "idx_motion_t_origin_id": {"contype": None, "field": "origin_id"}, + "idx_motion_t_origin_meeting_id": { + "contype": None, + "field": "origin_meeting_id", + }, + "idx_motion_t_state_id": {"contype": None, "field": "state_id"}, + "idx_motion_t_recommendation_id": { + "contype": None, + "field": "recommendation_id", + }, + "idx_motion_t_category_id": {"contype": None, "field": "category_id"}, + "idx_motion_t_block_id": {"contype": None, "field": "block_id"}, + "idx_motion_t_meeting_id": {"contype": None, "field": "meeting_id"}, }, "nm_motion_all_derived_motion_ids_motion_t": { "pk_nm_motion_all_derived_motion_ids_motion_t": { @@ -471,40 +574,72 @@ "unique_motion_block_sequential_number_meeting_id": { "contype": "u", "fields": "sequential_number, meeting_id", - } + }, + "idx_motion_block_t_meeting_id": {"contype": None, "field": "meeting_id"}, }, "motion_category_t": { "unique_motion_category_sequential_number_meeting_id": { "contype": "u", "fields": "sequential_number, meeting_id", - } + }, + "idx_motion_category_t_parent_id": {"contype": None, "field": "parent_id"}, + "idx_motion_category_t_meeting_id": {"contype": None, "field": "meeting_id"}, + }, + "motion_change_recommendation_t": { + "idx_motion_change_recommendation_t_motion_id": { + "contype": None, + "field": "motion_id", + }, + "idx_motion_change_recommendation_t_meeting_id": { + "contype": None, + "field": "meeting_id", + }, }, "motion_comment_t": { "unique_motion_comment_motion_id_section_id": { "contype": "u", "fields": "motion_id, section_id", "strict": False, - } + }, + "idx_motion_comment_t_motion_id": {"contype": None, "field": "motion_id"}, + "idx_motion_comment_t_section_id": {"contype": None, "field": "section_id"}, + "idx_motion_comment_t_meeting_id": {"contype": None, "field": "meeting_id"}, }, "motion_comment_section_t": { "unique_motion_comment_section_sequential_number_meeting_id": { "contype": "u", "fields": "sequential_number, meeting_id", - } + }, + "idx_motion_comment_section_t_meeting_id": { + "contype": None, + "field": "meeting_id", + }, }, "motion_editor_t": { "unique_motion_editor_meeting_user_id_motion_id": { "contype": "u", "fields": "meeting_user_id, motion_id", "strict": False, - } + }, + "idx_motion_editor_t_meeting_user_id": { + "contype": None, + "field": "meeting_user_id", + }, + "idx_motion_editor_t_motion_id": {"contype": None, "field": "motion_id"}, + "idx_motion_editor_t_meeting_id": {"contype": None, "field": "meeting_id"}, }, "motion_state_t": { "unique_motion_state_name_workflow_id": { "contype": "u", "fields": "name, workflow_id", "strict": False, - } + }, + "idx_motion_state_t_submitter_withdraw_state_id": { + "contype": None, + "field": "submitter_withdraw_state_id", + }, + "idx_motion_state_t_workflow_id": {"contype": None, "field": "workflow_id"}, + "idx_motion_state_t_meeting_id": {"contype": None, "field": "meeting_id"}, }, "nm_motion_state_next_state_ids_motion_state_t": { "pk_nm_motion_state_next_state_ids_motion_state_t": { @@ -525,14 +660,26 @@ "contype": "u", "fields": "meeting_user_id, motion_id", "strict": False, - } + }, + "idx_motion_submitter_t_meeting_user_id": { + "contype": None, + "field": "meeting_user_id", + }, + "idx_motion_submitter_t_motion_id": {"contype": None, "field": "motion_id"}, + "idx_motion_submitter_t_meeting_id": {"contype": None, "field": "meeting_id"}, }, "motion_supporter_t": { "unique_motion_supporter_meeting_user_id_motion_id": { "contype": "u", "fields": "meeting_user_id, motion_id", "strict": False, - } + }, + "idx_motion_supporter_t_meeting_user_id": { + "contype": None, + "field": "meeting_user_id", + }, + "idx_motion_supporter_t_motion_id": {"contype": None, "field": "motion_id"}, + "idx_motion_supporter_t_meeting_id": {"contype": None, "field": "meeting_id"}, }, "motion_workflow_t": { "unique_motion_workflow_sequential_number_meeting_id": { @@ -543,13 +690,26 @@ "contype": "u", "fields": "first_state_id", }, + "idx_motion_workflow_t_meeting_id": {"contype": None, "field": "meeting_id"}, }, "motion_working_group_speaker_t": { "unique_motion_working_group_speaker_meeting_user_id_motion_id": { "contype": "u", "fields": "meeting_user_id, motion_id", "strict": False, - } + }, + "idx_motion_working_group_speaker_t_meeting_user_id": { + "contype": None, + "field": "meeting_user_id", + }, + "idx_motion_working_group_speaker_t_motion_id": { + "contype": None, + "field": "motion_id", + }, + "idx_motion_working_group_speaker_t_meeting_id": { + "contype": None, + "field": "meeting_id", + }, }, "option_t": { "unique_option_content_object_id_poll_id": { @@ -557,10 +717,12 @@ "fields": "content_object_id, poll_id", "strict": False, }, + "idx_option_t_poll_id": {"contype": None, "field": "poll_id"}, "unique_option_content_object_id_poll_candidate_list_id": { "contype": "u", "fields": "content_object_id_poll_candidate_list_id", }, + "idx_option_t_meeting_id": {"contype": None, "field": "meeting_id"}, }, "organization_t": { "unique_organization_theme_id": {"contype": "u", "fields": "theme_id"} @@ -587,11 +749,28 @@ "field": "tagged_id_meeting_id", }, }, + "organization_tag_t": { + "idx_organization_tag_t_organization_id": { + "contype": None, + "field": "organization_id", + } + }, "personal_note_t": { "unique_personal_note_meeting_user_id_content_object_id": { "contype": "u", "fields": "meeting_user_id, content_object_id", "strict": False, + }, + "idx_personal_note_t_meeting_user_id": { + "contype": None, + "field": "meeting_user_id", + }, + "idx_personal_note_t_meeting_id": {"contype": None, "field": "meeting_id"}, + }, + "point_of_order_category_t": { + "idx_point_of_order_category_t_meeting_id": { + "contype": None, + "field": "meeting_id", } }, "poll_t": { @@ -600,38 +779,158 @@ "fields": "sequential_number, meeting_id", }, "unique_poll_global_option_id": {"contype": "u", "fields": "global_option_id"}, + "idx_poll_t_meeting_id": {"contype": None, "field": "meeting_id"}, }, "nm_poll_voted_ids_user_t": { "pk_nm_poll_voted_ids_user_t": {"contype": "p", "fields": "poll_id, user_id"}, "idx_nm_poll_voted_ids_user_t_poll_id": {"contype": None, "field": "poll_id"}, "idx_nm_poll_voted_ids_user_t_user_id": {"contype": None, "field": "user_id"}, }, + "poll_candidate_t": { + "idx_poll_candidate_t_poll_candidate_list_id": { + "contype": None, + "field": "poll_candidate_list_id", + }, + "idx_poll_candidate_t_user_id": {"contype": None, "field": "user_id"}, + "idx_poll_candidate_t_meeting_id": {"contype": None, "field": "meeting_id"}, + }, + "poll_candidate_list_t": { + "idx_poll_candidate_list_t_meeting_id": {"contype": None, "field": "meeting_id"} + }, + "projection_t": { + "idx_projection_t_current_projector_id": { + "contype": None, + "field": "current_projector_id", + }, + "idx_projection_t_preview_projector_id": { + "contype": None, + "field": "preview_projector_id", + }, + "idx_projection_t_history_projector_id": { + "contype": None, + "field": "history_projector_id", + }, + "idx_projection_t_meeting_id": {"contype": None, "field": "meeting_id"}, + }, "projector_t": { "unique_projector_sequential_number_meeting_id": { "contype": "u", "fields": "sequential_number, meeting_id", - } + }, + "idx_projector_t_used_as_default_projector_for_agenda_ite41b5bd9": { + "contype": None, + "field": "used_as_default_projector_for_agenda_item_list_in_meeting_id", + }, + "idx_projector_t_used_as_default_projector_for_topic_in_mab361e8": { + "contype": None, + "field": "used_as_default_projector_for_topic_in_meeting_id", + }, + "idx_projector_t_used_as_default_projector_for_list_of_spb099704": { + "contype": None, + "field": "used_as_default_projector_for_list_of_speakers_in_meeting_id", + }, + "idx_projector_t_used_as_default_projector_for_current_locdd5b27": { + "contype": None, + "field": "used_as_default_projector_for_current_los_in_meeting_id", + }, + "idx_projector_t_used_as_default_projector_for_motion_in_f66691f": { + "contype": None, + "field": "used_as_default_projector_for_motion_in_meeting_id", + }, + "idx_projector_t_used_as_default_projector_for_amendment_2235e12": { + "contype": None, + "field": "used_as_default_projector_for_amendment_in_meeting_id", + }, + "idx_projector_t_used_as_default_projector_for_motion_blo7b0c3c8": { + "contype": None, + "field": "used_as_default_projector_for_motion_block_in_meeting_id", + }, + "idx_projector_t_used_as_default_projector_for_assignment78622ce": { + "contype": None, + "field": "used_as_default_projector_for_assignment_in_meeting_id", + }, + "idx_projector_t_used_as_default_projector_for_mediafile_1c31a99": { + "contype": None, + "field": "used_as_default_projector_for_mediafile_in_meeting_id", + }, + "idx_projector_t_used_as_default_projector_for_message_in339958e": { + "contype": None, + "field": "used_as_default_projector_for_message_in_meeting_id", + }, + "idx_projector_t_used_as_default_projector_for_countdown_07665b4": { + "contype": None, + "field": "used_as_default_projector_for_countdown_in_meeting_id", + }, + "idx_projector_t_used_as_default_projector_for_assignment5a08584": { + "contype": None, + "field": "used_as_default_projector_for_assignment_poll_in_meeting_id", + }, + "idx_projector_t_used_as_default_projector_for_motion_pol0605aa8": { + "contype": None, + "field": "used_as_default_projector_for_motion_poll_in_meeting_id", + }, + "idx_projector_t_used_as_default_projector_for_poll_in_me446bdba": { + "contype": None, + "field": "used_as_default_projector_for_poll_in_meeting_id", + }, + "idx_projector_t_meeting_id": {"contype": None, "field": "meeting_id"}, }, "projector_countdown_t": { "unique_projector_countdown_meeting_id_title": { "contype": "u", "fields": "meeting_id, title", "strict": False, - } + }, + "idx_projector_countdown_t_meeting_id": { + "contype": None, + "field": "meeting_id", + }, + }, + "projector_message_t": { + "idx_projector_message_t_meeting_id": {"contype": None, "field": "meeting_id"} + }, + "speaker_t": { + "idx_speaker_t_list_of_speakers_id": { + "contype": None, + "field": "list_of_speakers_id", + }, + "idx_speaker_t_structure_level_list_of_speakers_id": { + "contype": None, + "field": "structure_level_list_of_speakers_id", + }, + "idx_speaker_t_meeting_user_id": {"contype": None, "field": "meeting_user_id"}, + "idx_speaker_t_point_of_order_category_id": { + "contype": None, + "field": "point_of_order_category_id", + }, + "idx_speaker_t_meeting_id": {"contype": None, "field": "meeting_id"}, }, "structure_level_t": { "unique_structure_level_meeting_id_name": { "contype": "u", "fields": "meeting_id, name", "strict": False, - } + }, + "idx_structure_level_t_meeting_id": {"contype": None, "field": "meeting_id"}, }, "structure_level_list_of_speakers_t": { "unique_structure_level_list_of_speakers_meeting_id_struce047abe": { "contype": "u", "fields": "meeting_id, structure_level_id, list_of_speakers_id", "strict": False, - } + }, + "idx_structure_level_list_of_speakers_t_structure_level_id": { + "contype": None, + "field": "structure_level_id", + }, + "idx_structure_level_list_of_speakers_t_list_of_speakers_id": { + "contype": None, + "field": "list_of_speakers_id", + }, + "idx_structure_level_list_of_speakers_t_meeting_id": { + "contype": None, + "field": "meeting_id", + }, }, "gm_tag_tagged_ids_t": { "unique_tag_id_tagged_id": {"contype": "u", "fields": "tag_id, tagged_id"}, @@ -650,15 +949,29 @@ "field": "tagged_id_motion_id", }, }, + "tag_t": {"idx_tag_t_meeting_id": {"contype": None, "field": "meeting_id"}}, + "theme_t": { + "idx_theme_t_organization_id": {"contype": None, "field": "organization_id"} + }, "topic_t": { "unique_topic_sequential_number_meeting_id": { "contype": "u", "fields": "sequential_number, meeting_id", - } + }, + "idx_topic_t_meeting_id": {"contype": None, "field": "meeting_id"}, }, "user_t": { "unique_user_username": {"contype": "u", "fields": "username"}, "unique_user_member_number": {"contype": "u", "fields": "member_number"}, "unique_user_saml_id": {"contype": "u", "fields": "saml_id"}, + "idx_user_t_gender_id": {"contype": None, "field": "gender_id"}, + "idx_user_t_home_committee_id": {"contype": None, "field": "home_committee_id"}, + "idx_user_t_organization_id": {"contype": None, "field": "organization_id"}, + }, + "vote_t": { + "idx_vote_t_option_id": {"contype": None, "field": "option_id"}, + "idx_vote_t_user_id": {"contype": None, "field": "user_id"}, + "idx_vote_t_delegated_user_id": {"contype": None, "field": "delegated_user_id"}, + "idx_vote_t_meeting_id": {"contype": None, "field": "meeting_id"}, }, } diff --git a/openslides_backend/migrations/migrations/mig_0103_update_indexes/migration.py b/openslides_backend/migrations/migrations/mig_0103_update_indexes/migration.py index 6e8634c423..c6cb930e14 100644 --- a/openslides_backend/migrations/migrations/mig_0103_update_indexes/migration.py +++ b/openslides_backend/migrations/migrations/mig_0103_update_indexes/migration.py @@ -4,9 +4,16 @@ from psycopg import Cursor from psycopg.rows import DictRow +from meta.dev.src.generate_sql_schema import Helper +from meta.dev.src.helper_get_names import ( + FieldSqlErrorType, + HelperGetNames, + InternalHelper, + TableFieldType, +) from openslides_backend.migrations.base import BaseMigration -from .indexes import schema_indexes +# from .indexes import schema_indexes class Migration(BaseMigration): @@ -96,6 +103,183 @@ def data_definition(curs: Cursor[DictRow]) -> None: database_indexes = { row["tablename"]: row["indexnames"] for row in curs.fetchall() } + # Calculate indexes defined in schema + schema_indexes: dict[str, dict[str, dict[str, Any]]] = defaultdict(dict) + for collection, collection_data in InternalHelper.MODELS.items(): + if collection == "_meta": + continue + table_name = HelperGetNames.get_table_name(collection) + for unique_together_key in ["unique_together", "unique_together_strict"]: + if fields_groups := collection_data.get(unique_together_key): + for fields in fields_groups: + constraint_name = HelperGetNames.get_unique_constraint_name( + collection, Helper.split_unique_together_fields(fields) + ) + schema_indexes[table_name][constraint_name] = { + "contype": "u", + "fields": fields, + "strict": "strict" in unique_together_key, + } + for field_name, field_def in collection_data["fields"].items(): + if field_name == "id": + continue + if "relation" in (type_ := field_def["type"]): + own_table_field = TableFieldType(collection, field_name, field_def) + if "generic" not in type_: + foreign_table_field: TableFieldType = ( + TableFieldType.get_definitions_from_foreign( + field_def.get("to"), field_def.get("reference") + ) + ) + state, primary, _, _ = ( + InternalHelper.check_relation_definitions( + own_table_field, [foreign_table_field] + ) + ) + if type_ == "relation": + if ( + foreign_table_field.field_def["type"] == "relation" + and state == FieldSqlErrorType.FIELD + ): + constraint_name = ( + HelperGetNames.get_unique_constraint_name( + collection, [field_name] + ) + ) + schema_indexes[table_name][constraint_name] = { + "contype": "u", + "fields": field_name, + } + elif ( + foreign_table_field.field_def["type"] == "relation-list" + ): + schema_indexes[table_name][ + HelperGetNames.get_index_name( + table_name, field_name + ) + ] = { + "contype": None, + "field": field_name, + } + elif ( + "sql" not in field_def + and foreign_table_field.field_def["type"] == "relation-list" + and primary + and state != FieldSqlErrorType.ERROR + ): + nm_table_name, field1, field2 = ( + Helper.get_nm_table_name_and_fields( + own_table_field, foreign_table_field + ) + ) + nm_table_name = HelperGetNames.get_table_name(nm_table_name) + schema_indexes[nm_table_name] = { + HelperGetNames.get_nm_pk_constraint_name( + nm_table_name + ): { + "contype": "p", + "fields": f"{field1}, {field2}", + }, + HelperGetNames.get_index_name(nm_table_name, field1): { + "contype": None, + "field": field1, + }, + HelperGetNames.get_index_name(nm_table_name, field2): { + "contype": None, + "field": field2, + }, + } + else: + foreign_table_fields: list[TableFieldType] = ( + InternalHelper.get_definitions_from_foreign_list( + field_def.get("to"), field_def.get("reference") + ) + ) + if type_ == "generic-relation-list": + ( + gm_table_name, + own_table_name_with_ref_column, + intermediate_field_to_foreign_table_field, + own_table_name, + own_table_column, + foreign_table_ref_lines, + indices_lines, + ) = Helper.get_gm_table_config( + own_table_field, foreign_table_fields + ) + index_1 = HelperGetNames.get_index_name( + gm_table_name, own_table_name_with_ref_column + ) + index_2 = HelperGetNames.get_index_name( + gm_table_name, own_table_column + ) + unique_constraint_name = ( + HelperGetNames.get_generic_unique_constraint_name( + own_table_name_with_ref_column, own_table_column + ) + ) + schema_indexes[gm_table_name] = { + unique_constraint_name: { + "contype": "u", + "fields": f"{own_table_name_with_ref_column}, {own_table_column}", + }, + index_1: { + "contype": None, + "field": own_table_name_with_ref_column, + }, + index_2: { + "contype": None, + "field": own_table_column, + }, + } + for foreign_table_field in foreign_table_fields: + gm_content_field = HelperGetNames.get_gm_content_field( + own_table_column, foreign_table_field.table + ) + schema_indexes[gm_table_name][ + HelperGetNames.get_index_name( + gm_table_name, gm_content_field + ) + ] = {"contype": None, "field": gm_content_field} + else: + for foreign_table_field in foreign_table_fields: + if foreign_table_field.field_def["type"] == "relation": + generic_plain_field_name = ( + HelperGetNames.get_generic_plain_field_name( + own_table_field.column, + foreign_table_field.table, + foreign_table_field.ref_column, + ) + ) + schema_indexes[table_name][ + HelperGetNames.get_unique_constraint_name( + collection, [generic_plain_field_name] + ) + ] = { + "contype": "u", + "fields": generic_plain_field_name, + } + else: + if field_def.get("unique", False): + schema_indexes[table_name][ + HelperGetNames.get_unique_constraint_name( + collection, [field_name] + ) + ] = {"contype": "u", "fields": field_name} + if sequence_scope := field_def.get("sequence_scope"): + schema_indexes[table_name][ + HelperGetNames.get_unique_constraint_name( + collection, [field_name, sequence_scope] + ) + ] = { + "contype": "u", + "fields": f"{field_name}, {sequence_scope}", + } + with open( + "openslides_backend/migrations/migrations/mig_0103_update_indexes/indexes.py", + "w", + ) as dest: + dest.write(str(dict(schema_indexes))) # Calculate diff drop: dict[str, dict[str, str]] = defaultdict(dict) From e301f1e0f9b25935315c2e5128c7e36b2ad03662 Mon Sep 17 00:00:00 2001 From: vkrasnovyd Date: Wed, 2 Dec 2026 18:41:45 +0100 Subject: [PATCH 2/4] Add indexes for 1g-n --- .../mig_0103_update_indexes/indexes.py | 88 +++++++++++++++++++ .../mig_0103_update_indexes/migration.py | 23 +++-- 2 files changed, 104 insertions(+), 7 deletions(-) diff --git a/openslides_backend/migrations/migrations/mig_0103_update_indexes/indexes.py b/openslides_backend/migrations/migrations/mig_0103_update_indexes/indexes.py index 562c5b7aa6..34caa5334c 100644 --- a/openslides_backend/migrations/migrations/mig_0103_update_indexes/indexes.py +++ b/openslides_backend/migrations/migrations/mig_0103_update_indexes/indexes.py @@ -255,6 +255,18 @@ "idx_nm_group_poll_ids_poll_t_poll_id": {"contype": None, "field": "poll_id"}, }, "history_entry_t": { + "idx_history_entry_t_model_id_user_id": { + "contype": None, + "field": "model_id_user_id", + }, + "idx_history_entry_t_model_id_motion_id": { + "contype": None, + "field": "model_id_motion_id", + }, + "idx_history_entry_t_model_id_assignment_id": { + "contype": None, + "field": "model_id_assignment_id", + }, "idx_history_entry_t_position_id": {"contype": None, "field": "position_id"}, "idx_history_entry_t_meeting_id": {"contype": None, "field": "meeting_id"}, }, @@ -300,6 +312,14 @@ "field": "published_to_meetings_in_organization_id", }, "idx_mediafile_t_parent_id": {"contype": None, "field": "parent_id"}, + "idx_mediafile_t_owner_id_meeting_id": { + "contype": None, + "field": "owner_id_meeting_id", + }, + "idx_mediafile_t_owner_id_organization_id": { + "contype": None, + "field": "owner_id_organization_id", + }, }, "meeting_t": { "unique_meeting_external_id": {"contype": "u", "fields": "external_id"}, @@ -718,6 +738,14 @@ "strict": False, }, "idx_option_t_poll_id": {"contype": None, "field": "poll_id"}, + "idx_option_t_content_object_id_motion_id": { + "contype": None, + "field": "content_object_id_motion_id", + }, + "idx_option_t_content_object_id_user_id": { + "contype": None, + "field": "content_object_id_user_id", + }, "unique_option_content_object_id_poll_candidate_list_id": { "contype": "u", "fields": "content_object_id_poll_candidate_list_id", @@ -765,6 +793,10 @@ "contype": None, "field": "meeting_user_id", }, + "idx_personal_note_t_content_object_id_motion_id": { + "contype": None, + "field": "content_object_id_motion_id", + }, "idx_personal_note_t_meeting_id": {"contype": None, "field": "meeting_id"}, }, "point_of_order_category_t": { @@ -778,6 +810,18 @@ "contype": "u", "fields": "sequential_number, meeting_id", }, + "idx_poll_t_content_object_id_motion_id": { + "contype": None, + "field": "content_object_id_motion_id", + }, + "idx_poll_t_content_object_id_assignment_id": { + "contype": None, + "field": "content_object_id_assignment_id", + }, + "idx_poll_t_content_object_id_topic_id": { + "contype": None, + "field": "content_object_id_topic_id", + }, "unique_poll_global_option_id": {"contype": "u", "fields": "global_option_id"}, "idx_poll_t_meeting_id": {"contype": None, "field": "meeting_id"}, }, @@ -810,6 +854,50 @@ "contype": None, "field": "history_projector_id", }, + "idx_projection_t_content_object_id_meeting_id": { + "contype": None, + "field": "content_object_id_meeting_id", + }, + "idx_projection_t_content_object_id_motion_id": { + "contype": None, + "field": "content_object_id_motion_id", + }, + "idx_projection_t_content_object_id_meeting_mediafile_id": { + "contype": None, + "field": "content_object_id_meeting_mediafile_id", + }, + "idx_projection_t_content_object_id_list_of_speakers_id": { + "contype": None, + "field": "content_object_id_list_of_speakers_id", + }, + "idx_projection_t_content_object_id_motion_block_id": { + "contype": None, + "field": "content_object_id_motion_block_id", + }, + "idx_projection_t_content_object_id_assignment_id": { + "contype": None, + "field": "content_object_id_assignment_id", + }, + "idx_projection_t_content_object_id_agenda_item_id": { + "contype": None, + "field": "content_object_id_agenda_item_id", + }, + "idx_projection_t_content_object_id_topic_id": { + "contype": None, + "field": "content_object_id_topic_id", + }, + "idx_projection_t_content_object_id_poll_id": { + "contype": None, + "field": "content_object_id_poll_id", + }, + "idx_projection_t_content_object_id_projector_message_id": { + "contype": None, + "field": "content_object_id_projector_message_id", + }, + "idx_projection_t_content_object_id_projector_countdown_id": { + "contype": None, + "field": "content_object_id_projector_countdown_id", + }, "idx_projection_t_meeting_id": {"contype": None, "field": "meeting_id"}, }, "projector_t": { diff --git a/openslides_backend/migrations/migrations/mig_0103_update_indexes/migration.py b/openslides_backend/migrations/migrations/mig_0103_update_indexes/migration.py index c6cb930e14..81a5d56344 100644 --- a/openslides_backend/migrations/migrations/mig_0103_update_indexes/migration.py +++ b/openslides_backend/migrations/migrations/mig_0103_update_indexes/migration.py @@ -243,14 +243,14 @@ def data_definition(curs: Cursor[DictRow]) -> None: ] = {"contype": None, "field": gm_content_field} else: for foreign_table_field in foreign_table_fields: - if foreign_table_field.field_def["type"] == "relation": - generic_plain_field_name = ( - HelperGetNames.get_generic_plain_field_name( - own_table_field.column, - foreign_table_field.table, - foreign_table_field.ref_column, - ) + generic_plain_field_name = ( + HelperGetNames.get_generic_plain_field_name( + own_table_field.column, + foreign_table_field.table, + foreign_table_field.ref_column, ) + ) + if foreign_table_field.field_def["type"] == "relation": schema_indexes[table_name][ HelperGetNames.get_unique_constraint_name( collection, [generic_plain_field_name] @@ -259,6 +259,15 @@ def data_definition(curs: Cursor[DictRow]) -> None: "contype": "u", "fields": generic_plain_field_name, } + else: + schema_indexes[table_name][ + HelperGetNames.get_index_name( + table_name, generic_plain_field_name + ) + ] = { + "contype": None, + "field": generic_plain_field_name, + } else: if field_def.get("unique", False): schema_indexes[table_name][ From 18ebdef8f8c0689b456e24d74a409c235d0bd0ac Mon Sep 17 00:00:00 2001 From: vkrasnovyd Date: Wed, 2 Dec 2026 18:41:57 +0100 Subject: [PATCH 3/4] Extend tests --- .../migrations/test_0103_update_indexes.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/system/migrations/test_0103_update_indexes.py b/tests/system/migrations/test_0103_update_indexes.py index 0b231ade37..f18547c01b 100644 --- a/tests/system/migrations/test_0103_update_indexes.py +++ b/tests/system/migrations/test_0103_update_indexes.py @@ -249,3 +249,24 @@ def test_0103(self) -> None: WHERE indexname = '{index}'; """).fetchone() assert row == {"on_columns": index.split("_t_")[1]} + + # Check correct primary keys not dropped + for pk in [ + "pk_nm_motion_identical_motion_ids_motion_t", + "committee_t_pkey", + ]: + self.assert_table_constraint_exists(curs, pk) + + # Check indexes from schema not dropped + for index in [ + "idx_nm_committee_all_child_ids_committee_t_all_parent_id", + "idx_nm_committee_all_child_ids_committee_t_all_child_id", + "idx_gm_motion_recommendation_extension_reference_ids_t_m81631d0", + "idx_gm_motion_recommendation_extension_reference_ids_t_r6488b59", + "idx_gm_motion_recommendation_extension_reference_ids_t_r1489537", + "idx_history_entry_t_model_id_user_id", + "idx_history_entry_t_model_id_motion_id", + "idx_history_entry_t_model_id_assignment_id", + "idx_history_entry_t_position_id", + ]: + self.assert_index_exists(curs, index) From 84d4a49b2113d2b81f7d33e73b95a924555657f2 Mon Sep 17 00:00:00 2001 From: vkrasnovyd Date: Wed, 2 Dec 2026 18:43:33 +0100 Subject: [PATCH 4/4] Remove indexes.py generator --- .../mig_0103_update_indexes/migration.py | 195 +----------------- 1 file changed, 1 insertion(+), 194 deletions(-) diff --git a/openslides_backend/migrations/migrations/mig_0103_update_indexes/migration.py b/openslides_backend/migrations/migrations/mig_0103_update_indexes/migration.py index 81a5d56344..6e8634c423 100644 --- a/openslides_backend/migrations/migrations/mig_0103_update_indexes/migration.py +++ b/openslides_backend/migrations/migrations/mig_0103_update_indexes/migration.py @@ -4,16 +4,9 @@ from psycopg import Cursor from psycopg.rows import DictRow -from meta.dev.src.generate_sql_schema import Helper -from meta.dev.src.helper_get_names import ( - FieldSqlErrorType, - HelperGetNames, - InternalHelper, - TableFieldType, -) from openslides_backend.migrations.base import BaseMigration -# from .indexes import schema_indexes +from .indexes import schema_indexes class Migration(BaseMigration): @@ -103,192 +96,6 @@ def data_definition(curs: Cursor[DictRow]) -> None: database_indexes = { row["tablename"]: row["indexnames"] for row in curs.fetchall() } - # Calculate indexes defined in schema - schema_indexes: dict[str, dict[str, dict[str, Any]]] = defaultdict(dict) - for collection, collection_data in InternalHelper.MODELS.items(): - if collection == "_meta": - continue - table_name = HelperGetNames.get_table_name(collection) - for unique_together_key in ["unique_together", "unique_together_strict"]: - if fields_groups := collection_data.get(unique_together_key): - for fields in fields_groups: - constraint_name = HelperGetNames.get_unique_constraint_name( - collection, Helper.split_unique_together_fields(fields) - ) - schema_indexes[table_name][constraint_name] = { - "contype": "u", - "fields": fields, - "strict": "strict" in unique_together_key, - } - for field_name, field_def in collection_data["fields"].items(): - if field_name == "id": - continue - if "relation" in (type_ := field_def["type"]): - own_table_field = TableFieldType(collection, field_name, field_def) - if "generic" not in type_: - foreign_table_field: TableFieldType = ( - TableFieldType.get_definitions_from_foreign( - field_def.get("to"), field_def.get("reference") - ) - ) - state, primary, _, _ = ( - InternalHelper.check_relation_definitions( - own_table_field, [foreign_table_field] - ) - ) - if type_ == "relation": - if ( - foreign_table_field.field_def["type"] == "relation" - and state == FieldSqlErrorType.FIELD - ): - constraint_name = ( - HelperGetNames.get_unique_constraint_name( - collection, [field_name] - ) - ) - schema_indexes[table_name][constraint_name] = { - "contype": "u", - "fields": field_name, - } - elif ( - foreign_table_field.field_def["type"] == "relation-list" - ): - schema_indexes[table_name][ - HelperGetNames.get_index_name( - table_name, field_name - ) - ] = { - "contype": None, - "field": field_name, - } - elif ( - "sql" not in field_def - and foreign_table_field.field_def["type"] == "relation-list" - and primary - and state != FieldSqlErrorType.ERROR - ): - nm_table_name, field1, field2 = ( - Helper.get_nm_table_name_and_fields( - own_table_field, foreign_table_field - ) - ) - nm_table_name = HelperGetNames.get_table_name(nm_table_name) - schema_indexes[nm_table_name] = { - HelperGetNames.get_nm_pk_constraint_name( - nm_table_name - ): { - "contype": "p", - "fields": f"{field1}, {field2}", - }, - HelperGetNames.get_index_name(nm_table_name, field1): { - "contype": None, - "field": field1, - }, - HelperGetNames.get_index_name(nm_table_name, field2): { - "contype": None, - "field": field2, - }, - } - else: - foreign_table_fields: list[TableFieldType] = ( - InternalHelper.get_definitions_from_foreign_list( - field_def.get("to"), field_def.get("reference") - ) - ) - if type_ == "generic-relation-list": - ( - gm_table_name, - own_table_name_with_ref_column, - intermediate_field_to_foreign_table_field, - own_table_name, - own_table_column, - foreign_table_ref_lines, - indices_lines, - ) = Helper.get_gm_table_config( - own_table_field, foreign_table_fields - ) - index_1 = HelperGetNames.get_index_name( - gm_table_name, own_table_name_with_ref_column - ) - index_2 = HelperGetNames.get_index_name( - gm_table_name, own_table_column - ) - unique_constraint_name = ( - HelperGetNames.get_generic_unique_constraint_name( - own_table_name_with_ref_column, own_table_column - ) - ) - schema_indexes[gm_table_name] = { - unique_constraint_name: { - "contype": "u", - "fields": f"{own_table_name_with_ref_column}, {own_table_column}", - }, - index_1: { - "contype": None, - "field": own_table_name_with_ref_column, - }, - index_2: { - "contype": None, - "field": own_table_column, - }, - } - for foreign_table_field in foreign_table_fields: - gm_content_field = HelperGetNames.get_gm_content_field( - own_table_column, foreign_table_field.table - ) - schema_indexes[gm_table_name][ - HelperGetNames.get_index_name( - gm_table_name, gm_content_field - ) - ] = {"contype": None, "field": gm_content_field} - else: - for foreign_table_field in foreign_table_fields: - generic_plain_field_name = ( - HelperGetNames.get_generic_plain_field_name( - own_table_field.column, - foreign_table_field.table, - foreign_table_field.ref_column, - ) - ) - if foreign_table_field.field_def["type"] == "relation": - schema_indexes[table_name][ - HelperGetNames.get_unique_constraint_name( - collection, [generic_plain_field_name] - ) - ] = { - "contype": "u", - "fields": generic_plain_field_name, - } - else: - schema_indexes[table_name][ - HelperGetNames.get_index_name( - table_name, generic_plain_field_name - ) - ] = { - "contype": None, - "field": generic_plain_field_name, - } - else: - if field_def.get("unique", False): - schema_indexes[table_name][ - HelperGetNames.get_unique_constraint_name( - collection, [field_name] - ) - ] = {"contype": "u", "fields": field_name} - if sequence_scope := field_def.get("sequence_scope"): - schema_indexes[table_name][ - HelperGetNames.get_unique_constraint_name( - collection, [field_name, sequence_scope] - ) - ] = { - "contype": "u", - "fields": f"{field_name}, {sequence_scope}", - } - with open( - "openslides_backend/migrations/migrations/mig_0103_update_indexes/indexes.py", - "w", - ) as dest: - dest.write(str(dict(schema_indexes))) # Calculate diff drop: dict[str, dict[str, str]] = defaultdict(dict)