diff --git a/arkindex/documents/tasks.py b/arkindex/documents/tasks.py index 2dd9855b9ba7fb4d9400db8973c18d6046354de1..956f250eab3cb7c73dad87b4fecf405c17b8578f 100644 --- a/arkindex/documents/tasks.py +++ b/arkindex/documents/tasks.py @@ -41,6 +41,10 @@ def corpus_delete(corpus_id: str) -> None: corpus.top_level_type_id = None corpus.save(update_fields=["top_level_type_id"]) + # Set process.element to NULL in all processes, to avoid IntegrityErrors when deleting elements, as + # elements are deleted before processes. + Process.objects.filter(corpus_id=corpus_id).exclude(element_id=None).update(element_id=None) + # Delete all related objects, bypassing RESTRICTs deletion rules # and Django's way of loading everything into memory before deleting. querysets = [ diff --git a/arkindex/sql_validation/corpus_delete.sql b/arkindex/sql_validation/corpus_delete.sql index 2ebb6b6f75be165e6be9d6bac416a10ec36d74bc..eb4184273585fe0f3a85e1e0fb1f875bfe56ff59 100644 --- a/arkindex/sql_validation/corpus_delete.sql +++ b/arkindex/sql_validation/corpus_delete.sql @@ -10,6 +10,11 @@ FROM "documents_corpus" WHERE "documents_corpus"."id" = '{corpus_id}'::uuid LIMIT 21; +UPDATE "process_process" +SET "element_id" = NULL +WHERE ("process_process"."corpus_id" = '{corpus_id}'::uuid + AND NOT ("process_process"."element_id" IS NULL)); + SELECT "django_content_type"."id", "django_content_type"."app_label", "django_content_type"."model" diff --git a/arkindex/sql_validation/corpus_delete_top_level_type.sql b/arkindex/sql_validation/corpus_delete_top_level_type.sql index 80ae4c77e85a44b11f33a3ef17803d55a9fc6d7b..6ee77ab04200a9b63218e08046aeaefde2f14192 100644 --- a/arkindex/sql_validation/corpus_delete_top_level_type.sql +++ b/arkindex/sql_validation/corpus_delete_top_level_type.sql @@ -14,6 +14,11 @@ UPDATE "documents_corpus" SET "top_level_type_id" = NULL WHERE "documents_corpus"."id" = '{corpus_id}'::uuid; +UPDATE "process_process" +SET "element_id" = NULL +WHERE ("process_process"."corpus_id" = '{corpus_id}'::uuid + AND NOT ("process_process"."element_id" IS NULL)); + SELECT "django_content_type"."id", "django_content_type"."app_label", "django_content_type"."model"