Browse Source

Reinicia sequence ao final da migracao caso necessário

pull/1544/head
Marcio Mazza 7 years ago
committed by Luciano Almeida
parent
commit
b97298e77f
  1. 19
      sapl/legacy/migration.py

19
sapl/legacy/migration.py

@ -276,6 +276,17 @@ def delete_old(legacy_model, cols_values):
exec_sql(delete_sql, 'legacy') 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: class DataMigrator:
def __init__(self): def __init__(self):
@ -383,7 +394,9 @@ class DataMigrator:
# setup migration strategy for tables with or without a pk # setup migration strategy for tables with or without a pk
if legacy_pk_name == 'id': if legacy_pk_name == 'id':
deve_ajustar_sequence_ao_final = False
# There is no pk in the legacy table # There is no pk in the legacy table
def save(new, old): def save(new, old):
with reversion.create_revision(): with reversion.create_revision():
new.save() new.save()
@ -395,6 +408,8 @@ class DataMigrator:
old_records = iter_sql_records( old_records = iter_sql_records(
'select * from ' + legacy_model._meta.db_table, 'legacy') 'select * from ' + legacy_model._meta.db_table, 'legacy')
else: else:
deve_ajustar_sequence_ao_final = True
def save(new, old): def save(new, old):
with reversion.create_revision(): with reversion.create_revision():
# salva new com id de old # salva new com id de old
@ -437,6 +452,10 @@ class DataMigrator:
self.data_mudada.clear() self.data_mudada.clear()
reversion.set_comment( reversion.set_comment(
'Ajuste de data pela migração') '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): def migrate(obj=appconfs, interativo=True):

Loading…
Cancel
Save