From e80c233960189da92107bd5b0e55fe3b18824729 Mon Sep 17 00:00:00 2001 From: Marcio Mazza Date: Fri, 19 Jun 2015 14:53:10 -0300 Subject: [PATCH] Optimize sequence reset on migration --- legacy/scripts/migration.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/legacy/scripts/migration.py b/legacy/scripts/migration.py index 16942f11f..7422d1c74 100644 --- a/legacy/scripts/migration.py +++ b/legacy/scripts/migration.py @@ -63,10 +63,13 @@ def migrate_model(model, to_delete, count_limit=None): model.objects.all().delete() def reset_seq(id): - # resets id sequence - sql_reset_seq = 'ALTER SEQUENCE %s_id_seq RESTART WITH %s;' % (model._meta.db_table, id) - cursor = connection.cursor() - cursor.execute(sql_reset_seq) + # resets id sequence + if id > reset_seq.last_id + 1: + sql_reset_seq = 'ALTER SEQUENCE %s_id_seq RESTART WITH %s;' % (model._meta.db_table, id) + cursor = connection.cursor() + cursor.execute(sql_reset_seq) + reset_seq.last_id = id + reset_seq.last_id = -1 legacy_model = legacy_app.get_model(model.__name__) old_pk_name = legacy_model._meta.pk.name