Browse Source

Corrige problema da exclusão de objetos protegidos

Signed-off-by: Luciano Almeida <lucianoalmeida@interlegis.leg.br>
pull/978/head
Luciano Almeida 8 years ago
parent
commit
5245b34e6a
  1. 28
      sapl/legacy/migration.py
  2. 1
      sapl/legacy/scripts/shell_para_migracao.sh

28
sapl/legacy/migration.py

@ -246,14 +246,12 @@ def recreate_constraints():
unique_constraints.clear() unique_constraints.clear()
def stub_desnecessario(obj): def obj_desnecessario(obj):
lista_fields = [ lista_fields = [
f for f in obj._meta.get_fields() f for f in obj._meta.get_fields()
if (f.one_to_many or f.one_to_one) and f.auto_created if (f.one_to_many or f.one_to_one) and f.auto_created]
] desnecessario = not any(rr.related_model.objects.filter(
desnecessario = not any( **{rr.field.name: obj}).exists() for rr in lista_fields)
rr.related_model.objects.filter(**{rr.field.name: obj}).exists()
for rr in lista_fields)
return desnecessario return desnecessario
@ -424,6 +422,7 @@ class DataMigrator:
info('Deletando models com ind_excluido...') info('Deletando models com ind_excluido...')
while self.to_delete: while self.to_delete:
for obj in self.to_delete: for obj in self.to_delete:
if obj_desnecessario(obj):
try: try:
obj.delete() obj.delete()
self.to_delete.remove(obj) self.to_delete.remove(obj)
@ -434,6 +433,14 @@ class DataMigrator:
warn(msg + ' => ' + descricao) warn(msg + ' => ' + descricao)
save_relation(obj=obj, problema=msg, save_relation(obj=obj, problema=msg,
descricao=descricao, eh_stub=False) descricao=descricao, eh_stub=False)
else:
msg = 'A entrada de PK %s da model %s não pode ser ' \
'excluida' % (obj.pk, obj._meta.model_name)
descricao = 'Outros objetos dependem dessa entrada'
warn(msg + ' => ' + descricao)
save_relation(obj=obj, problema=msg,
descricao=descricao, eh_stub=False)
self.to_delete.remove(obj)
info('Deletando stubs desnecessários...') info('Deletando stubs desnecessários...')
while self.delete_stubs(): while self.delete_stubs():
@ -468,13 +475,6 @@ class DataMigrator:
legacy_model = legacy_app.get_model(legacy_model_name) legacy_model = legacy_app.get_model(legacy_model_name)
legacy_pk_name = legacy_model._meta.pk.name legacy_pk_name = legacy_model._meta.pk.name
# Clear all model entries
# They may have been created in a previous migration attempt
try:
model.objects.all().delete()
except ProtectedError:
Proposicao.objects.all().delete()
model.objects.all().delete()
delete_constraints(model) delete_constraints(model)
# setup migration strategy for tables with or without a pk # setup migration strategy for tables with or without a pk
@ -520,7 +520,7 @@ class DataMigrator:
if obj.content_object and obj.eh_stub: if obj.content_object and obj.eh_stub:
original = obj.content_type.get_all_objects_for_this_type( original = obj.content_type.get_all_objects_for_this_type(
id=obj.object_id) id=obj.object_id)
if stub_desnecessario(original[0]): if obj_desnecessario(original[0]):
qtd_exclusoes, *_ = original.delete() qtd_exclusoes, *_ = original.delete()
assert qtd_exclusoes == 1 assert qtd_exclusoes == 1
qtd_exclusoes, *_ = obj.delete() qtd_exclusoes, *_ = obj.delete()

1
sapl/legacy/scripts/shell_para_migracao.sh

@ -5,4 +5,5 @@
# Rode esse script a partir da raiz do projeto # Rode esse script a partir da raiz do projeto
DATABASE_NAME=$1 ./manage.py migrate --settings sapl.legacy_migration_settings
DATABASE_NAME=$1 ./manage.py shell_plus --settings sapl.legacy_migration_settings DATABASE_NAME=$1 ./manage.py shell_plus --settings sapl.legacy_migration_settings

Loading…
Cancel
Save