Merge pull request #5508 from FinnStutzenstein/restartPostgresIdSequence

Fixed errors while creating countdown due to postgres id sequences
This commit is contained in:
Finn Stutzenstein 2020-08-14 11:15:09 +02:00 committed by GitHub
commit 2e8e32454e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 23 deletions

View File

@ -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)

View File

@ -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};"
)

View File

@ -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):

View File

@ -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")

View File

@ -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};"
)