summaryrefslogtreecommitdiff
path: root/db/post_migrate/20200819202048_remove_orphaned_emails.rb
diff options
context:
space:
mode:
Diffstat (limited to 'db/post_migrate/20200819202048_remove_orphaned_emails.rb')
-rw-r--r--db/post_migrate/20200819202048_remove_orphaned_emails.rb20
1 files changed, 20 insertions, 0 deletions
diff --git a/db/post_migrate/20200819202048_remove_orphaned_emails.rb b/db/post_migrate/20200819202048_remove_orphaned_emails.rb
new file mode 100644
index 00000000000..82cba244ad6
--- /dev/null
+++ b/db/post_migrate/20200819202048_remove_orphaned_emails.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+class RemoveOrphanedEmails < ActiveRecord::Migration[6.0]
+ DOWNTIME = false
+
+ def up
+ execute <<~SQL
+ DELETE FROM emails
+ WHERE not exists (
+ SELECT 1 FROM users WHERE users.id = emails.user_id
+ );
+ SQL
+
+ execute 'DELETE FROM emails WHERE user_id IS NULL;'
+ end
+
+ def down
+ # no-op
+ end
+end