Browse Source

Merge pull request #1544 from interlegis/1538-autoincremento-quebrado-apos-migracao

fix #1538 autoincremento quebrado apos migracao
pull/1554/head
Luciano de Almeida 7 years ago
committed by GitHub
parent
commit
886d0707ea
  1. 24
      sapl/legacy/migration.py

24
sapl/legacy/migration.py

@ -163,11 +163,6 @@ def iter_sql_records(sql, db):
yield record yield record
def get_last_value(model):
last_value = model.objects.all().aggregate(Max('pk'))
return last_value['pk__max'] or 0
def save_relation(obj, nome_campo='', problema='', descricao='', def save_relation(obj, nome_campo='', problema='', descricao='',
eh_stub=False, critico=False): eh_stub=False, critico=False):
link = ProblemaMigracao( link = ProblemaMigracao(
@ -276,6 +271,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 +389,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 +403,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 +447,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