20220617202502_migrate_roles.rb 919 B

123456789101112131415161718192021222324252627
  1. # frozen_string_literal: true
  2. class MigrateRoles < ActiveRecord::Migration[5.2]
  3. disable_ddl_transaction!
  4. class UserRole < ApplicationRecord; end
  5. class User < ApplicationRecord; end
  6. def up
  7. load Rails.root.join('db', 'seeds', '03_roles.rb')
  8. owner_role = UserRole.find_by(name: 'Owner')
  9. moderator_role = UserRole.find_by(name: 'Moderator')
  10. User.where(admin: true).in_batches.update_all(role_id: owner_role.id)
  11. User.where(moderator: true).in_batches.update_all(role_id: moderator_role.id)
  12. end
  13. def down
  14. admin_role = UserRole.find_by(name: 'Admin')
  15. owner_role = UserRole.find_by(name: 'Owner')
  16. moderator_role = UserRole.find_by(name: 'Moderator')
  17. User.where(role_id: [admin_role.id, owner_role.id]).in_batches.update_all(admin: true) if admin_role
  18. User.where(role_id: moderator_role.id).in_batches.update_all(moderator: true) if moderator_role
  19. end
  20. end