Skip to content

Commit

Permalink
Reset DB sequences after installing the fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Jan 22, 2015
1 parent 133919e commit b955f7b
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions django_migration_fixture/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
from django.core import serializers

from django.core import serializers
from django.core.management.color import no_style
from django.db import connection
from django.db.models import signals


Expand All @@ -9,6 +11,12 @@ class FixtureObjectDoesNotExist(Exception):
pass


def reset_db_sequences(models):
cursor = connection.cursor()
for sql in connection.ops.sequence_reset_sql(no_style(), models):
cursor.execute(sql)


def fixture(app, fixtures, fixtures_dir='fixtures', raise_does_not_exist=False):
"""
Load fixtures using a data migration.
Expand Down Expand Up @@ -42,7 +50,10 @@ def load_fixture(app_config, schema_editor):
This delays the actual loading until this app itself is being called
through the post_migrate signal hook. This is necessary for
the contenttypes and auth data to be available."""
the contenttypes and auth data to be available.
After the fixture has been loaded the database sequences for affected
models are being 'reset' (using `coalesce` with PostgreSQL)."""

def signal_handler(app_config, sender, **kwargs):
if sender.label == "django_migration_fixture":
Expand All @@ -53,6 +64,8 @@ def signal_handler(app_config, sender, **kwargs):
obj.save()
models.add(obj.object._meta.model)

reset_db_sequences(models)

signals.post_migrate.connect(signal_handler, weak=False)

def unload_fixture(apps, schema_editor):
Expand Down

0 comments on commit b955f7b

Please sign in to comment.