about summary refs log tree commit diff
path: root/db/migrate/20191212022653_create_imported_statuses.rb
blob: a2bf0a68de5a4d255bb4cf2e515ee36a5e6bd40f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class CreateImportedStatuses < ActiveRecord::Migration[5.2]
  def up
    create_table :imported_statuses do |t|
      t.references :status, null: false, foreign_key: {on_delete: :cascade}, index: {unique: true}
      t.string :origin, index: {unique: true}
    end

    safety_assured { execute 'INSERT INTO imported_statuses (status_id, origin) SELECT id, origin FROM statuses WHERE imported' }
    safety_assured do
      remove_column :statuses, :imported
      remove_column :statuses, :origin
    end
  end

  def down
    add_column :statuses, :imported, :boolean
    add_column :statuses, :origin, :string, index: { unique: true }
    execute 'UPDATE statuses SET imported = true, origin = s.origin FROM (SELECT status_id, origin FROM imported_statuses) AS s WHERE statuses.id = s.id'
    drop_table :imported_statuses
  end
end