Fixed errors while creating countdown due to postgres id sequences
This commit is contained in:
parent
a46d8ec7ad
commit
389a244615
@ -18,6 +18,7 @@ from openslides.utils.models import (
|
|||||||
SET_NULL_AND_AUTOUPDATE,
|
SET_NULL_AND_AUTOUPDATE,
|
||||||
RESTModelMixin,
|
RESTModelMixin,
|
||||||
)
|
)
|
||||||
|
from openslides.utils.postgres import restart_id_sequence
|
||||||
from openslides.utils.utils import to_roman
|
from openslides.utils.utils import to_roman
|
||||||
|
|
||||||
from .access_permissions import ItemAccessPermissions, ListOfSpeakersAccessPermissions
|
from .access_permissions import ItemAccessPermissions, ListOfSpeakersAccessPermissions
|
||||||
@ -535,7 +536,9 @@ class Speaker(RESTModelMixin, models.Model):
|
|||||||
"countdown_time": config["projector_default_countdown"],
|
"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="reset", skip_autoupdate=True)
|
||||||
countdown.control(action="start", skip_autoupdate=True)
|
countdown.control(action="start", skip_autoupdate=True)
|
||||||
|
|
||||||
|
@ -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};"
|
|
||||||
)
|
|
@ -2,7 +2,11 @@
|
|||||||
|
|
||||||
from django.db import migrations
|
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):
|
class Migration(migrations.Migration):
|
||||||
|
@ -2,8 +2,9 @@ from django.apps import apps
|
|||||||
from django.contrib.auth.models import Permission
|
from django.contrib.auth.models import Permission
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
|
|
||||||
from ..utils.auth import GROUP_ADMIN_PK, GROUP_DEFAULT_PK
|
from openslides.utils.auth import GROUP_ADMIN_PK, GROUP_DEFAULT_PK
|
||||||
from .db import postgres_restart_auth_group_id_sequence
|
from openslides.utils.postgres import restart_id_sequence
|
||||||
|
|
||||||
from .models import Group, User
|
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
|
# 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.
|
# 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.
|
# For postgres: After inserting the groups by id, the id sequence needs to be restarted.
|
||||||
postgres_restart_auth_group_id_sequence()
|
restart_id_sequence("auth_group")
|
||||||
|
17
openslides/utils/postgres.py
Normal file
17
openslides/utils/postgres.py
Normal 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};"
|
||||||
|
)
|
Loading…
Reference in New Issue
Block a user