20220617202502_migrate_roles.rb 849 B

1234567891011121314151617181920212223242526
  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. admin_role = UserRole.find_by(name: 'Admin')
  9. moderator_role = UserRole.find_by(name: 'Moderator')
  10. User.where(admin: true).in_batches.update_all(role_id: admin_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. moderator_role = UserRole.find_by(name: 'Moderator')
  16. User.where(role_id: admin_role.id).in_batches.update_all(admin: true) if admin_role
  17. User.where(role_id: moderator_role.id).in_batches.update_all(moderator: true) if moderator_role
  18. end
  19. end