forked from DGNum/gestioCOF
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
This commit is contained in:
parent
f843c337e3
commit
09a96a0375
|
@ -4,11 +4,15 @@ from __future__ import unicode_literals
|
||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
from django.db.models import F
|
from django.db.models import F
|
||||||
|
|
||||||
|
from utils.models import sqlsequencereset
|
||||||
|
|
||||||
|
|
||||||
def profiles_to_gestion(apps, schema_editor):
|
def profiles_to_gestion(apps, schema_editor):
|
||||||
OldProfile = apps.get_model('cof', 'CofProfile')
|
OldProfile = apps.get_model('cof', 'CofProfile')
|
||||||
NewProfile = apps.get_model('gestion', 'Profile')
|
NewProfile = apps.get_model('gestion', 'Profile')
|
||||||
|
|
||||||
|
connection = schema_editor.connection
|
||||||
|
|
||||||
# New profiles are massively imported from the old profiles.
|
# New profiles are massively imported from the old profiles.
|
||||||
# IDs are kept identical to ease the migration of models which were
|
# IDs are kept identical to ease the migration of models which were
|
||||||
# referencing the CofProfile model.
|
# referencing the CofProfile model.
|
||||||
|
@ -30,6 +34,8 @@ def profiles_to_gestion(apps, schema_editor):
|
||||||
|
|
||||||
NewProfile.objects.bulk_create(new_profiles)
|
NewProfile.objects.bulk_create(new_profiles)
|
||||||
|
|
||||||
|
sqlsequencereset([NewProfile], conn=connection)
|
||||||
|
|
||||||
OldProfile.objects.all().update(profile_id=F('id'))
|
OldProfile.objects.all().update(profile_id=F('id'))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,8 @@ from __future__ import unicode_literals
|
||||||
|
|
||||||
from django.db import migrations
|
from django.db import migrations
|
||||||
|
|
||||||
|
from utils.models import sqlsequencereset
|
||||||
|
|
||||||
|
|
||||||
def event_to_gestion(apps, schema_editor):
|
def event_to_gestion(apps, schema_editor):
|
||||||
# Fetching the models that have be moved from cof to gestion
|
# 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')
|
Location = apps.get_model('gestion', 'Location')
|
||||||
Association = apps.get_model('gestion', 'Association')
|
Association = apps.get_model('gestion', 'Association')
|
||||||
|
|
||||||
|
connection = schema_editor.connection
|
||||||
|
|
||||||
# The old Event.location field becomes a table: we need to create an entry
|
# 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.
|
# in this table for each value of the old `location` field.
|
||||||
locations = set() # A set to prevent duplicate entries
|
locations = set() # A set to prevent duplicate entries
|
||||||
|
@ -31,6 +35,8 @@ def event_to_gestion(apps, schema_editor):
|
||||||
for event in new_events
|
for event in new_events
|
||||||
])
|
])
|
||||||
|
|
||||||
|
sqlsequencereset([NewEvent], conn=connection)
|
||||||
|
|
||||||
# Do not forget to link all the existing event to the COF association
|
# Do not forget to link all the existing event to the COF association
|
||||||
cof_assoc = Association.objects.get(name="COF")
|
cof_assoc = Association.objects.get(name="COF")
|
||||||
cof_assoc.events.add(*NewEvent.objects.all())
|
cof_assoc.events.add(*NewEvent.objects.all())
|
||||||
|
@ -57,6 +63,7 @@ def event_to_gestion(apps, schema_editor):
|
||||||
ToModel(**values)
|
ToModel(**values)
|
||||||
for values in FromModel.objects.values()
|
for values in FromModel.objects.values()
|
||||||
])
|
])
|
||||||
|
sqlsequencereset([ToModel], conn=connection)
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
|
|
0
utils/__init__.py
Normal file
0
utils/__init__.py
Normal file
17
utils/models.py
Normal file
17
utils/models.py
Normal file
|
@ -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)
|
Loading…
Reference in a new issue