{"action":"created","comment":{"url":"https://api.github.com/repos/uktrade/data-hub-leeloo/pulls/comments/202409393","pull_request_review_id":137109816,"id":202409393,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIwMjQwOTM5Mw==","diff_hunk":"@@ -0,0 +1,373 @@\n+from datetime import datetime\n+from unittest import mock\n+\n+import pytest\n+from dateutil.relativedelta import relativedelta\n+from django.apps import apps\n+from django.conf import settings\n+from django.core import management\n+from django.core.management import CommandError\n+from django.db.models import QuerySet\n+from django.db.models.signals import post_delete\n+from django.utils.timezone import utc\n+from freezegun import freeze_time\n+\n+from datahub.cleanup.management.commands import delete_old_records, delete_orphans\n+from datahub.cleanup.query_utils import get_related_fields\n+from datahub.cleanup.test.commands.factories import ShallowInvestmentProjectFactory\n+from datahub.company.test.factories import CompanyFactory, ContactFactory\n+from datahub.core.exceptions import DataHubException\n+from datahub.event.test.factories import EventFactory\n+from datahub.interaction.test.factories import CompanyInteractionFactory\n+from datahub.investment.test.factories import InvestmentProjectFactory\n+from datahub.leads.test.factories import BusinessLeadFactory\n+from datahub.omis.order.test.factories import OrderFactory\n+from datahub.omis.quote.test.factories import QuoteFactory\n+from datahub.search.apps import get_search_app_by_model, get_search_apps\n+\n+COMMAND_CLASSES = [\n+    delete_old_records.Command,\n+    delete_orphans.Command,\n+]\n+\n+FROZEN_TIME = datetime(2018, 6, 1, 2, tzinfo=utc)\n+\n+\"\"\"\n+For each model in the CONFIG attribute for clean-up management commands:\n+    - specify the factory class to create an instance\n+    - specify the list of dependent models as a tuple of:\n+        - dependent factory class to create an instance\n+        - dependent field name referencing the main model\n+    - specify any implicitly-defined related models (e.g. for many-to-many fields) that would be\n+      deleted even for orphaned records\n+\"\"\"\n+MAPPINGS = {\n+    'company.Contact': {\n+        'factory': ContactFactory,\n+        'dependent_models': (\n+            (CompanyInteractionFactory, 'contact'),\n+            (OrderFactory, 'contact'),\n+            (QuoteFactory, 'accepted_by'),\n+            (InvestmentProjectFactory, 'client_contacts'),\n+        ),\n+        'implicit_related_models': (),\n+    },\n+    'company.Company': {\n+        'factory': CompanyFactory,\n+        'dependent_models': (\n+            (CompanyInteractionFactory, 'company'),\n+            (ContactFactory, 'company'),\n+            (ShallowInvestmentProjectFactory, 'intermediate_company'),\n+            (ShallowInvestmentProjectFactory, 'investor_company'),\n+            (ShallowInvestmentProjectFactory, 'uk_company'),\n+            (OrderFactory, 'company'),\n+            (CompanyFactory, 'global_headquarters'),\n+            (CompanyFactory, 'parent'),\n+            (BusinessLeadFactory, 'company'),\n+        ),\n+        'implicit_related_models': (),","path":"datahub/cleanup/test/commands/test_common.py","position":68,"original_position":68,"commit_id":"03cb6e22688436b8d12bd58e3759db0de4018746","original_commit_id":"03cb6e22688436b8d12bd58e3759db0de4018746","user":{"login":"marcofucci","id":178865,"node_id":"MDQ6VXNlcjE3ODg2NQ==","avatar_url":"https://avatars3.githubusercontent.com/u/178865?v=4","gravatar_id":"","url":"https://api.github.com/users/marcofucci","html_url":"https://github.com/marcofucci","followers_url":"https://api.github.com/users/marcofucci/followers","following_url":"https://api.github.com/users/marcofucci/following{/other_user}","gists_url":"https://api.github.com/users/marcofucci/gists{/gist_id}","starred_url":"https://api.github.com/users/marcofucci/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/marcofucci/subscriptions","organizations_url":"https://api.github.com/users/marcofucci/orgs","repos_url":"https://api.github.com/users/marcofucci/repos","events_url":"https://api.github.com/users/marcofucci/events{/privacy}","received_events_url":"https://api.github.com/users/marcofucci/received_events","type":"User","site_admin":false},"body":"It would be good to change the factory and add `export_to_countries` and `future_interest_countries` here for completeness.\r\n\r\n(I also realised today that the SQL does not print these tables which is something we might want to add probably in another PR).\r\n\r\nIdeally it would be good to have `implicit_related_models` build dynamically from the model but it can be in a different PR.","created_at":"2018-07-13T16:43:00Z","updated_at":"2018-07-13T16:43:00Z","html_url":"https://github.com/uktrade/data-hub-leeloo/pull/1023#discussion_r202409393","pull_request_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/pulls/1023","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/uktrade/data-hub-leeloo/pulls/comments/202409393"},"html":{"href":"https://github.com/uktrade/data-hub-leeloo/pull/1023#discussion_r202409393"},"pull_request":{"href":"https://api.github.com/repos/uktrade/data-hub-leeloo/pulls/1023"}}},"pull_request":{"url":"https://api.github.com/repos/uktrade/data-hub-leeloo/pulls/1023","id":201039629,"node_id":"MDExOlB1bGxSZXF1ZXN0MjAxMDM5NjI5","html_url":"https://github.com/uktrade/data-hub-leeloo/pull/1023","diff_url":"https://github.com/uktrade/data-hub-leeloo/pull/1023.diff","patch_url":"https://github.com/uktrade/data-hub-leeloo/pull/1023.patch","issue_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/issues/1023","number":1023,"state":"open","locked":false,"title":"Add clean-up command for deleting very old interactions","user":{"login":"reupen","id":12693549,"node_id":"MDQ6VXNlcjEyNjkzNTQ5","avatar_url":"https://avatars1.githubusercontent.com/u/12693549?v=4","gravatar_id":"","url":"https://api.github.com/users/reupen","html_url":"https://github.com/reupen","followers_url":"https://api.github.com/users/reupen/followers","following_url":"https://api.github.com/users/reupen/following{/other_user}","gists_url":"https://api.github.com/users/reupen/gists{/gist_id}","starred_url":"https://api.github.com/users/reupen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/reupen/subscriptions","organizations_url":"https://api.github.com/users/reupen/orgs","repos_url":"https://api.github.com/users/reupen/repos","events_url":"https://api.github.com/users/reupen/events{/privacy}","received_events_url":"https://api.github.com/users/reupen/received_events","type":"User","site_admin":false},"body":"Issue number: n/a\r\n\r\n### Description of change\r\n\r\nThis adds a `delete_old_records` command for deleting very old interactions.\r\n\r\nIt also adds a common base class for the `delete_old_records` and `delete_orphans` commands, which has some changes from the original command:\r\n\r\n* `--simulate` now uses a transaction with rollback to fully simulate the deletion (including any errors and which exact models were deleted)\r\n* the printing of the return value from QuerySet.delete() was improved\r\n* `--only-print-query` was added (only prints the SQL query)\r\n\r\nThe tests are shared between commands at the moment, but we can change that later on if that doesn't prove practical.\r\n\r\n### Checklist\r\n\r\n* [x] Have any relevant search models been updated?\r\n* [x] Have any relevant fixtures (`fixtures/test_data.yaml`) been updated?\r\n* [x] Have any relevant select-/prefetch-related field lists in the views and search apps been updated?\r\n* [x] Has the admin site been updated (for new models, fields etc.)?\r\n* [ ] Has the README been updated (if needed)?\r\n  _Nothing about these commands in the README at the moment_\r\n","created_at":"2018-07-12T15:26:31Z","updated_at":"2018-07-13T16:43:00Z","closed_at":null,"merged_at":null,"merge_commit_sha":"142e12c8d2c05270d4e6ba772c76e122ae3b6d10","assignee":null,"assignees":[],"requested_reviewers":[{"login":"elcct","id":5889630,"node_id":"MDQ6VXNlcjU4ODk2MzA=","avatar_url":"https://avatars2.githubusercontent.com/u/5889630?v=4","gravatar_id":"","url":"https://api.github.com/users/elcct","html_url":"https://github.com/elcct","followers_url":"https://api.github.com/users/elcct/followers","following_url":"https://api.github.com/users/elcct/following{/other_user}","gists_url":"https://api.github.com/users/elcct/gists{/gist_id}","starred_url":"https://api.github.com/users/elcct/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elcct/subscriptions","organizations_url":"https://api.github.com/users/elcct/orgs","repos_url":"https://api.github.com/users/elcct/repos","events_url":"https://api.github.com/users/elcct/events{/privacy}","received_events_url":"https://api.github.com/users/elcct/received_events","type":"User","site_admin":false}],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/pulls/1023/commits","review_comments_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/pulls/1023/comments","review_comment_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/pulls/comments{/number}","comments_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/issues/1023/comments","statuses_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/statuses/03cb6e22688436b8d12bd58e3759db0de4018746","head":{"label":"uktrade:feature/clean-up-old-interactions-2","ref":"feature/clean-up-old-interactions-2","sha":"03cb6e22688436b8d12bd58e3759db0de4018746","user":{"login":"uktrade","id":15107524,"node_id":"MDEyOk9yZ2FuaXphdGlvbjE1MTA3NTI0","avatar_url":"https://avatars2.githubusercontent.com/u/15107524?v=4","gravatar_id":"","url":"https://api.github.com/users/uktrade","html_url":"https://github.com/uktrade","followers_url":"https://api.github.com/users/uktrade/followers","following_url":"https://api.github.com/users/uktrade/following{/other_user}","gists_url":"https://api.github.com/users/uktrade/gists{/gist_id}","starred_url":"https://api.github.com/users/uktrade/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/uktrade/subscriptions","organizations_url":"https://api.github.com/users/uktrade/orgs","repos_url":"https://api.github.com/users/uktrade/repos","events_url":"https://api.github.com/users/uktrade/events{/privacy}","received_events_url":"https://api.github.com/users/uktrade/received_events","type":"Organization","site_admin":false},"repo":{"id":73379434,"node_id":"MDEwOlJlcG9zaXRvcnk3MzM3OTQzNA==","name":"data-hub-leeloo","full_name":"uktrade/data-hub-leeloo","owner":{"login":"uktrade","id":15107524,"node_id":"MDEyOk9yZ2FuaXphdGlvbjE1MTA3NTI0","avatar_url":"https://avatars2.githubusercontent.com/u/15107524?v=4","gravatar_id":"","url":"https://api.github.com/users/uktrade","html_url":"https://github.com/uktrade","followers_url":"https://api.github.com/users/uktrade/followers","following_url":"https://api.github.com/users/uktrade/following{/other_user}","gists_url":"https://api.github.com/users/uktrade/gists{/gist_id}","starred_url":"https://api.github.com/users/uktrade/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/uktrade/subscriptions","organizations_url":"https://api.github.com/users/uktrade/orgs","repos_url":"https://api.github.com/users/uktrade/repos","events_url":"https://api.github.com/users/uktrade/events{/privacy}","received_events_url":"https://api.github.com/users/uktrade/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/uktrade/data-hub-leeloo","description":"Django API for Data Hub frontend","fork":false,"url":"https://api.github.com/repos/uktrade/data-hub-leeloo","forks_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/forks","keys_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/keys{/key_id}","collaborators_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/teams","hooks_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/hooks","issue_events_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/issues/events{/number}","events_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/events","assignees_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/assignees{/user}","branches_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/branches{/branch}","tags_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/tags","blobs_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/git/refs{/sha}","trees_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/git/trees{/sha}","statuses_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/statuses/{sha}","languages_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/languages","stargazers_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/stargazers","contributors_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/contributors","subscribers_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/subscribers","subscription_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/subscription","commits_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/commits{/sha}","git_commits_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/git/commits{/sha}","comments_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/comments{/number}","issue_comment_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/issues/comments{/number}","contents_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/contents/{+path}","compare_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/compare/{base}...{head}","merges_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/merges","archive_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/downloads","issues_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/issues{/number}","pulls_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/pulls{/number}","milestones_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/milestones{/number}","notifications_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/labels{/name}","releases_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/releases{/id}","deployments_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/deployments","created_at":"2016-11-10T12:21:01Z","updated_at":"2018-07-13T14:24:23Z","pushed_at":"2018-07-13T16:05:21Z","git_url":"git://github.com/uktrade/data-hub-leeloo.git","ssh_url":"git@github.com:uktrade/data-hub-leeloo.git","clone_url":"https://github.com/uktrade/data-hub-leeloo.git","svn_url":"https://github.com/uktrade/data-hub-leeloo","homepage":null,"size":5341,"stargazers_count":1,"watchers_count":1,"language":"Python","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":11,"license":null,"forks":0,"open_issues":11,"watchers":1,"default_branch":"develop"}},"base":{"label":"uktrade:develop","ref":"develop","sha":"694a69d9d1018fb22634497df62a75110ec42d6c","user":{"login":"uktrade","id":15107524,"node_id":"MDEyOk9yZ2FuaXphdGlvbjE1MTA3NTI0","avatar_url":"https://avatars2.githubusercontent.com/u/15107524?v=4","gravatar_id":"","url":"https://api.github.com/users/uktrade","html_url":"https://github.com/uktrade","followers_url":"https://api.github.com/users/uktrade/followers","following_url":"https://api.github.com/users/uktrade/following{/other_user}","gists_url":"https://api.github.com/users/uktrade/gists{/gist_id}","starred_url":"https://api.github.com/users/uktrade/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/uktrade/subscriptions","organizations_url":"https://api.github.com/users/uktrade/orgs","repos_url":"https://api.github.com/users/uktrade/repos","events_url":"https://api.github.com/users/uktrade/events{/privacy}","received_events_url":"https://api.github.com/users/uktrade/received_events","type":"Organization","site_admin":false},"repo":{"id":73379434,"node_id":"MDEwOlJlcG9zaXRvcnk3MzM3OTQzNA==","name":"data-hub-leeloo","full_name":"uktrade/data-hub-leeloo","owner":{"login":"uktrade","id":15107524,"node_id":"MDEyOk9yZ2FuaXphdGlvbjE1MTA3NTI0","avatar_url":"https://avatars2.githubusercontent.com/u/15107524?v=4","gravatar_id":"","url":"https://api.github.com/users/uktrade","html_url":"https://github.com/uktrade","followers_url":"https://api.github.com/users/uktrade/followers","following_url":"https://api.github.com/users/uktrade/following{/other_user}","gists_url":"https://api.github.com/users/uktrade/gists{/gist_id}","starred_url":"https://api.github.com/users/uktrade/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/uktrade/subscriptions","organizations_url":"https://api.github.com/users/uktrade/orgs","repos_url":"https://api.github.com/users/uktrade/repos","events_url":"https://api.github.com/users/uktrade/events{/privacy}","received_events_url":"https://api.github.com/users/uktrade/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/uktrade/data-hub-leeloo","description":"Django API for Data Hub frontend","fork":false,"url":"https://api.github.com/repos/uktrade/data-hub-leeloo","forks_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/forks","keys_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/keys{/key_id}","collaborators_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/teams","hooks_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/hooks","issue_events_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/issues/events{/number}","events_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/events","assignees_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/assignees{/user}","branches_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/branches{/branch}","tags_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/tags","blobs_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/git/refs{/sha}","trees_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/git/trees{/sha}","statuses_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/statuses/{sha}","languages_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/languages","stargazers_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/stargazers","contributors_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/contributors","subscribers_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/subscribers","subscription_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/subscription","commits_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/commits{/sha}","git_commits_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/git/commits{/sha}","comments_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/comments{/number}","issue_comment_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/issues/comments{/number}","contents_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/contents/{+path}","compare_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/compare/{base}...{head}","merges_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/merges","archive_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/downloads","issues_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/issues{/number}","pulls_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/pulls{/number}","milestones_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/milestones{/number}","notifications_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/labels{/name}","releases_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/releases{/id}","deployments_url":"https://api.github.com/repos/uktrade/data-hub-leeloo/deployments","created_at":"2016-11-10T12:21:01Z","updated_at":"2018-07-13T14:24:23Z","pushed_at":"2018-07-13T16:05:21Z","git_url":"git://github.com/uktrade/data-hub-leeloo.git","ssh_url":"git@github.com:uktrade/data-hub-leeloo.git","clone_url":"https://github.com/uktrade/data-hub-leeloo.git","svn_url":"https://github.com/uktrade/data-hub-leeloo","homepage":null,"size":5341,"stargazers_count":1,"watchers_count":1,"language":"Python","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":11,"license":null,"forks":0,"open_issues":11,"watchers":1,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/uktrade/data-hub-leeloo/pulls/1023"},"html":{"href":"https://github.com/uktrade/data-hub-leeloo/pull/1023"},"issue":{"href":"https://api.github.com/repos/uktrade/data-hub-leeloo/issues/1023"},"comments":{"href":"https://api.github.com/repos/uktrade/data-hub-leeloo/issues/1023/comments"},"review_comments":{"href":"https://api.github.com/repos/uktrade/data-hub-leeloo/pulls/1023/comments"},"review_comment":{"href":"https://api.github.com/repos/uktrade/data-hub-leeloo/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/uktrade/data-hub-leeloo/pulls/1023/commits"},"statuses":{"href":"https://api.github.com/repos/uktrade/data-hub-leeloo/statuses/03cb6e22688436b8d12bd58e3759db0de4018746"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"marcofucci"}}