Merge pull request #4872 from FinnStutzenstein/postgresIdSequence
Restarting the id sequence for postgresql
This commit is contained in:
commit
1ed9aab380
17
openslides/users/db.py
Normal file
17
openslides/users/db.py
Normal file
@ -0,0 +1,17 @@
|
||||
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};"
|
||||
)
|
@ -14,6 +14,9 @@ def create_superadmin_group(apps, schema_editor):
|
||||
- If a group with the name 'Admin' (probably with pk = 4) exists, move all
|
||||
users from it to the new superadmin group and delete it. If not, check for
|
||||
the staff group and assign all users to the superadmin group.
|
||||
|
||||
In 0011_postgres_auth_group_id_sequence, the id sequence for this migration is
|
||||
restarted when using postgresql.
|
||||
"""
|
||||
Group = apps.get_model("users", "Group")
|
||||
|
||||
|
@ -0,0 +1,19 @@
|
||||
# Generated by Finn Stutzenstein on 2019-07-23 13:37
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
from openslides.users.db import postgres_restart_auth_group_id_sequence
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
"""
|
||||
When migrating old databases (especially after 0007_superadmin) the id sequence
|
||||
in postgres needs to be restarted.
|
||||
|
||||
This is a additional migration to 0007_superadmin. If a later migration does
|
||||
something with changing groups in the database, this method needs to run again.
|
||||
"""
|
||||
|
||||
dependencies = [("users", "0010_auto_20190119_1447")]
|
||||
|
||||
operations = [migrations.RunPython(postgres_restart_auth_group_id_sequence)]
|
@ -1,9 +1,9 @@
|
||||
from django.apps import apps
|
||||
from django.contrib.auth.models import Permission
|
||||
from django.db import connection
|
||||
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 .models import Group, User
|
||||
|
||||
|
||||
@ -185,11 +185,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, the id sequence (the current auto increment value for the id field)
|
||||
# needs to be refreshed after inserting the groups per id, because postgres does not
|
||||
# increment the sequence then.
|
||||
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]
|
||||
cursor.execute(f"ALTER SEQUENCE auth_group_id_seq RESTART WITH {max_id};")
|
||||
# For postgres: After inserting the gorups by id, the id sequence needs to be restarted.
|
||||
postgres_restart_auth_group_id_sequence()
|
||||
|
Loading…
Reference in New Issue
Block a user