From 5245b34e6a1b938cdb5c15ccda84023e9a52945f Mon Sep 17 00:00:00 2001 From: Luciano Almeida Date: Fri, 24 Mar 2017 17:07:05 -0300 Subject: [PATCH] =?UTF-8?q?Corrige=20problema=20da=20exclus=C3=A3o=20de=20?= =?UTF-8?q?objetos=20protegidos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Luciano Almeida --- sapl/legacy/migration.py | 38 +++++++++++----------- sapl/legacy/scripts/shell_para_migracao.sh | 1 + 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/sapl/legacy/migration.py b/sapl/legacy/migration.py index 379ac13ad..1342009b8 100644 --- a/sapl/legacy/migration.py +++ b/sapl/legacy/migration.py @@ -246,14 +246,12 @@ def recreate_constraints(): unique_constraints.clear() -def stub_desnecessario(obj): +def obj_desnecessario(obj): lista_fields = [ f for f in obj._meta.get_fields() - if (f.one_to_many or f.one_to_one) and f.auto_created - ] - desnecessario = not any( - rr.related_model.objects.filter(**{rr.field.name: obj}).exists() - for rr in lista_fields) + if (f.one_to_many or f.one_to_one) and f.auto_created] + desnecessario = not any(rr.related_model.objects.filter( + **{rr.field.name: obj}).exists() for rr in lista_fields) return desnecessario @@ -424,16 +422,25 @@ class DataMigrator: info('Deletando models com ind_excluido...') while self.to_delete: for obj in self.to_delete: - try: - obj.delete() - self.to_delete.remove(obj) - except ProtectedError: + if obj_desnecessario(obj): + try: + obj.delete() + self.to_delete.remove(obj) + except ProtectedError: + msg = 'A entrada de PK %s da model %s não pode ser ' \ + 'excluida' % (obj.pk, obj._meta.model_name) + descricao = 'Um ou mais objetos protegidos ' + warn(msg + ' => ' + descricao) + save_relation(obj=obj, problema=msg, + 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 = 'Um ou mais objetos protegidos ' + 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...') while self.delete_stubs(): @@ -468,13 +475,6 @@ class DataMigrator: legacy_model = legacy_app.get_model(legacy_model_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) # setup migration strategy for tables with or without a pk @@ -520,7 +520,7 @@ class DataMigrator: if obj.content_object and obj.eh_stub: original = obj.content_type.get_all_objects_for_this_type( id=obj.object_id) - if stub_desnecessario(original[0]): + if obj_desnecessario(original[0]): qtd_exclusoes, *_ = original.delete() assert qtd_exclusoes == 1 qtd_exclusoes, *_ = obj.delete() diff --git a/sapl/legacy/scripts/shell_para_migracao.sh b/sapl/legacy/scripts/shell_para_migracao.sh index fb0f3c6de..5f5ea97b7 100755 --- a/sapl/legacy/scripts/shell_para_migracao.sh +++ b/sapl/legacy/scripts/shell_para_migracao.sh @@ -5,4 +5,5 @@ # 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