summaryrefslogtreecommitdiff
path: root/db/post_migrate/20200819202048_remove_orphaned_emails.rb
blob: 82cba244ad6b3160ca0841259d96f8ec115ab211 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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