Skip to content

Saleor Core: the high performance, composable, headless commerce API.

add sequence reset to populatedb

Michał Jelonek1841f5d3128c
diff --git a/saleor/core/management/commands/populatedb.py b/saleor/core/management/commands/populatedb.py
index 907915782c..b56bf3bc62 100644
--- a/saleor/core/management/commands/populatedb.py
+++ b/saleor/core/management/commands/populatedb.py
@@ -1,3 +1,6 @@
+from io import StringIO
+
+from django.apps import apps
from django.conf import settings
from django.core.management import call_command
from django.core.management.base import BaseCommand
@@ -34,6 +37,12 @@ class Command(BaseCommand):
dest='withoutsearch',
default=False,
help='Don\'t update search index')
+ parser.add_argument(
+ '--skipsequencereset',
+ action='store_true',
+ dest='skipsequencereset',
+ default=False,
+ help='Don\'t reset SQL sequences that are out of sync.')
def make_database_faster(self):
"""Sacrifice some of the safeguards of sqlite3 for speed.
@@ -50,6 +59,22 @@ class Command(BaseCommand):
if settings.ES_URL:
call_command('search_index', '--rebuild', force=True)
+ def sequence_reset(self):
+ """Runs SQL sequence reset on all saleor.* apps.
+
+ When a value is manually assigned to an auto-incrementing field
+ it doesn't update the field's sequence, which might cause a conflict
+ later on.
+ """
+ commands = StringIO()
+ for app in apps.get_app_configs():
+ if 'saleor' in app.name:
+ call_command(
+ 'sqlsequencereset', app.label,
+ stdout=commands, no_color=True)
+ with connection.cursor() as cursor:
+ cursor.execute(commands.getvalue())
+
def handle(self, *args, **options):
self.make_database_faster()
create_images = not options['withoutimages']
@@ -81,3 +106,5 @@ class Command(BaseCommand):
add_address_to_admin(credentials['email'])
if not options['withoutsearch']:
self.populate_search_index()
+ if not options['skipsequencereset']:
+ self.sequence_reset()