From b97298e77f9e8cd7259ef8bf14ee99ef64efa7f3 Mon Sep 17 00:00:00 2001 From: Marcio Mazza Date: Thu, 19 Oct 2017 14:10:31 -0200 Subject: [PATCH] =?UTF-8?q?Reinicia=20sequence=20ao=20final=20da=20migraca?= =?UTF-8?q?o=20caso=20necess=C3=A1rio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sapl/legacy/migration.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/sapl/legacy/migration.py b/sapl/legacy/migration.py index e9d4f73b1..5200e6c38 100644 --- a/sapl/legacy/migration.py +++ b/sapl/legacy/migration.py @@ -276,6 +276,17 @@ def delete_old(legacy_model, cols_values): exec_sql(delete_sql, 'legacy') +def get_last_pk(model): + last_value = model.objects.all().aggregate(Max('pk')) + return last_value['pk__max'] or 0 + + +def reinicia_sequence(model, id): + sequence_name = '%s_id_seq' % model._meta.db_table + exec_sql('ALTER SEQUENCE %s RESTART WITH %s MINVALUE -1;' % ( + sequence_name, id)) + + class DataMigrator: def __init__(self): @@ -383,7 +394,9 @@ class DataMigrator: # setup migration strategy for tables with or without a pk if legacy_pk_name == 'id': + deve_ajustar_sequence_ao_final = False # There is no pk in the legacy table + def save(new, old): with reversion.create_revision(): new.save() @@ -395,6 +408,8 @@ class DataMigrator: old_records = iter_sql_records( 'select * from ' + legacy_model._meta.db_table, 'legacy') else: + deve_ajustar_sequence_ao_final = True + def save(new, old): with reversion.create_revision(): # salva new com id de old @@ -437,6 +452,10 @@ class DataMigrator: self.data_mudada.clear() reversion.set_comment( 'Ajuste de data pela migração') + # reinicia sequence + if deve_ajustar_sequence_ao_final: + last_pk = get_last_pk(model) + reinicia_sequence(model, last_pk + 1) def migrate(obj=appconfs, interativo=True):