From 09a96a03750d22947036aec34999a8d13d3eb0ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Sat, 28 Oct 2017 17:30:37 +0200 Subject: [PATCH] Fix postgres sequences When manually assigning a value to AutoField, sequences must be updated manually. https://docs.djangoproject.com/en/1.11/ref/databases/#manually-specifying-values-of-auto-incrementing-primary-keys --- cof/migrations/0014_move_profile.py | 6 ++++++ cof/migrations/0016_move_event.py | 7 +++++++ utils/__init__.py | 0 utils/models.py | 17 +++++++++++++++++ 4 files changed, 30 insertions(+) create mode 100644 utils/__init__.py create mode 100644 utils/models.py diff --git a/cof/migrations/0014_move_profile.py b/cof/migrations/0014_move_profile.py index 344b512c..52e537eb 100644 --- a/cof/migrations/0014_move_profile.py +++ b/cof/migrations/0014_move_profile.py @@ -4,11 +4,15 @@ from __future__ import unicode_literals from django.db import migrations, models from django.db.models import F +from utils.models import sqlsequencereset + def profiles_to_gestion(apps, schema_editor): OldProfile = apps.get_model('cof', 'CofProfile') NewProfile = apps.get_model('gestion', 'Profile') + connection = schema_editor.connection + # New profiles are massively imported from the old profiles. # IDs are kept identical to ease the migration of models which were # referencing the CofProfile model. @@ -30,6 +34,8 @@ def profiles_to_gestion(apps, schema_editor): NewProfile.objects.bulk_create(new_profiles) + sqlsequencereset([NewProfile], conn=connection) + OldProfile.objects.all().update(profile_id=F('id')) diff --git a/cof/migrations/0016_move_event.py b/cof/migrations/0016_move_event.py index 60e1bbf5..033bf3a7 100644 --- a/cof/migrations/0016_move_event.py +++ b/cof/migrations/0016_move_event.py @@ -3,6 +3,8 @@ from __future__ import unicode_literals from django.db import migrations +from utils.models import sqlsequencereset + def event_to_gestion(apps, schema_editor): # Fetching the models that have be moved from cof to gestion @@ -11,6 +13,8 @@ def event_to_gestion(apps, schema_editor): Location = apps.get_model('gestion', 'Location') Association = apps.get_model('gestion', 'Association') + connection = schema_editor.connection + # The old Event.location field becomes a table: we need to create an entry # in this table for each value of the old `location` field. locations = set() # A set to prevent duplicate entries @@ -31,6 +35,8 @@ def event_to_gestion(apps, schema_editor): for event in new_events ]) + sqlsequencereset([NewEvent], conn=connection) + # Do not forget to link all the existing event to the COF association cof_assoc = Association.objects.get(name="COF") cof_assoc.events.add(*NewEvent.objects.all()) @@ -57,6 +63,7 @@ def event_to_gestion(apps, schema_editor): ToModel(**values) for values in FromModel.objects.values() ]) + sqlsequencereset([ToModel], conn=connection) class Migration(migrations.Migration): diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/utils/models.py b/utils/models.py new file mode 100644 index 00000000..9ae0a581 --- /dev/null +++ b/utils/models.py @@ -0,0 +1,17 @@ +from django.core.management.color import no_style +from django.db import connection + + +def sqlsequencereset(models, conn=connection): + """ + Update sequences of fields of `models`. + + This function should be called after manually assigning a value to an + `AutoField`. + https://docs.djangoproject.com/en/1.11/ref/databases/#manually-specifying-values-of-auto-incrementing-primary-keys + """ + sequence_sql = conn.ops.sequence_reset_sql(no_style(), models) + if sequence_sql: + with conn.cursor() as cursor: + for line in sequence_sql: + cursor.execute(line)