{"action":"created","comment":{"url":"https://api.github.com/repos/nyaruka/rapidpro/pulls/comments/46408868","id":46408868,"diff_hunk":"@@ -0,0 +1,62 @@\n+# -*- coding: utf-8 -*-\n+from __future__ import unicode_literals\n+\n+from collections import defaultdict\n+from django.db import migrations, models\n+from uuid import uuid4\n+\n+\n+def move_non_uuids_to_bod(apps, schema_editor):\n+    Channel = apps.get_model('channels', 'Channel')\n+    updates_by_type = defaultdict(lambda: 0)\n+\n+    for channel in Channel.objects.filter(channel_type__in=('NX', 'T', 'EX')):\n+        # ignore external channels whose UUIDs look valid\n+        if channel.channel_type == 'EX' and channel.uuid and len(channel.uuid) == 36:\n+            continue\n+\n+        channel.bod = channel.uuid\n+        channel.uuid = unicode(uuid4())\n+        channel.save(update_fields=('bod', 'uuid'))\n+\n+        updates_by_type[channel.channel_type] += 1\n+\n+    # nullify UUIDs on inactive Android channels\n+    updates_by_type['A-i'] = Channel.objects.filter(channel_type='A', is_active=False).update(uuid=None)\n+\n+    # gather up active channels by their UUID to find duplicates\n+    android_by_uuid = defaultdict(list)\n+    for android in Channel.objects.filter(channel_type='A', is_active=True).order_by('-last_seen'):\n+        android_by_uuid[android.uuid].append(android)\n+\n+    # for each set of Android channels sharing an UUID, last seen one keeps the UUID, others nullified\n+    for uuid, androids in android_by_uuid.iteritems():\n+        if len(androids) > 1:\n+            non_chosen_one_ids = [a.pk for a in androids[1:]]\n+            Channel.objects.filter(pk__in=non_chosen_one_ids).update(uuid=None)","path":"temba/channels/migrations/0028_channel_uuid_cleanup.py","position":36,"original_position":36,"commit_id":"42f60d4f6cc1e69d70088fc4d14df768c22e643a","original_commit_id":"42f60d4f6cc1e69d70088fc4d14df768c22e643a","user":{"login":"rowanseymour","id":675558,"avatar_url":"https://avatars.githubusercontent.com/u/675558?v=3","gravatar_id":"","url":"https://api.github.com/users/rowanseymour","html_url":"https://github.com/rowanseymour","followers_url":"https://api.github.com/users/rowanseymour/followers","following_url":"https://api.github.com/users/rowanseymour/following{/other_user}","gists_url":"https://api.github.com/users/rowanseymour/gists{/gist_id}","starred_url":"https://api.github.com/users/rowanseymour/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rowanseymour/subscriptions","organizations_url":"https://api.github.com/users/rowanseymour/orgs","repos_url":"https://api.github.com/users/rowanseymour/repos","events_url":"https://api.github.com/users/rowanseymour/events{/privacy}","received_events_url":"https://api.github.com/users/rowanseymour/received_events","type":"User","site_admin":false},"body":"This is the bit I'm not sure about. Doesn't seem like we should nullify UUID without deactivating a channel... but doesn't seem great to deactivate a channel without properly releasing it... but not sure if we should do that from a migration","created_at":"2015-12-02T12:39:00Z","updated_at":"2015-12-02T12:39:00Z","html_url":"https://github.com/nyaruka/rapidpro/pull/453#discussion_r46408868","pull_request_url":"https://api.github.com/repos/nyaruka/rapidpro/pulls/453","_links":{"self":{"href":"https://api.github.com/repos/nyaruka/rapidpro/pulls/comments/46408868"},"html":{"href":"https://github.com/nyaruka/rapidpro/pull/453#discussion_r46408868"},"pull_request":{"href":"https://api.github.com/repos/nyaruka/rapidpro/pulls/453"}}},"pull_request":{"url":"https://api.github.com/repos/nyaruka/rapidpro/pulls/453","id":52126289,"html_url":"https://github.com/nyaruka/rapidpro/pull/453","diff_url":"https://github.com/nyaruka/rapidpro/pull/453.diff","patch_url":"https://github.com/nyaruka/rapidpro/pull/453.patch","issue_url":"https://api.github.com/repos/nyaruka/rapidpro/issues/453","number":453,"state":"open","locked":false,"title":"Standard UUIDs for channels","user":{"login":"rowanseymour","id":675558,"avatar_url":"https://avatars.githubusercontent.com/u/675558?v=3","gravatar_id":"","url":"https://api.github.com/users/rowanseymour","html_url":"https://github.com/rowanseymour","followers_url":"https://api.github.com/users/rowanseymour/followers","following_url":"https://api.github.com/users/rowanseymour/following{/other_user}","gists_url":"https://api.github.com/users/rowanseymour/gists{/gist_id}","starred_url":"https://api.github.com/users/rowanseymour/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rowanseymour/subscriptions","organizations_url":"https://api.github.com/users/rowanseymour/orgs","repos_url":"https://api.github.com/users/rowanseymour/repos","events_url":"https://api.github.com/users/rowanseymour/events{/privacy}","received_events_url":"https://api.github.com/users/rowanseymour/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-11-30T13:55:02Z","updated_at":"2015-12-02T12:39:00Z","closed_at":null,"merged_at":null,"merge_commit_sha":"b06ffc0b26ed7272cc187d6a4f22ebe944ddc69f","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/nyaruka/rapidpro/pulls/453/commits","review_comments_url":"https://api.github.com/repos/nyaruka/rapidpro/pulls/453/comments","review_comment_url":"https://api.github.com/repos/nyaruka/rapidpro/pulls/comments{/number}","comments_url":"https://api.github.com/repos/nyaruka/rapidpro/issues/453/comments","statuses_url":"https://api.github.com/repos/nyaruka/rapidpro/statuses/42f60d4f6cc1e69d70088fc4d14df768c22e643a","head":{"label":"nyaruka:channel_uuids","ref":"channel_uuids","sha":"42f60d4f6cc1e69d70088fc4d14df768c22e643a","user":{"login":"nyaruka","id":299256,"avatar_url":"https://avatars.githubusercontent.com/u/299256?v=3","gravatar_id":"","url":"https://api.github.com/users/nyaruka","html_url":"https://github.com/nyaruka","followers_url":"https://api.github.com/users/nyaruka/followers","following_url":"https://api.github.com/users/nyaruka/following{/other_user}","gists_url":"https://api.github.com/users/nyaruka/gists{/gist_id}","starred_url":"https://api.github.com/users/nyaruka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nyaruka/subscriptions","organizations_url":"https://api.github.com/users/nyaruka/orgs","repos_url":"https://api.github.com/users/nyaruka/repos","events_url":"https://api.github.com/users/nyaruka/events{/privacy}","received_events_url":"https://api.github.com/users/nyaruka/received_events","type":"Organization","site_admin":false},"repo":{"id":27415685,"name":"rapidpro","full_name":"nyaruka/rapidpro","owner":{"login":"nyaruka","id":299256,"avatar_url":"https://avatars.githubusercontent.com/u/299256?v=3","gravatar_id":"","url":"https://api.github.com/users/nyaruka","html_url":"https://github.com/nyaruka","followers_url":"https://api.github.com/users/nyaruka/followers","following_url":"https://api.github.com/users/nyaruka/following{/other_user}","gists_url":"https://api.github.com/users/nyaruka/gists{/gist_id}","starred_url":"https://api.github.com/users/nyaruka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nyaruka/subscriptions","organizations_url":"https://api.github.com/users/nyaruka/orgs","repos_url":"https://api.github.com/users/nyaruka/repos","events_url":"https://api.github.com/users/nyaruka/events{/privacy}","received_events_url":"https://api.github.com/users/nyaruka/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/nyaruka/rapidpro","description":"RapidPro is a hosted service allowing organizations to visually build scalable interactive messaging applications.","fork":true,"url":"https://api.github.com/repos/nyaruka/rapidpro","forks_url":"https://api.github.com/repos/nyaruka/rapidpro/forks","keys_url":"https://api.github.com/repos/nyaruka/rapidpro/keys{/key_id}","collaborators_url":"https://api.github.com/repos/nyaruka/rapidpro/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/nyaruka/rapidpro/teams","hooks_url":"https://api.github.com/repos/nyaruka/rapidpro/hooks","issue_events_url":"https://api.github.com/repos/nyaruka/rapidpro/issues/events{/number}","events_url":"https://api.github.com/repos/nyaruka/rapidpro/events","assignees_url":"https://api.github.com/repos/nyaruka/rapidpro/assignees{/user}","branches_url":"https://api.github.com/repos/nyaruka/rapidpro/branches{/branch}","tags_url":"https://api.github.com/repos/nyaruka/rapidpro/tags","blobs_url":"https://api.github.com/repos/nyaruka/rapidpro/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/nyaruka/rapidpro/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/nyaruka/rapidpro/git/refs{/sha}","trees_url":"https://api.github.com/repos/nyaruka/rapidpro/git/trees{/sha}","statuses_url":"https://api.github.com/repos/nyaruka/rapidpro/statuses/{sha}","languages_url":"https://api.github.com/repos/nyaruka/rapidpro/languages","stargazers_url":"https://api.github.com/repos/nyaruka/rapidpro/stargazers","contributors_url":"https://api.github.com/repos/nyaruka/rapidpro/contributors","subscribers_url":"https://api.github.com/repos/nyaruka/rapidpro/subscribers","subscription_url":"https://api.github.com/repos/nyaruka/rapidpro/subscription","commits_url":"https://api.github.com/repos/nyaruka/rapidpro/commits{/sha}","git_commits_url":"https://api.github.com/repos/nyaruka/rapidpro/git/commits{/sha}","comments_url":"https://api.github.com/repos/nyaruka/rapidpro/comments{/number}","issue_comment_url":"https://api.github.com/repos/nyaruka/rapidpro/issues/comments{/number}","contents_url":"https://api.github.com/repos/nyaruka/rapidpro/contents/{+path}","compare_url":"https://api.github.com/repos/nyaruka/rapidpro/compare/{base}...{head}","merges_url":"https://api.github.com/repos/nyaruka/rapidpro/merges","archive_url":"https://api.github.com/repos/nyaruka/rapidpro/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/nyaruka/rapidpro/downloads","issues_url":"https://api.github.com/repos/nyaruka/rapidpro/issues{/number}","pulls_url":"https://api.github.com/repos/nyaruka/rapidpro/pulls{/number}","milestones_url":"https://api.github.com/repos/nyaruka/rapidpro/milestones{/number}","notifications_url":"https://api.github.com/repos/nyaruka/rapidpro/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/nyaruka/rapidpro/labels{/name}","releases_url":"https://api.github.com/repos/nyaruka/rapidpro/releases{/id}","created_at":"2014-12-02T04:53:16Z","updated_at":"2015-05-08T12:28:17Z","pushed_at":"2015-12-02T11:08:50Z","git_url":"git://github.com/nyaruka/rapidpro.git","ssh_url":"git@github.com:nyaruka/rapidpro.git","clone_url":"https://github.com/nyaruka/rapidpro.git","svn_url":"https://github.com/nyaruka/rapidpro","homepage":null,"size":23399,"stargazers_count":4,"watchers_count":4,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":1,"mirror_url":null,"open_issues_count":7,"forks":1,"open_issues":7,"watchers":4,"default_branch":"master"}},"base":{"label":"nyaruka:master","ref":"master","sha":"036114181d1d81d332bfbf51ed8472a005f88df6","user":{"login":"nyaruka","id":299256,"avatar_url":"https://avatars.githubusercontent.com/u/299256?v=3","gravatar_id":"","url":"https://api.github.com/users/nyaruka","html_url":"https://github.com/nyaruka","followers_url":"https://api.github.com/users/nyaruka/followers","following_url":"https://api.github.com/users/nyaruka/following{/other_user}","gists_url":"https://api.github.com/users/nyaruka/gists{/gist_id}","starred_url":"https://api.github.com/users/nyaruka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nyaruka/subscriptions","organizations_url":"https://api.github.com/users/nyaruka/orgs","repos_url":"https://api.github.com/users/nyaruka/repos","events_url":"https://api.github.com/users/nyaruka/events{/privacy}","received_events_url":"https://api.github.com/users/nyaruka/received_events","type":"Organization","site_admin":false},"repo":{"id":27415685,"name":"rapidpro","full_name":"nyaruka/rapidpro","owner":{"login":"nyaruka","id":299256,"avatar_url":"https://avatars.githubusercontent.com/u/299256?v=3","gravatar_id":"","url":"https://api.github.com/users/nyaruka","html_url":"https://github.com/nyaruka","followers_url":"https://api.github.com/users/nyaruka/followers","following_url":"https://api.github.com/users/nyaruka/following{/other_user}","gists_url":"https://api.github.com/users/nyaruka/gists{/gist_id}","starred_url":"https://api.github.com/users/nyaruka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nyaruka/subscriptions","organizations_url":"https://api.github.com/users/nyaruka/orgs","repos_url":"https://api.github.com/users/nyaruka/repos","events_url":"https://api.github.com/users/nyaruka/events{/privacy}","received_events_url":"https://api.github.com/users/nyaruka/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/nyaruka/rapidpro","description":"RapidPro is a hosted service allowing organizations to visually build scalable interactive messaging applications.","fork":true,"url":"https://api.github.com/repos/nyaruka/rapidpro","forks_url":"https://api.github.com/repos/nyaruka/rapidpro/forks","keys_url":"https://api.github.com/repos/nyaruka/rapidpro/keys{/key_id}","collaborators_url":"https://api.github.com/repos/nyaruka/rapidpro/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/nyaruka/rapidpro/teams","hooks_url":"https://api.github.com/repos/nyaruka/rapidpro/hooks","issue_events_url":"https://api.github.com/repos/nyaruka/rapidpro/issues/events{/number}","events_url":"https://api.github.com/repos/nyaruka/rapidpro/events","assignees_url":"https://api.github.com/repos/nyaruka/rapidpro/assignees{/user}","branches_url":"https://api.github.com/repos/nyaruka/rapidpro/branches{/branch}","tags_url":"https://api.github.com/repos/nyaruka/rapidpro/tags","blobs_url":"https://api.github.com/repos/nyaruka/rapidpro/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/nyaruka/rapidpro/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/nyaruka/rapidpro/git/refs{/sha}","trees_url":"https://api.github.com/repos/nyaruka/rapidpro/git/trees{/sha}","statuses_url":"https://api.github.com/repos/nyaruka/rapidpro/statuses/{sha}","languages_url":"https://api.github.com/repos/nyaruka/rapidpro/languages","stargazers_url":"https://api.github.com/repos/nyaruka/rapidpro/stargazers","contributors_url":"https://api.github.com/repos/nyaruka/rapidpro/contributors","subscribers_url":"https://api.github.com/repos/nyaruka/rapidpro/subscribers","subscription_url":"https://api.github.com/repos/nyaruka/rapidpro/subscription","commits_url":"https://api.github.com/repos/nyaruka/rapidpro/commits{/sha}","git_commits_url":"https://api.github.com/repos/nyaruka/rapidpro/git/commits{/sha}","comments_url":"https://api.github.com/repos/nyaruka/rapidpro/comments{/number}","issue_comment_url":"https://api.github.com/repos/nyaruka/rapidpro/issues/comments{/number}","contents_url":"https://api.github.com/repos/nyaruka/rapidpro/contents/{+path}","compare_url":"https://api.github.com/repos/nyaruka/rapidpro/compare/{base}...{head}","merges_url":"https://api.github.com/repos/nyaruka/rapidpro/merges","archive_url":"https://api.github.com/repos/nyaruka/rapidpro/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/nyaruka/rapidpro/downloads","issues_url":"https://api.github.com/repos/nyaruka/rapidpro/issues{/number}","pulls_url":"https://api.github.com/repos/nyaruka/rapidpro/pulls{/number}","milestones_url":"https://api.github.com/repos/nyaruka/rapidpro/milestones{/number}","notifications_url":"https://api.github.com/repos/nyaruka/rapidpro/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/nyaruka/rapidpro/labels{/name}","releases_url":"https://api.github.com/repos/nyaruka/rapidpro/releases{/id}","created_at":"2014-12-02T04:53:16Z","updated_at":"2015-05-08T12:28:17Z","pushed_at":"2015-12-02T11:08:50Z","git_url":"git://github.com/nyaruka/rapidpro.git","ssh_url":"git@github.com:nyaruka/rapidpro.git","clone_url":"https://github.com/nyaruka/rapidpro.git","svn_url":"https://github.com/nyaruka/rapidpro","homepage":null,"size":23399,"stargazers_count":4,"watchers_count":4,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":1,"mirror_url":null,"open_issues_count":7,"forks":1,"open_issues":7,"watchers":4,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/nyaruka/rapidpro/pulls/453"},"html":{"href":"https://github.com/nyaruka/rapidpro/pull/453"},"issue":{"href":"https://api.github.com/repos/nyaruka/rapidpro/issues/453"},"comments":{"href":"https://api.github.com/repos/nyaruka/rapidpro/issues/453/comments"},"review_comments":{"href":"https://api.github.com/repos/nyaruka/rapidpro/pulls/453/comments"},"review_comment":{"href":"https://api.github.com/repos/nyaruka/rapidpro/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/nyaruka/rapidpro/pulls/453/commits"},"statuses":{"href":"https://api.github.com/repos/nyaruka/rapidpro/statuses/42f60d4f6cc1e69d70088fc4d14df768c22e643a"}}}}