about summary refs log tree commit diff
path: root/db/post_migrate/20220617202502_migrate_roles.rb
blob: 950699d9c99af5bc7aa6a783cec31f34296f5f3b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# frozen_string_literal: true

class MigrateRoles < ActiveRecord::Migration[5.2]
  disable_ddl_transaction!

  class UserRole < ApplicationRecord; end
  class User < ApplicationRecord; end

  def up
    load Rails.root.join('db', 'seeds', '03_roles.rb')

    owner_role     = UserRole.find_by(name: 'Owner')
    moderator_role = UserRole.find_by(name: 'Moderator')

    User.where(admin: true).in_batches.update_all(role_id: owner_role.id)
    User.where(moderator: true).in_batches.update_all(role_id: moderator_role.id)
  end

  def down
    admin_role     = UserRole.find_by(name: 'Admin')
    owner_role     = UserRole.find_by(name: 'Owner')
    moderator_role = UserRole.find_by(name: 'Moderator')

    User.where(role_id: [admin_role.id, owner_role.id]).in_batches.update_all(admin: true) if admin_role
    User.where(role_id: moderator_role.id).in_batches.update_all(moderator: true) if moderator_role
  end
end