Skip to content

Commit

Permalink
Merge pull request django-import-export#398 from sgarcialaguna/master
Browse files Browse the repository at this point in the history
Reset SQL sequences when new objects are imported
  • Loading branch information
bmihelac committed Feb 11, 2016
2 parents 4672176 + 8c3244e commit ceac928
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
13 changes: 12 additions & 1 deletion import_export/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

from django import VERSION
from django.conf import settings
from django.db import transaction
from django.core.management.color import no_style
from django.db import connections, transaction, DEFAULT_DB_ALIAS
from django.db.models.fields import FieldDoesNotExist
from django.db.models.query import QuerySet
from django.db.transaction import TransactionManagementError
Expand Down Expand Up @@ -397,6 +398,16 @@ def import_data(self, dataset, dry_run=False, raise_errors=False,
self._meta.report_skipped):
result.rows.append(row_result)

# Reset the SQL sequences when new objects are imported
# Adapted from django's loaddata
if not dry_run and any(r.import_type == RowResult.IMPORT_TYPE_NEW for r in result.rows):
connection = connections[DEFAULT_DB_ALIAS]
sequence_sql = connection.ops.sequence_reset_sql(no_style(), [self.Meta.model])
if sequence_sql:
with connection.cursor() as cursor:
for line in sequence_sql:
cursor.execute(line)

if use_transactions:
if dry_run or result.has_errors():
savepoint_rollback(sp1)
Expand Down
1 change: 1 addition & 0 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-r base.txt
sphinx
mysql-python
psycopg2==2.6.1
23 changes: 22 additions & 1 deletion tests/core/tests/resources_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
from copy import deepcopy
from datetime import date
from decimal import Decimal
from unittest import skip
from unittest import skip, skipUnless

from django.conf import settings
from django.contrib.auth.models import User
from django.db import IntegrityError
from django.db.models import Count
from django.db.models.fields import FieldDoesNotExist
from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature
Expand Down Expand Up @@ -668,3 +670,22 @@ def test_create(self):
BookResource = resources.modelresource_factory(Book)
self.assertIn('id', BookResource.fields)
self.assertEqual(BookResource._meta.model, Book)


@skipUnless(
'postgresql' in settings.DATABASES['default']['ENGINE'],
'Run only against Postgres')
class PostgresTests(TransactionTestCase):
# Make sure to start the sequences back at 1
reset_sequences = True

def test_create_object_after_importing_dataset_with_id(self):
dataset = tablib.Dataset(headers=['id', 'name'])
dataset.append([1, 'Some book'])
resource = BookResource()
result = resource.import_data(dataset)
self.assertFalse(result.has_errors())
try:
Book.objects.create(name='Some other book')
except IntegrityError:
self.fail('IntegrityError was raised.')
12 changes: 12 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@
}
}
}
elif os.environ.get('IMPORT_EXPORT_TEST_TYPE') == 'postgres':
IMPORT_EXPORT_USE_TRANSACTIONS = True
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'import_export',
'USER': os.environ.get('IMPORT_EXPORT_POSTGRESQL_USER'),
'PASSWORD': os.environ.get('IMPORT_EXPORT_POSTGRESQL_PASSWORD'),
'HOST': 'localhost',
'PORT': 5432
}
}
else:
DATABASES = {
'default': {
Expand Down

0 comments on commit ceac928

Please sign in to comment.