2022-07-05 09:41:40 +09:00
|
|
|
# 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')
|
|
|
|
|
2022-11-03 00:34:47 +09:00
|
|
|
owner_role = UserRole.find_by(name: 'Owner')
|
2022-07-05 09:41:40 +09:00
|
|
|
moderator_role = UserRole.find_by(name: 'Moderator')
|
|
|
|
|
2022-11-03 00:34:47 +09:00
|
|
|
User.where(admin: true).in_batches.update_all(role_id: owner_role.id)
|
2022-07-05 09:41:40 +09:00
|
|
|
User.where(moderator: true).in_batches.update_all(role_id: moderator_role.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
admin_role = UserRole.find_by(name: 'Admin')
|
2022-11-03 00:34:47 +09:00
|
|
|
owner_role = UserRole.find_by(name: 'Owner')
|
2022-07-05 09:41:40 +09:00
|
|
|
moderator_role = UserRole.find_by(name: 'Moderator')
|
|
|
|
|
2022-11-03 00:34:47 +09:00
|
|
|
User.where(role_id: [admin_role.id, owner_role.id]).in_batches.update_all(admin: true) if admin_role
|
2022-07-05 09:41:40 +09:00
|
|
|
User.where(role_id: moderator_role.id).in_batches.update_all(moderator: true) if moderator_role
|
|
|
|
end
|
|
|
|
end
|