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

    safety_assured do
      execute 'INSERT INTO sharekeys (status_id, key) SELECT id, sharekey FROM statuses WHERE local AND sharekey IS NOT NULL'
      remove_column :statuses, :sharekey
    end
  end

  def down
    add_column :statuses, :sharekey, :string
    execute 'UPDATE statuses SET sharekey = s.key FROM (SELECT status_id, key FROM sharekeys) AS s WHERE statuses.id = s.id'
    drop_table :sharekeys
  end
end