{"action":"created","comment":{"url":"https://api.github.com/repos/tootsuite/mastodon/pulls/comments/191534633","pull_request_review_id":124125424,"id":191534633,"diff_hunk":"@@ -0,0 +1,68 @@\n+class FixAccountsUniqueIndex < ActiveRecord::Migration[5.2]\n+  disable_ddl_transaction!\n+\n+  def up\n+    duplicates = Account.connection.select_all('SELECT string_agg(id::text, \\',\\') AS ids FROM accounts GROUP BY lower(username), lower(domain) HAVING count(*) > 1').to_hash\n+\n+    duplicates.each do |row|\n+      deduplicate_account!(row['ids'].split(','))\n+    end\n+\n+    remove_index :accounts, name: 'index_accounts_on_username_and_domain_lower' if index_name_exists?(:accounts, 'index_accounts_on_username_and_domain_lower')\n+    safety_assured { execute 'CREATE UNIQUE INDEX CONCURRENTLY index_accounts_on_username_and_domain_lower ON accounts (lower(username), lower(domain))' }\n+    remove_index :accounts, name: 'index_accounts_on_username_and_domain' if index_name_exists?(:accounts, 'index_accounts_on_username_and_domain')\n+  end\n+\n+  def down\n+    raise ActiveRecord::IrreversibleMigration\n+  end\n+\n+  private\n+\n+  def deduplicate_account!(account_ids)\n+    accounts          = Account.where(id: account_ids).order(updated_at: :desc)\n+    reference_account = accounts.shift\n+\n+    accounts.each do |other_account|","path":"db/migrate/20180528141303_fix_accounts_unique_index.rb","position":26,"original_position":26,"commit_id":"f8c9184989a25bb3bf70b88c4a6b7457e5e1cdad","original_commit_id":"f8c9184989a25bb3bf70b88c4a6b7457e5e1cdad","user":{"login":"nightpool","id":233815,"avatar_url":"https://avatars2.githubusercontent.com/u/233815?v=4","gravatar_id":"","url":"https://api.github.com/users/nightpool","html_url":"https://github.com/nightpool","followers_url":"https://api.github.com/users/nightpool/followers","following_url":"https://api.github.com/users/nightpool/following{/other_user}","gists_url":"https://api.github.com/users/nightpool/gists{/gist_id}","starred_url":"https://api.github.com/users/nightpool/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nightpool/subscriptions","organizations_url":"https://api.github.com/users/nightpool/orgs","repos_url":"https://api.github.com/users/nightpool/repos","events_url":"https://api.github.com/users/nightpool/events{/privacy}","received_events_url":"https://api.github.com/users/nightpool/received_events","type":"User","site_admin":false},"body":"I'm saying each iteration of the loop should be a new transaction, so we don't lose half the progress but an error doesn't leave an account in a half-deleted state.","created_at":"2018-05-29T18:52:07Z","updated_at":"2018-05-29T18:52:08Z","html_url":"https://github.com/tootsuite/mastodon/pull/7658#discussion_r191534633","pull_request_url":"https://api.github.com/repos/tootsuite/mastodon/pulls/7658","author_association":"COLLABORATOR","_links":{"self":{"href":"https://api.github.com/repos/tootsuite/mastodon/pulls/comments/191534633"},"html":{"href":"https://github.com/tootsuite/mastodon/pull/7658#discussion_r191534633"},"pull_request":{"href":"https://api.github.com/repos/tootsuite/mastodon/pulls/7658"}},"in_reply_to_id":191262411},"pull_request":{"url":"https://api.github.com/repos/tootsuite/mastodon/pulls/7658","id":190974375,"html_url":"https://github.com/tootsuite/mastodon/pull/7658","diff_url":"https://github.com/tootsuite/mastodon/pull/7658.diff","patch_url":"https://github.com/tootsuite/mastodon/pull/7658.patch","issue_url":"https://api.github.com/repos/tootsuite/mastodon/issues/7658","number":7658,"state":"open","locked":false,"title":"Deduplicate accounts and make unique username/domain index case-insensitive","user":{"login":"Gargron","id":184731,"avatar_url":"https://avatars3.githubusercontent.com/u/184731?v=4","gravatar_id":"","url":"https://api.github.com/users/Gargron","html_url":"https://github.com/Gargron","followers_url":"https://api.github.com/users/Gargron/followers","following_url":"https://api.github.com/users/Gargron/following{/other_user}","gists_url":"https://api.github.com/users/Gargron/gists{/gist_id}","starred_url":"https://api.github.com/users/Gargron/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Gargron/subscriptions","organizations_url":"https://api.github.com/users/Gargron/orgs","repos_url":"https://api.github.com/users/Gargron/repos","events_url":"https://api.github.com/users/Gargron/events{/privacy}","received_events_url":"https://api.github.com/users/Gargron/received_events","type":"User","site_admin":false},"body":"> **Previously on Mastodon**: Username/domain uniqueness constraint exists, but is case-*sensitive*. Case-insensitive index also exists, but is not unique. Duplicates caused primarily by admins changing casing of usernames in database, and that one time we had a missing validation in Ruby for one short release.\r\n\r\nFix #6937\r\nFix #6837\r\nFix #6667\r\n\r\n1. Get all accounts with overlapping username/domain\r\n2. For each account:\r\n    1. Select most recently updated (used) one to be the reference account\r\n    2. Compare others against it\r\n        - If the public key is the same, it's definitely the same person, so they can be merged. Change attributions on owned associations to the reference account\r\n        - Delete the other account\r\n3. Re-create case-insensitive index with uniqueness constraint\r\n4. Remove now useless case-sensitive unique index\r\n\r\n> Damn, why do all those issues end in 7s. I just noticed.","created_at":"2018-05-28T17:39:35Z","updated_at":"2018-05-29T18:52:08Z","closed_at":null,"merged_at":null,"merge_commit_sha":"ab88d984d4d280614e1164914d8140d25a0df07e","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[{"id":330102524,"url":"https://api.github.com/repos/tootsuite/mastodon/labels/bug","name":"bug","color":"c91910","default":true}],"milestone":null,"commits_url":"https://api.github.com/repos/tootsuite/mastodon/pulls/7658/commits","review_comments_url":"https://api.github.com/repos/tootsuite/mastodon/pulls/7658/comments","review_comment_url":"https://api.github.com/repos/tootsuite/mastodon/pulls/comments{/number}","comments_url":"https://api.github.com/repos/tootsuite/mastodon/issues/7658/comments","statuses_url":"https://api.github.com/repos/tootsuite/mastodon/statuses/f8c9184989a25bb3bf70b88c4a6b7457e5e1cdad","head":{"label":"tootsuite:fix-accounts-index","ref":"fix-accounts-index","sha":"f8c9184989a25bb3bf70b88c4a6b7457e5e1cdad","user":{"login":"tootsuite","id":24979032,"avatar_url":"https://avatars3.githubusercontent.com/u/24979032?v=4","gravatar_id":"","url":"https://api.github.com/users/tootsuite","html_url":"https://github.com/tootsuite","followers_url":"https://api.github.com/users/tootsuite/followers","following_url":"https://api.github.com/users/tootsuite/following{/other_user}","gists_url":"https://api.github.com/users/tootsuite/gists{/gist_id}","starred_url":"https://api.github.com/users/tootsuite/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tootsuite/subscriptions","organizations_url":"https://api.github.com/users/tootsuite/orgs","repos_url":"https://api.github.com/users/tootsuite/repos","events_url":"https://api.github.com/users/tootsuite/events{/privacy}","received_events_url":"https://api.github.com/users/tootsuite/received_events","type":"Organization","site_admin":false},"repo":{"id":52281283,"name":"mastodon","full_name":"tootsuite/mastodon","owner":{"login":"tootsuite","id":24979032,"avatar_url":"https://avatars3.githubusercontent.com/u/24979032?v=4","gravatar_id":"","url":"https://api.github.com/users/tootsuite","html_url":"https://github.com/tootsuite","followers_url":"https://api.github.com/users/tootsuite/followers","following_url":"https://api.github.com/users/tootsuite/following{/other_user}","gists_url":"https://api.github.com/users/tootsuite/gists{/gist_id}","starred_url":"https://api.github.com/users/tootsuite/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tootsuite/subscriptions","organizations_url":"https://api.github.com/users/tootsuite/orgs","repos_url":"https://api.github.com/users/tootsuite/repos","events_url":"https://api.github.com/users/tootsuite/events{/privacy}","received_events_url":"https://api.github.com/users/tootsuite/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/tootsuite/mastodon","description":"Your self-hosted, globally interconnected microblogging community","fork":false,"url":"https://api.github.com/repos/tootsuite/mastodon","forks_url":"https://api.github.com/repos/tootsuite/mastodon/forks","keys_url":"https://api.github.com/repos/tootsuite/mastodon/keys{/key_id}","collaborators_url":"https://api.github.com/repos/tootsuite/mastodon/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/tootsuite/mastodon/teams","hooks_url":"https://api.github.com/repos/tootsuite/mastodon/hooks","issue_events_url":"https://api.github.com/repos/tootsuite/mastodon/issues/events{/number}","events_url":"https://api.github.com/repos/tootsuite/mastodon/events","assignees_url":"https://api.github.com/repos/tootsuite/mastodon/assignees{/user}","branches_url":"https://api.github.com/repos/tootsuite/mastodon/branches{/branch}","tags_url":"https://api.github.com/repos/tootsuite/mastodon/tags","blobs_url":"https://api.github.com/repos/tootsuite/mastodon/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/tootsuite/mastodon/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/tootsuite/mastodon/git/refs{/sha}","trees_url":"https://api.github.com/repos/tootsuite/mastodon/git/trees{/sha}","statuses_url":"https://api.github.com/repos/tootsuite/mastodon/statuses/{sha}","languages_url":"https://api.github.com/repos/tootsuite/mastodon/languages","stargazers_url":"https://api.github.com/repos/tootsuite/mastodon/stargazers","contributors_url":"https://api.github.com/repos/tootsuite/mastodon/contributors","subscribers_url":"https://api.github.com/repos/tootsuite/mastodon/subscribers","subscription_url":"https://api.github.com/repos/tootsuite/mastodon/subscription","commits_url":"https://api.github.com/repos/tootsuite/mastodon/commits{/sha}","git_commits_url":"https://api.github.com/repos/tootsuite/mastodon/git/commits{/sha}","comments_url":"https://api.github.com/repos/tootsuite/mastodon/comments{/number}","issue_comment_url":"https://api.github.com/repos/tootsuite/mastodon/issues/comments{/number}","contents_url":"https://api.github.com/repos/tootsuite/mastodon/contents/{+path}","compare_url":"https://api.github.com/repos/tootsuite/mastodon/compare/{base}...{head}","merges_url":"https://api.github.com/repos/tootsuite/mastodon/merges","archive_url":"https://api.github.com/repos/tootsuite/mastodon/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/tootsuite/mastodon/downloads","issues_url":"https://api.github.com/repos/tootsuite/mastodon/issues{/number}","pulls_url":"https://api.github.com/repos/tootsuite/mastodon/pulls{/number}","milestones_url":"https://api.github.com/repos/tootsuite/mastodon/milestones{/number}","notifications_url":"https://api.github.com/repos/tootsuite/mastodon/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/tootsuite/mastodon/labels{/name}","releases_url":"https://api.github.com/repos/tootsuite/mastodon/releases{/id}","deployments_url":"https://api.github.com/repos/tootsuite/mastodon/deployments","created_at":"2016-02-22T15:01:25Z","updated_at":"2018-05-29T18:50:43Z","pushed_at":"2018-05-29T17:03:29Z","git_url":"git://github.com/tootsuite/mastodon.git","ssh_url":"git@github.com:tootsuite/mastodon.git","clone_url":"https://github.com/tootsuite/mastodon.git","svn_url":"https://github.com/tootsuite/mastodon","homepage":"https://joinmastodon.org","size":57748,"stargazers_count":13571,"watchers_count":13571,"language":"Ruby","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":2448,"mirror_url":null,"archived":false,"open_issues_count":906,"license":{"key":"agpl-3.0","name":"GNU Affero General Public License v3.0","spdx_id":"AGPL-3.0","url":"https://api.github.com/licenses/agpl-3.0"},"forks":2448,"open_issues":906,"watchers":13571,"default_branch":"master"}},"base":{"label":"tootsuite:master","ref":"master","sha":"03f4c214b459a241a1aab08095a986475bd6f8f0","user":{"login":"tootsuite","id":24979032,"avatar_url":"https://avatars3.githubusercontent.com/u/24979032?v=4","gravatar_id":"","url":"https://api.github.com/users/tootsuite","html_url":"https://github.com/tootsuite","followers_url":"https://api.github.com/users/tootsuite/followers","following_url":"https://api.github.com/users/tootsuite/following{/other_user}","gists_url":"https://api.github.com/users/tootsuite/gists{/gist_id}","starred_url":"https://api.github.com/users/tootsuite/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tootsuite/subscriptions","organizations_url":"https://api.github.com/users/tootsuite/orgs","repos_url":"https://api.github.com/users/tootsuite/repos","events_url":"https://api.github.com/users/tootsuite/events{/privacy}","received_events_url":"https://api.github.com/users/tootsuite/received_events","type":"Organization","site_admin":false},"repo":{"id":52281283,"name":"mastodon","full_name":"tootsuite/mastodon","owner":{"login":"tootsuite","id":24979032,"avatar_url":"https://avatars3.githubusercontent.com/u/24979032?v=4","gravatar_id":"","url":"https://api.github.com/users/tootsuite","html_url":"https://github.com/tootsuite","followers_url":"https://api.github.com/users/tootsuite/followers","following_url":"https://api.github.com/users/tootsuite/following{/other_user}","gists_url":"https://api.github.com/users/tootsuite/gists{/gist_id}","starred_url":"https://api.github.com/users/tootsuite/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tootsuite/subscriptions","organizations_url":"https://api.github.com/users/tootsuite/orgs","repos_url":"https://api.github.com/users/tootsuite/repos","events_url":"https://api.github.com/users/tootsuite/events{/privacy}","received_events_url":"https://api.github.com/users/tootsuite/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/tootsuite/mastodon","description":"Your self-hosted, globally interconnected microblogging community","fork":false,"url":"https://api.github.com/repos/tootsuite/mastodon","forks_url":"https://api.github.com/repos/tootsuite/mastodon/forks","keys_url":"https://api.github.com/repos/tootsuite/mastodon/keys{/key_id}","collaborators_url":"https://api.github.com/repos/tootsuite/mastodon/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/tootsuite/mastodon/teams","hooks_url":"https://api.github.com/repos/tootsuite/mastodon/hooks","issue_events_url":"https://api.github.com/repos/tootsuite/mastodon/issues/events{/number}","events_url":"https://api.github.com/repos/tootsuite/mastodon/events","assignees_url":"https://api.github.com/repos/tootsuite/mastodon/assignees{/user}","branches_url":"https://api.github.com/repos/tootsuite/mastodon/branches{/branch}","tags_url":"https://api.github.com/repos/tootsuite/mastodon/tags","blobs_url":"https://api.github.com/repos/tootsuite/mastodon/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/tootsuite/mastodon/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/tootsuite/mastodon/git/refs{/sha}","trees_url":"https://api.github.com/repos/tootsuite/mastodon/git/trees{/sha}","statuses_url":"https://api.github.com/repos/tootsuite/mastodon/statuses/{sha}","languages_url":"https://api.github.com/repos/tootsuite/mastodon/languages","stargazers_url":"https://api.github.com/repos/tootsuite/mastodon/stargazers","contributors_url":"https://api.github.com/repos/tootsuite/mastodon/contributors","subscribers_url":"https://api.github.com/repos/tootsuite/mastodon/subscribers","subscription_url":"https://api.github.com/repos/tootsuite/mastodon/subscription","commits_url":"https://api.github.com/repos/tootsuite/mastodon/commits{/sha}","git_commits_url":"https://api.github.com/repos/tootsuite/mastodon/git/commits{/sha}","comments_url":"https://api.github.com/repos/tootsuite/mastodon/comments{/number}","issue_comment_url":"https://api.github.com/repos/tootsuite/mastodon/issues/comments{/number}","contents_url":"https://api.github.com/repos/tootsuite/mastodon/contents/{+path}","compare_url":"https://api.github.com/repos/tootsuite/mastodon/compare/{base}...{head}","merges_url":"https://api.github.com/repos/tootsuite/mastodon/merges","archive_url":"https://api.github.com/repos/tootsuite/mastodon/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/tootsuite/mastodon/downloads","issues_url":"https://api.github.com/repos/tootsuite/mastodon/issues{/number}","pulls_url":"https://api.github.com/repos/tootsuite/mastodon/pulls{/number}","milestones_url":"https://api.github.com/repos/tootsuite/mastodon/milestones{/number}","notifications_url":"https://api.github.com/repos/tootsuite/mastodon/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/tootsuite/mastodon/labels{/name}","releases_url":"https://api.github.com/repos/tootsuite/mastodon/releases{/id}","deployments_url":"https://api.github.com/repos/tootsuite/mastodon/deployments","created_at":"2016-02-22T15:01:25Z","updated_at":"2018-05-29T18:50:43Z","pushed_at":"2018-05-29T17:03:29Z","git_url":"git://github.com/tootsuite/mastodon.git","ssh_url":"git@github.com:tootsuite/mastodon.git","clone_url":"https://github.com/tootsuite/mastodon.git","svn_url":"https://github.com/tootsuite/mastodon","homepage":"https://joinmastodon.org","size":57748,"stargazers_count":13571,"watchers_count":13571,"language":"Ruby","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":2448,"mirror_url":null,"archived":false,"open_issues_count":906,"license":{"key":"agpl-3.0","name":"GNU Affero General Public License v3.0","spdx_id":"AGPL-3.0","url":"https://api.github.com/licenses/agpl-3.0"},"forks":2448,"open_issues":906,"watchers":13571,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/tootsuite/mastodon/pulls/7658"},"html":{"href":"https://github.com/tootsuite/mastodon/pull/7658"},"issue":{"href":"https://api.github.com/repos/tootsuite/mastodon/issues/7658"},"comments":{"href":"https://api.github.com/repos/tootsuite/mastodon/issues/7658/comments"},"review_comments":{"href":"https://api.github.com/repos/tootsuite/mastodon/pulls/7658/comments"},"review_comment":{"href":"https://api.github.com/repos/tootsuite/mastodon/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/tootsuite/mastodon/pulls/7658/commits"},"statuses":{"href":"https://api.github.com/repos/tootsuite/mastodon/statuses/f8c9184989a25bb3bf70b88c4a6b7457e5e1cdad"}},"author_association":"MEMBER"}}
{"actor":{"display_login":"nightpool"}}