From 389a2446155653da10efaf7a9bb8cb8f228d813b Mon Sep 17 00:00:00 2001 From: Finn Stutzenstein Date: Fri, 14 Aug 2020 10:30:33 +0200 Subject: [PATCH] Fixed errors while creating countdown due to postgres id sequences --- openslides/agenda/models.py | 5 ++++- openslides/users/db.py | 17 ----------------- .../0011_postgresql_auth_group_id_sequence.py | 6 +++++- openslides/users/signals.py | 9 +++++---- openslides/utils/postgres.py | 17 +++++++++++++++++ 5 files changed, 31 insertions(+), 23 deletions(-) delete mode 100644 openslides/users/db.py create mode 100644 openslides/utils/postgres.py diff --git a/openslides/agenda/models.py b/openslides/agenda/models.py index 7183cfc47..cbb608330 100644 --- a/openslides/agenda/models.py +++ b/openslides/agenda/models.py @@ -18,6 +18,7 @@ from openslides.utils.models import ( SET_NULL_AND_AUTOUPDATE, RESTModelMixin, ) +from openslides.utils.postgres import restart_id_sequence from openslides.utils.utils import to_roman from .access_permissions import ItemAccessPermissions, ListOfSpeakersAccessPermissions @@ -535,7 +536,9 @@ class Speaker(RESTModelMixin, models.Model): "countdown_time": config["projector_default_countdown"], }, ) - if not created: + if created: + restart_id_sequence("core_countdown") + else: countdown.control(action="reset", skip_autoupdate=True) countdown.control(action="start", skip_autoupdate=True) diff --git a/openslides/users/db.py b/openslides/users/db.py deleted file mode 100644 index 20b619a27..000000000 --- a/openslides/users/db.py +++ /dev/null @@ -1,17 +0,0 @@ -from django.db import connection - - -def postgres_restart_auth_group_id_sequence(*args, **kwargs): - """ - This function resets the id sequence from the auth_group table (the current auto - increment value for the id field) to the max_id+1. This is needed, when inserting - groups by id, because Postgresql does not update the id sequence. - """ - if connection.vendor == "postgresql": - with connection.cursor() as cursor: - cursor.execute("SELECT max(id) + 1 as max FROM auth_group;") - max_id = cursor.fetchone()[0] - if max_id is not None: - cursor.execute( - f"ALTER SEQUENCE auth_group_id_seq RESTART WITH {max_id};" - ) diff --git a/openslides/users/migrations/0011_postgresql_auth_group_id_sequence.py b/openslides/users/migrations/0011_postgresql_auth_group_id_sequence.py index c1df11d5b..87b78a840 100644 --- a/openslides/users/migrations/0011_postgresql_auth_group_id_sequence.py +++ b/openslides/users/migrations/0011_postgresql_auth_group_id_sequence.py @@ -2,7 +2,11 @@ from django.db import migrations -from openslides.users.db import postgres_restart_auth_group_id_sequence +from openslides.utils.postgres import restart_id_sequence + + +def postgres_restart_auth_group_id_sequence(*args, **kwargs): + restart_id_sequence("auth_group") class Migration(migrations.Migration): diff --git a/openslides/users/signals.py b/openslides/users/signals.py index 1ee85ca92..aa3c3a743 100644 --- a/openslides/users/signals.py +++ b/openslides/users/signals.py @@ -2,8 +2,9 @@ from django.apps import apps from django.contrib.auth.models import Permission from django.db.models import Q -from ..utils.auth import GROUP_ADMIN_PK, GROUP_DEFAULT_PK -from .db import postgres_restart_auth_group_id_sequence +from openslides.utils.auth import GROUP_ADMIN_PK, GROUP_DEFAULT_PK +from openslides.utils.postgres import restart_id_sequence + from .models import Group, User @@ -185,5 +186,5 @@ def create_builtin_groups_and_admin(**kwargs): # added to the group. But we do not have to update the cache by calling # inform_changed_data() because the cache is updated on server start. - # For postgres: After inserting the gorups by id, the id sequence needs to be restarted. - postgres_restart_auth_group_id_sequence() + # For postgres: After inserting the groups by id, the id sequence needs to be restarted. + restart_id_sequence("auth_group") diff --git a/openslides/utils/postgres.py b/openslides/utils/postgres.py new file mode 100644 index 000000000..c52010ed1 --- /dev/null +++ b/openslides/utils/postgres.py @@ -0,0 +1,17 @@ +from django.db import connection + + +def restart_id_sequence(table_name: str) -> None: + """ + This function resets the id sequence from the given table (the current auto + increment value for the id field) to the max_id+1. This is needed, when manually + inserting object id, because Postgresql does not update the id sequence in this case. + """ + if connection.vendor == "postgresql": + with connection.cursor() as cursor: + cursor.execute(f"SELECT max(id) + 1 as max FROM {table_name};") + max_id = cursor.fetchone()[0] + if max_id is not None: + cursor.execute( + f"ALTER SEQUENCE {table_name}_id_seq RESTART WITH {max_id};" + )